Skip to content

Commit 00fc9cb

Browse files
author
Dean Ward
committed
Refactor HttpJsonMetricHandler to be BufferedHttpMetricHandler
This makes it easier for derived implementations to be created that don't necessarily use JSON. This commit adds the ability to specify a media type when sending data which means derived implementations only need to handle serialization.
1 parent 0c8bb93 commit 00fc9cb

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

src/StackExchange.Metrics/Handlers/BosunMetricHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace StackExchange.Metrics.Handlers
1515
/// <summary>
1616
/// Implements <see cref="BufferedMetricHandler" /> by sending data to a Bosun endpoint.
1717
/// </summary>
18-
public class BosunMetricHandler : HttpJsonMetricHandler
18+
public class BosunMetricHandler : BufferedHttpMetricHandler
1919
{
2020
static readonly byte[] s_comma;
2121
static readonly byte[] s_startArray;
@@ -94,7 +94,7 @@ public Uri BaseUri
9494
public bool EnableExternalCounters { get; set; } = true;
9595

9696
/// <inheritdoc />
97-
protected override ValueTask SendCounterAsync(ReadOnlySequence<byte> sequence) => SendAsync(_metricUri, HttpMethod.Post, PayloadType.Counter, sequence);
97+
protected override ValueTask SendCounterAsync(ReadOnlySequence<byte> sequence) => SendAsync(_metricUri, HttpMethod.Post, PayloadType.Counter, MediaTypes.Json, sequence);
9898

9999
/// <inheritdoc />
100100
protected override ValueTask SendCumulativeCounterAsync(ReadOnlySequence<byte> sequence)
@@ -104,14 +104,14 @@ protected override ValueTask SendCumulativeCounterAsync(ReadOnlySequence<byte> s
104104
return default;
105105
}
106106

107-
return SendAsync(_counterUri, HttpMethod.Post, PayloadType.CumulativeCounter, sequence);
107+
return SendAsync(_counterUri, HttpMethod.Post, PayloadType.CumulativeCounter, MediaTypes.Json, sequence);
108108
}
109109

110110
/// <inheritdoc />
111-
protected override ValueTask SendGaugeAsync(ReadOnlySequence<byte> sequence) => SendAsync(_metricUri, HttpMethod.Post, PayloadType.Gauge, sequence);
111+
protected override ValueTask SendGaugeAsync(ReadOnlySequence<byte> sequence) => SendAsync(_metricUri, HttpMethod.Post, PayloadType.Gauge, MediaTypes.Json, sequence);
112112

113113
/// <inheritdoc />
114-
protected override ValueTask SendMetadataAsync(ReadOnlySequence<byte> sequence) => SendAsync(_metadataUri, HttpMethod.Post, PayloadType.Metadata, sequence, gzip: false);
114+
protected override ValueTask SendMetadataAsync(ReadOnlySequence<byte> sequence) => SendAsync(_metadataUri, HttpMethod.Post, PayloadType.Metadata, MediaTypes.Json, sequence, gzip: false);
115115

116116
/// <inheritdoc />
117117
protected override void SerializeMetric(IBufferWriter<byte> writer, in MetricReading reading)

src/StackExchange.Metrics/Handlers/SignalFxMetricHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ private IMetricHandler GetHandler()
139139
}
140140

141141
/// <summary>
142-
/// Implements <see cref="HttpJsonMetricHandler" /> by sending data to the SignalFX REST API.
142+
/// Implements <see cref="BufferedHttpMetricHandler" /> by sending data to the SignalFX REST API.
143143
/// </summary>
144-
private class JsonMetricHandler : HttpJsonMetricHandler
144+
private class JsonMetricHandler : BufferedHttpMetricHandler
145145
{
146146
Uri _baseUri;
147147
Uri _metricUri;
@@ -255,13 +255,13 @@ protected override void SerializeMetadata(IBufferWriter<byte> writer, IEnumerabl
255255
}
256256

257257
/// <inheritdoc />
258-
protected override ValueTask SendCounterAsync(ReadOnlySequence<byte> sequence) => SendAsync(_metricUri, HttpMethod.Post, PayloadType.Counter, sequence);
258+
protected override ValueTask SendCounterAsync(ReadOnlySequence<byte> sequence) => SendAsync(_metricUri, HttpMethod.Post, PayloadType.Counter, MediaTypes.Json, sequence);
259259

260260
/// <inheritdoc />
261-
protected override ValueTask SendCumulativeCounterAsync(ReadOnlySequence<byte> sequence) => SendAsync(_metricUri, HttpMethod.Post, PayloadType.CumulativeCounter, sequence);
261+
protected override ValueTask SendCumulativeCounterAsync(ReadOnlySequence<byte> sequence) => SendAsync(_metricUri, HttpMethod.Post, PayloadType.CumulativeCounter, MediaTypes.Json, sequence);
262262

263263
/// <inheritdoc />
264-
protected override ValueTask SendGaugeAsync(ReadOnlySequence<byte> sequence) => SendAsync(_metricUri, HttpMethod.Post, PayloadType.Gauge, sequence);
264+
protected override ValueTask SendGaugeAsync(ReadOnlySequence<byte> sequence) => SendAsync(_metricUri, HttpMethod.Post, PayloadType.Gauge, MediaTypes.Json, sequence);
265265

266266
/// <inheritdoc />
267267
protected override ValueTask SendMetadataAsync(ReadOnlySequence<byte> sequence)

src/StackExchange.Metrics/Infrastructure/HttpJsonMetricHandler.cs renamed to src/StackExchange.Metrics/Infrastructure/BufferedHttpMetricHandler.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@
99

1010
namespace StackExchange.Metrics.Infrastructure
1111
{
12+
13+
1214
/// <summary>
13-
/// Abstract implementation of <see cref="BufferedMetricHandler" /> that posts JSON
14-
/// to an HTTP endpoint.
15+
/// Abstract implementation of <see cref="BufferedMetricHandler" /> that posts content to an HTTP endpoint.
1516
/// </summary>
16-
public abstract class HttpJsonMetricHandler : BufferedMetricHandler
17+
public abstract class BufferedHttpMetricHandler : BufferedMetricHandler
1718
{
1819
private readonly Lazy<HttpClient> _httpClientFactory;
1920

2021
/// <summary>
21-
/// Constructs an instance of <see cref="HttpJsonMetricHandler" /> that sends metric data using an <see cref="HttpClient" />.
22+
/// Constructs an instance of <see cref="BufferedHttpMetricHandler" /> that sends metric data using an <see cref="HttpClient" />.
2223
/// </summary>
23-
protected HttpJsonMetricHandler()
24+
protected BufferedHttpMetricHandler()
2425
{
2526
_httpClientFactory = new Lazy<HttpClient>(CreateHttpClient);
2627
}
@@ -124,7 +125,7 @@ protected override ValueTask SendAsync(PayloadType payloadType, ReadOnlySequence
124125
protected virtual HttpClient CreateHttpClient() => new HttpClient();
125126

126127
/// <inheritdoc />
127-
protected async ValueTask SendAsync(Uri uri, HttpMethod method, PayloadType payloadType, ReadOnlySequence<byte> sequence, bool gzip = true)
128+
protected async ValueTask SendAsync(Uri uri, HttpMethod method, PayloadType payloadType, MediaTypeHeaderValue mediaType, ReadOnlySequence<byte> sequence, bool gzip = true)
128129
{
129130
if (uri == null)
130131
{
@@ -135,7 +136,7 @@ protected async ValueTask SendAsync(Uri uri, HttpMethod method, PayloadType payl
135136
var postambleLength = GetPostambleLength(payloadType);
136137
var request = new HttpRequestMessage(method, uri)
137138
{
138-
Content = new ReadOnlySequenceContent(gzip, payloadType, sequence, preambleLength, WritePreambleAsync, postambleLength, WritePostambleAsync)
139+
Content = new ReadOnlySequenceContent(gzip, payloadType, mediaType, sequence, preambleLength, WritePreambleAsync, postambleLength, WritePostambleAsync)
139140
};
140141

141142
var response = await _httpClientFactory.Value.SendAsync(request);
@@ -176,6 +177,7 @@ private class ReadOnlySequenceContent : HttpContent
176177
public ReadOnlySequenceContent(
177178
bool gzip,
178179
PayloadType type,
180+
MediaTypeHeaderValue mediaType,
179181
in ReadOnlySequence<byte> sequence,
180182
int preambleLength,
181183
Func<Stream, PayloadType, Task> writePreamble,
@@ -190,15 +192,13 @@ Func<Stream, PayloadType, Task> writePostamble
190192
_writePreamble = writePreamble;
191193
_writePostamble = writePostamble;
192194

193-
Headers.ContentType = s_jsonHeader;
195+
Headers.ContentType = mediaType;
194196
if (gzip)
195197
{
196198
Headers.ContentEncoding.Add("gzip");
197199
}
198200
}
199201

200-
static readonly MediaTypeHeaderValue s_jsonHeader = new MediaTypeHeaderValue("application/json");
201-
202202
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
203203
{
204204
IDisposable toDispose = null;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Net.Http.Headers;
2+
3+
namespace StackExchange.Metrics.Infrastructure
4+
{
5+
/// <summary>
6+
/// Common media types used by the <see cref="BufferedHttpMetricHandler"/>.
7+
/// </summary>
8+
public static class MediaTypes
9+
{
10+
/// <summary>
11+
/// Media type used for JSON payloads: application/json
12+
/// </summary>
13+
public static readonly MediaTypeHeaderValue Json = new MediaTypeHeaderValue("application/json");
14+
}
15+
}

0 commit comments

Comments
 (0)