Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up MockTraceAgent, add event-based API #501

Merged
merged 4 commits into from
Nov 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 28 additions & 38 deletions test/Datadog.Trace.TestHelpers/MockTracerAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public MockTracerAgent(int port = 8126, int retries = 5)

public event EventHandler<EventArgs<HttpListenerContext>> RequestReceived;

public event EventHandler<EventArgs<IList<IList<Span>>>> RequestDeserialized;

/// <summary>
/// Gets or sets a value indicating whether to skip serialization of traces.
/// </summary>
Expand Down Expand Up @@ -96,7 +98,7 @@ public IImmutableList<Span> WaitForSpans(
var deadline = DateTime.Now.AddMilliseconds(timeoutInMilliseconds);
var minimumOffset = (minDateTime ?? DateTimeOffset.MinValue).ToUnixTimeNanoseconds();

IImmutableList<Span> relevantSpans = null;
IImmutableList<Span> relevantSpans = ImmutableList<Span>.Empty;

while (DateTime.Now < deadline)
{
Expand Down Expand Up @@ -152,40 +154,9 @@ protected virtual void OnRequestReceived(HttpListenerContext context)
RequestReceived?.Invoke(this, new EventArgs<HttpListenerContext>(context));
}

private static List<Span> ToSpans(dynamic data)
protected virtual void OnRequestDeserialized(IList<IList<Span>> traces)
{
if (data is IDictionary dict)
{
var span = new Span
{
TraceId = dict.GetValueOrDefault<ulong>("trace_id"),
SpanId = dict.GetValueOrDefault<ulong>("span_id"),
Name = dict.GetValueOrDefault<string>("name"),
Resource = dict.GetValueOrDefault<string>("resource"),
Service = dict.GetValueOrDefault<string>("service"),
Type = dict.GetValueOrDefault<string>("type"),
Start = dict.GetValueOrDefault<long>("start"),
Duration = dict.GetValueOrDefault<ulong>("duration"),
Tags = dict.GetValueOrDefault<Dictionary<object, object>>("meta")
.ToDictionary(p => (string)p.Key, p => (string)p.Value),
};

return new List<Span> { span };
}

if (data is IEnumerable rawSpans)
{
var allSpans = new List<Span>();

foreach (var rawSpan in rawSpans)
{
allSpans.AddRange(ToSpans(rawSpan));
}

return allSpans;
}

return new List<Span>();
RequestDeserialized?.Invoke(this, new EventArgs<IList<IList<Span>>>(traces));
}

private void AssertHeader(
Expand Down Expand Up @@ -217,14 +188,14 @@ private void HandleHttpRequests()

if (ShouldDeserializeTraces)
{
var rawSpans = MessagePackSerializer.Deserialize<dynamic>(ctx.Request.InputStream);
var spans = ToSpans(rawSpans);
var spans = MessagePackSerializer.Deserialize<IList<IList<Span>>>(ctx.Request.InputStream);
OnRequestDeserialized(spans);

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));
}
}
Expand All @@ -242,26 +213,45 @@ 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("parent_id")]
public ulong? ParentId { get; set; }

[Key("error")]
public byte Error { get; set; }

[Key("meta")]
public Dictionary<string, string> Tags { get; set; }

[Key("metrics")]
public Dictionary<string, double> Metrics { get; set; }
}
}
}