/// <summary> /// Sends the given request. If tracing is initialized and enabled the outgoing request is /// traced and the trace header is added to the request. /// </summary> protected override async Task <HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { if (_tracer.GetCurrentTraceId() == null) { return(await base.SendAsync(request, cancellationToken).ConfigureAwait(false)); } var traceHeader = TraceHeaderContext.Create( _tracer.GetCurrentTraceId(), _tracer.GetCurrentSpanId() ?? 0, true); request.Headers.Add(TraceHeaderContext.TraceHeader, traceHeader.ToString()); _tracer.StartSpan(request.RequestUri.ToString()); try { return(await base.SendAsync(request, cancellationToken).ConfigureAwait(false)); } catch (Exception e) { StackTrace stackTrace = new StackTrace(e, true); _tracer.SetStackTrace(stackTrace); throw; } finally { _tracer.EndSpan(); } }
/// <summary> /// Invokes the next <see cref="RequestDelegate"/> and trace the time /// taken for the next delegate to run, reporting the results to the /// Stackdriver Trace API. /// </summary> public async Task Invoke(HttpContext httpContext, IManagedTracer tracer) { GaxPreconditions.CheckNotNull(tracer, nameof(tracer)); if (tracer.GetCurrentTraceId() == null) { await _next(httpContext); } else { // Trace the delegate and annotate it with information from the current // http context. tracer.StartSpan(httpContext.Request.Path); try { await _next(httpContext); } catch (Exception e) { StackTrace stackTrace = new StackTrace(e, true); tracer.SetStackTrace(stackTrace); throw; } finally { tracer.AnnotateSpan(Labels.AgentLabel); tracer.AnnotateSpan(Labels.FromHttpContext(httpContext)); tracer.EndSpan(); } } }
/// <summary>Traces a 10ms sleep and adds a stacktrace.</summary> public string TraceStackTrace(string id, [FromServices] IManagedTracer tracer) { string message = GetMessage(nameof(TraceStackTrace), id); tracer.StartSpan(message); Thread.Sleep(10); tracer.SetStackTrace(CreateStackTrace()); tracer.EndSpan(); return(message); }
/// <summary>Traces a 10ms sleep and adds a stacktrace.</summary> public string TraceStackTrace(string id, [FromServices] IManagedTracer tracer) { string message = EntryData.GetMessage(nameof(TraceStackTrace), id); using (tracer.StartSpan(message)) { Thread.Sleep(10); tracer.SetStackTrace(TraceEntryData.CreateStackTrace()); } return(message); }