Skip to content

Commit

Permalink
Add OTLP/HTTP binary protobuf trace exporter for .NET open-telemetry#…
Browse files Browse the repository at this point in the history
…2292

- proper variables naming
- code cleanup
  • Loading branch information
rypdal committed Sep 7, 2021
1 parent 34ef797 commit b06f415
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,5 @@ public static TResult RunSync<TResult>(Func<Task<TResult>> func)
.Unwrap()
.GetAwaiter()
.GetResult();

public static void RunSync(Func<Task> func)
=> TaskFactory
.StartNew(func)
.Unwrap()
.GetAwaiter()
.GetResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ public override ExportResult Export(in Batch<Activity> activityBatch)
// Prevents the exporter's gRPC and HTTP operations from being instrumented.
using var scope = SuppressInstrumentationScope.Begin();

var request = new OtlpCollector.ExportTraceServiceRequest();
request.AddBatch(this.ProcessResource, activityBatch);
var exportRequest = new OtlpCollector.ExportTraceServiceRequest();
exportRequest.AddBatch(this.ProcessResource, activityBatch);

try
{
var httpRequest = this.CreateHttpRequest(request);
this.HttpHandler.Send(httpRequest);
using var request = this.CreateHttpRequest(exportRequest);

using var response = this.HttpHandler.Send(request);

response?.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
Expand All @@ -68,32 +71,32 @@ public override ExportResult Export(in Batch<Activity> activityBatch)
}
finally
{
request.Return();
exportRequest.Return();
}

return ExportResult.Success;
}

private HttpRequestMessage CreateHttpRequest(OtlpCollector.ExportTraceServiceRequest request)
private HttpRequestMessage CreateHttpRequest(OtlpCollector.ExportTraceServiceRequest exportRequest)
{
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, this.Options.Endpoint);
var request = new HttpRequestMessage(HttpMethod.Post, this.Options.Endpoint);
foreach (var header in this.Headers)
{
httpRequestMessage.Headers.Add(header.Key, header.Value);
request.Headers.Add(header.Key, header.Value);
}

var content = Array.Empty<byte>();
using (var stream = new MemoryStream())
{
request.WriteTo(stream);
exportRequest.WriteTo(stream);
content = stream.ToArray();
}

var binaryContent = new ByteArrayContent(content);
binaryContent.Headers.ContentType = new MediaTypeHeaderValue(MediaContentType);
httpRequestMessage.Content = binaryContent;
request.Content = binaryContent;

return httpRequestMessage;
return request;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,14 @@ public async Task Export_ActivityBatch_SendsCorrectHttpRequest(bool includeServi

var httpHandlerMock = new Mock<IHttpHandler>();
HttpRequestMessage httpRequest = null;
var httpRequestContent = Array.Empty<byte>();
httpHandlerMock.Setup(h => h.Send(It.IsAny<HttpRequestMessage>(), It.IsAny<CancellationToken>()))
.Callback<HttpRequestMessage, CancellationToken>((r, ct) => httpRequest = r);
.Callback<HttpRequestMessage, CancellationToken>(async (r, ct) =>
{
httpRequest = r;
// We have to capture content as it can't be accessed when request is disposed inside of Export method
httpRequestContent = await r.Content.ReadAsByteArrayAsync();
});

var exporter = new OtlpHttpTraceExporter(options, httpHandlerMock.Object);

Expand Down Expand Up @@ -147,7 +153,7 @@ public async Task Export_ActivityBatch_SendsCorrectHttpRequest(bool includeServi
Assert.IsType<ByteArrayContent>(httpRequest.Content);
Assert.Contains(httpRequest.Content.Headers, h => h.Key == "Content-Type" && h.Value.First() == OtlpHttpTraceExporter.MediaContentType);

var exportTraceRequest = OtlpCollector.ExportTraceServiceRequest.Parser.ParseFrom(await httpRequest.Content.ReadAsByteArrayAsync());
var exportTraceRequest = OtlpCollector.ExportTraceServiceRequest.Parser.ParseFrom(httpRequestContent);
Assert.NotNull(exportTraceRequest);

Assert.Single(exportTraceRequest.ResourceSpans);
Expand Down

0 comments on commit b06f415

Please sign in to comment.