/// <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();
            }
        }
コード例 #2
0
            // End sample

            // Sample: UseTracer
            /// <summary>
            /// The <see cref="IManagedTracer"/> is populated by dependency injection.
            /// </summary>
            public void TraceHelloWorld(IManagedTracer tracer)
            {
                // Manually trace a specific operation.
                tracer.StartSpan(nameof(TraceHelloWorld));
                Console.Out.WriteLine("Hello, World!");
                tracer.EndSpan();
            }
コード例 #3
0
        /// <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();
                }
            }
        }
コード例 #4
0
 public IActionResult About([FromServices] IManagedTracer tracer)
 {
     tracer.StartSpan(nameof(About));
     ViewData["Message"] = "Your application description page.";
     tracer.EndSpan();
     return(View());
 }
コード例 #5
0
        /// <summary>Traces a 10ms sleep.</summary>
        public string Trace(string id, [FromServices] IManagedTracer tracer)
        {
            string message = GetMessage(nameof(Trace), id);

            tracer.StartSpan(message);
            Thread.Sleep(10);
            tracer.EndSpan();
            return(message);
        }
コード例 #6
0
        /// <summary>Traces a 10ms sleep and throws an exception.</summary>
        public string ThrowException(string id, [FromServices] IManagedTracer tracer)
        {
            // Add a span with the test id to allow for the trace to be found.
            string message = GetMessage(nameof(ThrowException), id);

            tracer.StartSpan(message);
            Thread.Sleep(10);
            tracer.EndSpan();
            throw new DivideByZeroException();
        }
コード例 #7
0
        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();
        }
コード例 #8
0
        /// <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>
        /// Creates a <see cref="TraceHeaderPropagatingHandler"/> and traces the sending of a
        /// GET request to the given uri.  The trace is wrapped in a parent span.
        /// </summary>
        /// <param name="tracer">The tracer to trace the request with.</param>
        /// <param name="rootSpanName">The name of the root span that will wrap the span
        ///     that traces the request.</param>
        /// <param name="uri">The uri to request.</param>
        /// <param name="exceptionExpected">True if an exception from the request to the uri is expected.</param>
        private async Task TraceOutGoingRequest(
            IManagedTracer tracer, string rootSpanName, string uri, bool exceptionExpected)
        {
            tracer.StartSpan(rootSpanName);
            var traceHeaderHandler = TraceHeaderPropagatingHandler.Create(tracer);

            using (var httpClient = new HttpClient(traceHeaderHandler))
            {
                try
                {
                    await httpClient.GetAsync(uri);

                    Assert.False(exceptionExpected);
                }
                catch (Exception e) when(exceptionExpected && !(e is Xunit.Sdk.XunitException))
                {
                }
            }
            tracer.EndSpan();
        }