/// <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();
            }
        }
Exemple #2
0
        /// <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 Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var tracer = _managedTracerFactory();

            if (tracer.GetCurrentTraceId() == null)
            {
                return(base.SendAsync(request, cancellationToken));
            }

            var traceHeader = TraceHeaderContext.Create(
                tracer.GetCurrentTraceId(), tracer.GetCurrentSpanId() ?? 0, true);

            request.Headers.Add(TraceHeaderContext.TraceHeader, traceHeader.ToString());

            return(tracer.RunInSpanAsync(
                       async() =>
            {
                tracer.AnnotateSpan(TraceLabels.FromHttpRequestMessage(request));
                var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
                tracer.AnnotateSpan(TraceLabels.FromHttpResponseMessage(response));
                return response;
            },
                       request.RequestUri.ToString()));
        }
        /// <inheritdoc />
        public IManagedTracer CreateTracer(TraceHeaderContext headerContext)
        {
            GaxPreconditions.CheckNotNull(headerContext, nameof(headerContext));
            if (!ShouldTrace(headerContext))
            {
                return(NullManagedTracer.Instance);
            }
            var traceId = headerContext.TraceId ?? _traceIdFactory.NextId();

            return(SimpleManagedTracer.Create(_consumer, _projectId, traceId, headerContext.SpanId));
        }
Exemple #4
0
        /// <inheritdoc />
        public IManagedTracer CreateTracer(TraceHeaderContext headerContext)
        {
            GaxPreconditions.CheckNotNull(headerContext, nameof(headerContext));
            if (!ShouldTrace(headerContext))
            {
                return(DoNothingTracer.Instance);
            }

            TraceProto trace = new TraceProto
            {
                ProjectId = _projectId,
                TraceId   = headerContext.TraceId ?? _traceIdFactory.NextId(),
            };

            return(SimpleManagedTracer.Create(_consumer, trace, headerContext.SpanId));
        }
Exemple #5
0
        /// <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 Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var tracer = _managedTracerFactory();

            if (tracer.GetCurrentTraceId() == null)
            {
                return(base.SendAsync(request, cancellationToken));
            }

            var traceHeader = TraceHeaderContext.Create(
                tracer.GetCurrentTraceId(), tracer.GetCurrentSpanId() ?? 0, true);

            request.Headers.Add(TraceHeaderContext.TraceHeader, traceHeader.ToString());

            return(tracer.RunInSpanAsync(
                       () => base.SendAsync(request, cancellationToken),
                       request.RequestUri.ToString()));
        }
Exemple #6
0
 /// <summary>
 /// True if the tracing should occur. Decision based on a <see cref="TraceHeaderContext"/>
 /// and an <see cref="ITraceOptionsFactory"/>.
 /// </summary>
 internal bool ShouldTrace(TraceHeaderContext headerContext) =>
 headerContext.ShouldTrace ?? _optionsFactory.CreateOptions().ShouldTrace;