public async Task RunInSpanAsync_CorrectSpan() { const string traceId = "trace-id"; const string projectId = "project-id"; const string traceName = "trace-name"; const int delayInSeconds = 1; TraceProto actualTraceProto = null; Mock <IConsumer <TraceProto> > mockConsumer = new Mock <IConsumer <TraceProto> >(); mockConsumer.Setup(x => x.Receive(It.IsAny <IEnumerable <TraceProto> >())) .Callback <IEnumerable <TraceProto> >(arg => actualTraceProto = arg?.FirstOrDefault()); IManagedTracer simpleTracer = SimpleManagedTracer.Create(mockConsumer.Object, projectId, traceId); IManagedTracer delegatingTracer = new DelegatingTracer(() => simpleTracer); var stopwatch = Stopwatch.StartNew(); await delegatingTracer.RunInSpanAsync(async() => { await Task.Delay(TimeSpan.FromSeconds(delayInSeconds)); return(0); }, traceName); stopwatch.Stop(); Assert.NotNull(actualTraceProto); Assert.InRange(SpanInSeconds(actualTraceProto), delayInSeconds, stopwatch.Elapsed.Seconds); }
/// <inheritdoc /> public IManagedTracer CreateTracer(TraceHeaderContext headerContext) { GaxPreconditions.CheckNotNull(headerContext, nameof(headerContext)); if (!ShouldTrace(headerContext)) { return(NullManagedTracer.Instance); } TraceProto trace = new TraceProto { ProjectId = _projectId, TraceId = headerContext.TraceId ?? _traceIdFactory.NextId(), }; return(SimpleManagedTracer.Create(_consumer, trace, headerContext.SpanId)); }
internal static void PollAndVerifyTrace(Timestamp startTime, string automaticSpanName, string explicitSpanName, HttpResponseMessage response) { GcpTrace trace = s_polling.GetTrace(explicitSpanName, startTime); Assert.NotNull(trace); Assert.Equal(2, trace.Spans.Count); var span = trace.Spans.FirstOrDefault(s => s.Name.StartsWith(automaticSpanName)); Assert.NotNull(span); Assert.NotEmpty(span.Labels); Assert.Equal("GET", span.Labels[TraceLabels.HttpMethod]); Assert.Equal("200", span.Labels[TraceLabels.HttpStatusCode]); span = trace.Spans.FirstOrDefault(s => s.Name.StartsWith(explicitSpanName)); Assert.NotNull(span); Assert.False(response.Headers.Contains(TraceHeaderContext.TraceHeader)); }
private static long SpanInSeconds(TraceProto actualTraceProto) { var duration = actualTraceProto.Spans.First().EndTime - actualTraceProto.Spans.First().StartTime; return(duration.Seconds); }