@@ -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 {
0 commit comments