protected internal void HandleStopEvent(HttpContext context)
        {
            var spanContext = active.Value;
            if (spanContext == null)
            {
                Logger?.LogDebug("HandleStopEvent: Missing span context");
                return;
            }

            ISpan span = spanContext.Active;
            span.PutHttpStatusCodeAttribute(context.Response.StatusCode);

            if (context.Response.Headers != null)
            {
                span.PutHttpResponseHeadersAttribute(AsList(context.Response.Headers));
            }

            long? reqSize = ExtractRequestSize(context);
            if (reqSize != null)
            {
                span.PutHttpRequestSizeAttribute(reqSize.Value);
            }

            long? respSize = ExtractResponseSize(context);
            if (respSize != null)
            {
                span.PutHttpResponseSizeAttribute(respSize.Value);
            }

            span.End();

            AsyncLocalContext.CurrentSpan = spanContext.Previous;
            active.Value = null;
        }
        /// <summary>
        /// Helper method that populates span properties from http status code according
        /// to https://github.com/open-telemetry/OpenTelemetry-specs/blob/4954074adf815f437534457331178194f6847ff9/trace/HTTP.md.
        /// </summary>
        /// <param name="span">Span to fill out.</param>
        /// <param name="statusCode">Http status code.</param>
        /// <param name="reasonPhrase">Http reason phrase.</param>
        /// <returns>Span with populated properties.</returns>
        public static ISpan PutHttpStatusCode(this ISpan span, int statusCode, string reasonPhrase)
        {
            span.PutHttpStatusCodeAttribute(statusCode);

            Status newStatus = Status.Ok;

            if ((int)statusCode < 200)
            {
                newStatus = Status.Unknown;
            }
            else if ((int)statusCode >= 200 && (int)statusCode <= 399)
            {
                newStatus = Status.Ok;
            }
            else if ((int)statusCode == 400)
            {
                newStatus = Status.InvalidArgument;
            }
            else if ((int)statusCode == 401)
            {
                newStatus = Status.Unauthenticated;
            }
            else if ((int)statusCode == 403)
            {
                newStatus = Status.PermissionDenied;
            }
            else if ((int)statusCode == 404)
            {
                newStatus = Status.NotFound;
            }
            else if ((int)statusCode == 429)
            {
                newStatus = Status.ResourceExhausted;
            }
            else if ((int)statusCode == 501)
            {
                newStatus = Status.Unimplemented;
            }
            else if ((int)statusCode == 503)
            {
                newStatus = Status.Unavailable;
            }
            else if ((int)statusCode == 504)
            {
                newStatus = Status.DeadlineExceeded;
            }
            else
            {
                newStatus = Status.Unknown;
            }

            span.Status = newStatus.WithDescription(reasonPhrase);

            return(span);
        }
Пример #3
0
        protected internal void HandleStopEvent(HttpWebRequest request, HttpStatusCode statusCode, WebHeaderCollection headers)
        {
            if (!Pending.TryRemove(request, out SpanContext spanContext))
            {
                Logger?.LogDebug("HandleStopEvent: Missing span context");
                return;
            }

            ISpan  span  = spanContext.Active;
            IScope scope = spanContext.ActiveScope;

            if (span != null)
            {
                span.PutHttpStatusCodeAttribute((int)statusCode);
                if (headers != null)
                {
                    span.PutHttpResponseHeadersAttribute(headers);
                }

                scope.Dispose();
            }
        }
Пример #4
0
        protected internal void HandleStopEvent(HttpRequestMessage request, HttpResponseMessage response, TaskStatus taskStatus)
        {
            if (!request.Properties.TryGetValue(SPANCONTEXT_KEY, out object context))
            {
                Logger?.LogDebug("HandleStopEvent: Missing span context");
                return;
            }

            SpanContext spanContext = context as SpanContext;

            if (spanContext != null)
            {
                ISpan  span  = spanContext.Active;
                IScope scope = spanContext.ActiveScope;

                if (response != null)
                {
                    span.PutHttpStatusCodeAttribute((int)response.StatusCode)
                    .PutHttpResponseHeadersAttribute(response.Headers.ToList());
                }

                if (taskStatus == TaskStatus.Faulted)
                {
                    span.PutErrorAttribute("TaskStatus.Faulted")
                    .Status = Status.Aborted;
                }

                if (taskStatus == TaskStatus.Canceled)
                {
                    span.PutErrorAttribute("TaskStatus.Canceled")
                    .Status = Status.Cancelled;
                }

                scope.Dispose();  // Calls span.End(); and replaces CurrentSpan with previous span

                request.Properties.Remove(SPANCONTEXT_KEY);
            }
        }
        protected internal void HandleStopEvent(HttpContext context)
        {
            var spanContext = ActiveValue.Value;

            if (spanContext == null)
            {
                Logger?.LogDebug("HandleStopEvent: Missing span context");
                return;
            }

            ISpan  span  = spanContext.Active;
            IScope scope = spanContext.ActiveScope;

            span.PutHttpStatusCodeAttribute(context.Response.StatusCode);

            if (context.Response.Headers != null)
            {
                span.PutHttpResponseHeadersAttribute(AsList(context.Response.Headers));
            }

            long?reqSize = ExtractRequestSize(context);

            if (reqSize != null)
            {
                span.PutHttpRequestSizeAttribute(reqSize.Value);
            }

            long?respSize = ExtractResponseSize(context);

            if (respSize != null)
            {
                span.PutHttpResponseSizeAttribute(respSize.Value);
            }

            scope.Dispose();
            ActiveValue.Value = null;
        }
        protected internal void HandleStopEvent(HttpRequestMessage request, HttpResponseMessage response, TaskStatus taskStatus)
        {
            if (!request.Properties.TryGetValue(SPANCONTEXT_KEY, out object context))
            {
                Logger?.LogDebug("HandleStopEvent: Missing span context");
                return;
            }

            var spanContext = context as SpanContext;

            if (spanContext != null)
            {
                ISpan span = spanContext.Active;
                if (response != null)
                {
                    span.PutHttpStatusCodeAttribute((int)response.StatusCode)
                    .PutHttpResponseHeadersAttribute(response.Headers.ToList());
                }

                if (taskStatus == TaskStatus.Faulted)
                {
                    span.PutErrorAttribute("TaskStatus.Faulted")
                    .Status = Status.ABORTED;
                }

                if (taskStatus == TaskStatus.Canceled)
                {
                    span.PutErrorAttribute("TaskStatus.Canceled")
                    .Status = Status.CANCELLED;
                }

                span.End();

                AsyncLocalContext.CurrentSpan = spanContext.Previous;
                request.Properties.Remove(SPANCONTEXT_KEY);
            }
        }