/// <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();
            }
        }
        public async Task <IActionResult> AllTags()
        {
            // var handler = new TraceHeaderPropagatingHandler();
            var headers       = HttpContext.Request.Headers;
            var spanIdBefore  = _managedTracer.GetCurrentSpanId();
            var traceIdBefore = _managedTracer.GetCurrentTraceId();

            _logger.LogInformation(
                "headers: {@HeadersBefore}, spanIdBefore: {@SpanIdBefore}, traceIdBefore: {@TraceIdBefore}",
                headers, spanIdBefore, traceIdBefore);
            // var span = _managedTracer.StartSpan(nameof(AllTags))
            using (var span = _managedTracer.StartSpan(nameof(AllTags)))
            {
                var headersAfter = HttpContext.Request.Headers;
                var spanIdAfter  = _managedTracer.GetCurrentSpanId();
                var traceIdAfter = _managedTracer.GetCurrentTraceId();

                _logger.LogInformation(
                    "span: {@Span}, headersAfter: {@HeadersAfter}, spanIdAfter: {@SpanIdAfter}, traceIdAfter: {@TraceIdAfter}",
                    span, headersAfter, spanIdAfter, traceIdAfter);
                // var traceHeaderHandler = new TraceHeaderPropagatingHandler(() => _managedTracer);
                var result = await _tagBL.GetAllTags();

                return(Ok(result));
            }
        }
Ejemplo n.º 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();
                }
            }
        }
        public void CreateTracer_SimpleManagedTracer()
        {
            IManagedTracer tracer = s_tracerFactoryNoLimit.CreateTracer(s_headerTrue);

            Assert.IsType(typeof(SimpleManagedTracer), tracer);
            Assert.Equal(tracer.GetCurrentTraceId(), TraceId);
            Assert.Equal(tracer.GetCurrentSpanId(), SpanId);
        }
Ejemplo n.º 5
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();
        }