From 12d3f5828b9c05e60c9de2a8a1c3636cd8b28e34 Mon Sep 17 00:00:00 2001 From: Lucas Pimentel-Ordyna Date: Tue, 3 Sep 2019 15:49:08 -0400 Subject: [PATCH 1/4] avoid null relevantSpans --- test/Datadog.Trace.TestHelpers/MockTracerAgent.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs b/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs index 8e54f77c7923..6ee6d9d505a3 100644 --- a/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs +++ b/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs @@ -96,7 +96,7 @@ public IImmutableList WaitForSpans( var deadline = DateTime.Now.AddMilliseconds(timeoutInMilliseconds); var minimumOffset = (minDateTime ?? DateTimeOffset.MinValue).ToUnixTimeNanoseconds(); - IImmutableList relevantSpans = null; + IImmutableList relevantSpans = ImmutableList.Empty; while (DateTime.Now < deadline) { From 4a44e2716f0c5c9c0ec2cd6dee5c5e46dfcb1e76 Mon Sep 17 00:00:00 2001 From: Lucas Pimentel-Ordyna Date: Tue, 3 Sep 2019 15:51:20 -0400 Subject: [PATCH 2/4] replace use of "dynamic" with deserialization attributes --- .../MockTracerAgent.cs | 54 +++++-------------- 1 file changed, 14 insertions(+), 40 deletions(-) diff --git a/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs b/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs index 6ee6d9d505a3..201dd4e99362 100644 --- a/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs +++ b/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs @@ -152,42 +152,6 @@ protected virtual void OnRequestReceived(HttpListenerContext context) RequestReceived?.Invoke(this, new EventArgs(context)); } - private static List ToSpans(dynamic data) - { - if (data is IDictionary dict) - { - var span = new Span - { - TraceId = dict.GetValueOrDefault("trace_id"), - SpanId = dict.GetValueOrDefault("span_id"), - Name = dict.GetValueOrDefault("name"), - Resource = dict.GetValueOrDefault("resource"), - Service = dict.GetValueOrDefault("service"), - Type = dict.GetValueOrDefault("type"), - Start = dict.GetValueOrDefault("start"), - Duration = dict.GetValueOrDefault("duration"), - Tags = dict.GetValueOrDefault>("meta") - .ToDictionary(p => (string)p.Key, p => (string)p.Value), - }; - - return new List { span }; - } - - if (data is IEnumerable rawSpans) - { - var allSpans = new List(); - - foreach (var rawSpan in rawSpans) - { - allSpans.AddRange(ToSpans(rawSpan)); - } - - return allSpans; - } - - return new List(); - } - private void AssertHeader( NameValueCollection headers, string headerKey, @@ -217,14 +181,13 @@ private void HandleHttpRequests() if (ShouldDeserializeTraces) { - var rawSpans = MessagePackSerializer.Deserialize(ctx.Request.InputStream); - var spans = ToSpans(rawSpans); + var spans = MessagePackSerializer.Deserialize>>(ctx.Request.InputStream); lock (this) { // we only need to lock when replacing the span collection, // not when reading it because it is immutable - Spans = Spans.AddRange(spans); + Spans = Spans.AddRange(spans.SelectMany(trace => trace)); RequestHeaders = RequestHeaders.Add(new NameValueCollection(ctx.Request.Headers)); } } @@ -242,25 +205,36 @@ private void HandleHttpRequests() } } + [MessagePackObject] [DebuggerDisplay("TraceId={TraceId}, SpanId={SpanId}, Service={Service}, Name={Name}, Resource={Resource}")] public struct Span { + [Key("trace_id")] public ulong TraceId { get; set; } + [Key("span_id")] public ulong SpanId { get; set; } + [Key("name")] public string Name { get; set; } + [Key("resource")] public string Resource { get; set; } + [Key("service")] public string Service { get; set; } + [Key("type")] public string Type { get; set; } + [Key("start")] public long Start { get; set; } - public ulong Duration { get; set; } + [Key("duration")] + public long Duration { get; set; } + + [Key("meta")] public Dictionary Tags { get; set; } } } From 7d6716003df01b8b920da1e15da997c8ff7f0fd1 Mon Sep 17 00:00:00 2001 From: Lucas Pimentel-Ordyna Date: Tue, 3 Sep 2019 15:51:36 -0400 Subject: [PATCH 3/4] add missing Span properties --- test/Datadog.Trace.TestHelpers/MockTracerAgent.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs b/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs index 201dd4e99362..97470d51fa84 100644 --- a/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs +++ b/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs @@ -233,9 +233,17 @@ public struct Span [Key("duration")] public long Duration { get; set; } + [Key("parent_id")] + public ulong? ParentId { get; set; } + + [Key("error")] + public byte Error { get; set; } [Key("meta")] public Dictionary Tags { get; set; } + + [Key("metrics")] + public Dictionary Metrics { get; set; } } } } From d7d7b83f9d2c75917bf136e49bc56f863dd02f95 Mon Sep 17 00:00:00 2001 From: Lucas Pimentel-Ordyna Date: Tue, 3 Sep 2019 15:52:19 -0400 Subject: [PATCH 4/4] add RequestDeserialized event --- test/Datadog.Trace.TestHelpers/MockTracerAgent.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs b/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs index 97470d51fa84..a2965f58d0fa 100644 --- a/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs +++ b/test/Datadog.Trace.TestHelpers/MockTracerAgent.cs @@ -56,6 +56,8 @@ public MockTracerAgent(int port = 8126, int retries = 5) public event EventHandler> RequestReceived; + public event EventHandler>>> RequestDeserialized; + /// /// Gets or sets a value indicating whether to skip serialization of traces. /// @@ -152,6 +154,11 @@ protected virtual void OnRequestReceived(HttpListenerContext context) RequestReceived?.Invoke(this, new EventArgs(context)); } + protected virtual void OnRequestDeserialized(IList> traces) + { + RequestDeserialized?.Invoke(this, new EventArgs>>(traces)); + } + private void AssertHeader( NameValueCollection headers, string headerKey, @@ -182,6 +189,7 @@ private void HandleHttpRequests() if (ShouldDeserializeTraces) { var spans = MessagePackSerializer.Deserialize>>(ctx.Request.InputStream); + OnRequestDeserialized(spans); lock (this) {