/// <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(); } } }
private void BeginRequest(object sender, EventArgs e) { TraceHeaderContext headerContext = TraceHeaderContext.FromRequest(HttpContext.Current.Request); TraceOptions headerOptions = _headerFactory.CreateOptions(headerContext); // If the trace header says to trace or if the rate limiter allows tracing continue. if (!headerOptions.ShouldTrace) { TraceOptions options = _rateFactory.CreateOptions(); if (!options.ShouldTrace) { return; } } // Create and set the tracer for the request. TraceProto trace = new TraceProto { ProjectId = _projectId, TraceId = headerContext.TraceId ?? _traceIdfactory.NextId(), }; IManagedTracer tracer = SimpleManagedTracer.Create(_consumer, trace, headerContext.SpanId); TracerManager.SetCurrentTracer(tracer); // Start the span and annotate it with information from the current request. tracer.StartSpan(HttpContext.Current.Request.Path); tracer.AnnotateSpan(Labels.FromHttpRequest(HttpContext.Current.Request)); }
public string CreateNoSpan(string id) { string message = EntryData.GetMessage(nameof(CreateNoSpan), id); _tracer.AnnotateSpan( new Dictionary <string, string> { { "Message", message } }); return(message); }
private void EndRequest(object sender, EventArgs e) { IManagedTracer tracer = Tracer; if (tracer.GetCurrentTraceId() == null) { return; } // End the span and annotate it with information from the current response. tracer.AnnotateSpan(Labels.FromHttpResponse(HttpContext.Current.Response)); tracer.EndSpan(); }
/// <summary>Traces a 10ms sleep and adds an annotation.</summary> public string TraceLabels(string id, [FromServices] IManagedTracer tracer) { string message = GetMessage(nameof(TraceLabels), id); tracer.StartSpan(message); Thread.Sleep(10); tracer.AnnotateSpan(new Dictionary <string, string> { { Label, LabelValue } }); tracer.EndSpan(); return(message); }
/// <summary>Traces a 10ms sleep and adds an annotation.</summary> public string TraceLabels(string id, [FromServices] IManagedTracer tracer) { string message = EntryData.GetMessage(nameof(TraceLabels), id); using (tracer.StartSpan(message)) { Thread.Sleep(10); tracer.AnnotateSpan(new Dictionary <string, string> { { TraceEntryData.TraceLabel, TraceEntryData.TraceLabelValue } }); return(message); } }