Skip to content

Commit 3bca90c

Browse files
committed
Serialize directly to stream
1 parent e7fe431 commit 3bca90c

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
lines changed

src/Datadog.Tracer.Tests/TracerTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Moq;
22
using System;
3-
using System.Collections.Generic;
43
using System.Linq;
54
using System.Threading.Tasks;
65
using Xunit;

src/Datadog.Tracer/Api.cs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ internal class Api : IApi
1313
{
1414
private const string TracesPath = "/v0.3/traces";
1515
private const string ServicesPath = "/v0.3/services";
16-
17-
private static MessagePackSerializer<IList<List<Span>>> _traceSerializer;
18-
private static MessagePackSerializer<ServiceInfo> _serviceSerializer;
16+
private static SerializationContext _serializationContext;
1917

2018
static Api()
2119
{
22-
var serializationContext = new SerializationContext();
23-
var spanSerializer = new SpanMessagePackSerializer(serializationContext);
24-
var serviceSerializer = new ServiceInfoMessagePackSerializer(serializationContext);
25-
serializationContext.ResolveSerializer += (sender, eventArgs) => {
20+
_serializationContext = new SerializationContext();
21+
var spanSerializer = new SpanMessagePackSerializer(_serializationContext);
22+
var serviceSerializer = new ServiceInfoMessagePackSerializer(_serializationContext);
23+
_serializationContext.ResolveSerializer += (sender, eventArgs) => {
2624
if (eventArgs.TargetType == typeof(Span))
2725
{
2826
eventArgs.SetSerializer(spanSerializer);
@@ -32,8 +30,6 @@ static Api()
3230
eventArgs.SetSerializer(serviceSerializer);
3331
}
3432
};
35-
_traceSerializer = serializationContext.GetSerializer<IList<List<Span>>>();
36-
_serviceSerializer = serializationContext.GetSerializer<ServiceInfo>();
3733
}
3834

3935
private Uri _tracesEndpoint;
@@ -62,15 +58,9 @@ public async Task SendTracesAsync(IList<List<Span>> traces)
6258
{
6359
try
6460
{
65-
// TODO:bertrand avoid using a memory stream and stream the serialized content directly to the network
66-
using (var ms = new MemoryStream())
67-
{
68-
await _traceSerializer.PackAsync(ms, traces);
69-
var content = new ByteArrayContent(ms.GetBuffer());
70-
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/msgpack");
71-
var response = await _client.PostAsync(_tracesEndpoint, content);
72-
response.EnsureSuccessStatusCode();
73-
}
61+
var content = new MsgPackContent<IList<List<Span>>>(traces, _serializationContext);
62+
var response = await _client.PostAsync(_tracesEndpoint, content);
63+
response.EnsureSuccessStatusCode();
7464
}
7565
catch
7666
{
@@ -82,15 +72,9 @@ public async Task SendServiceAsync(ServiceInfo service)
8272
{
8373
try
8474
{
85-
// TODO:bertrand avoid using a memory stream and stream the serialized content directly to the network
86-
using (var ms = new MemoryStream())
87-
{
88-
await _serviceSerializer.PackAsync(ms, service);
89-
var content = new ByteArrayContent(ms.GetBuffer());
90-
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/msgpack");
91-
var response = await _client.PostAsync(_servicesEndpoint, content);
92-
response.EnsureSuccessStatusCode();
93-
}
75+
var content = new MsgPackContent<ServiceInfo>(service, _serializationContext);
76+
var response = await _client.PostAsync(_servicesEndpoint, content);
77+
response.EnsureSuccessStatusCode();
9478
}
9579
catch
9680
{
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using MsgPack.Serialization;
2+
using System.IO;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Net.Http.Headers;
6+
using System.Threading.Tasks;
7+
8+
namespace Datadog.Tracer
9+
{
10+
public class MsgPackContent<T> : HttpContent
11+
{
12+
public T Value { get; private set; }
13+
private SerializationContext _serializationContext;
14+
15+
public MsgPackContent(T value, SerializationContext serializationContext)
16+
{
17+
Value = value;
18+
Headers.ContentType = new MediaTypeHeaderValue("application/msgpack");
19+
_serializationContext = serializationContext;
20+
}
21+
22+
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
23+
{
24+
var serializer = _serializationContext.GetSerializer<T>();
25+
await serializer.PackAsync(stream, Value);
26+
}
27+
28+
protected override bool TryComputeLength(out long length)
29+
{
30+
// We can't compute the length beforehand
31+
length = -1;
32+
return false;
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)