private static void AddRequestTagsAndInstrumentRequest(HttpWebRequest request, Activity activity)
        {
            activity.DisplayName = HttpTagHelper.GetOperationNameForHttpMethod(request.Method);

            InstrumentRequest(request, activity);

            if (activity.IsAllDataRequested)
            {
                try
                {
                    Options.Enrich?.Invoke(activity, "OnStartActivity", request);
                }
                catch (Exception ex)
                {
                    HttpInstrumentationEventSource.Log.EnrichmentException(ex);
                }

                activity.SetTag(SemanticConventions.AttributeHttpMethod, request.Method);
                activity.SetTag(SemanticConventions.AttributeHttpHost, HttpTagHelper.GetHostTagValueFromRequestUri(request.RequestUri));
                activity.SetTag(SemanticConventions.AttributeHttpUrl, request.RequestUri.OriginalString);
                if (Options.SetHttpFlavor)
                {
                    activity.SetTag(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.ProtocolVersion));
                }
            }
        }
Пример #2
0
        private static void AddResponseTags(HttpWebResponse response, Activity activity)
        {
            activity.SetCustomProperty("HttpWebRequest.Response", response);

            if (activity.IsAllDataRequested)
            {
                activity.AddTag(SemanticConventions.AttributeHttpStatusCode, HttpTagHelper.GetStatusCodeTagValueFromHttpStatusCode(response.StatusCode));

                activity.SetStatus(
                    SpanHelper
                    .ResolveSpanStatusForHttpStatusCode((int)response.StatusCode)
                    .WithDescription(response.StatusDescription));
            }
        }
        private static void AddRequestTagsAndInstrumentRequest(HttpWebRequest request, Activity activity)
        {
            activity.DisplayName = HttpTagHelper.GetOperationNameForHttpMethod(request.Method);

            InstrumentRequest(request, activity);

            if (activity.IsAllDataRequested)
            {
                activity.SetCustomProperty(RequestCustomPropertyName, request);
                activity.SetTag(SemanticConventions.AttributeHttpMethod, request.Method);
                activity.SetTag(SemanticConventions.AttributeHttpHost, HttpTagHelper.GetHostTagValueFromRequestUri(request.RequestUri));
                activity.SetTag(SemanticConventions.AttributeHttpUrl, request.RequestUri.OriginalString);
                if (Options.SetHttpFlavor)
                {
                    activity.SetTag(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.ProtocolVersion));
                }
            }
        }
Пример #4
0
        public override void OnStartActivity(Activity activity, object payload)
        {
            if (!(this.startRequestFetcher.Fetch(payload) is HttpRequestMessage request))
            {
                HttpInstrumentationEventSource.Log.NullPayload(nameof(HttpHandlerDiagnosticListener), nameof(this.OnStartActivity));
                return;
            }

            if (this.options.TextFormat.IsInjected(request, HttpRequestMessageHeaderValuesGetter))
            {
                // this request is already instrumented, we should back off
                activity.IsAllDataRequested = false;
                return;
            }

            activity.SetKind(ActivityKind.Client);
            activity.DisplayName = HttpTagHelper.GetOperationNameForHttpMethod(request.Method);

            this.activitySource.Start(activity);

            if (activity.IsAllDataRequested)
            {
                activity.AddTag(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method));
                activity.AddTag(SemanticConventions.AttributeHttpHost, HttpTagHelper.GetHostTagValueFromRequestUri(request.RequestUri));
                activity.AddTag(SemanticConventions.AttributeHttpUrl, request.RequestUri.OriginalString);

                if (this.options.SetHttpFlavor)
                {
                    activity.AddTag(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version));
                }
            }

            if (!(this.httpClientSupportsW3C && this.options.TextFormat is TraceContextFormat))
            {
                this.options.TextFormat.Inject(activity.Context, request, HttpRequestMessageHeaderValueSetter);
            }
        }
Пример #5
0
        public override void OnStopActivity(Activity activity, object payload)
        {
            if (activity.IsAllDataRequested)
            {
                var requestTaskStatus = this.stopRequestStatusFetcher.Fetch(payload) as TaskStatus?;

                if (requestTaskStatus.HasValue)
                {
                    if (requestTaskStatus != TaskStatus.RanToCompletion)
                    {
                        if (requestTaskStatus == TaskStatus.Canceled)
                        {
                            activity.SetStatus(Status.Cancelled);
                        }
                        else if (requestTaskStatus != TaskStatus.Faulted)
                        {
                            // Faults are handled in OnException and should already have a span.Status of Unknown w/ Description.
                            activity.SetStatus(Status.Unknown);
                        }
                    }
                }

                if (this.stopResponseFetcher.Fetch(payload) is HttpResponseMessage response)
                {
                    // response could be null for DNS issues, timeouts, etc...
                    activity.SetTag(SemanticConventions.AttributeHttpStatusCode, HttpTagHelper.GetStatusCodeTagValueFromHttpStatusCode(response.StatusCode));

                    activity.SetStatus(
                        SpanHelper
                        .ResolveSpanStatusForHttpStatusCode((int)response.StatusCode)
                        .WithDescription(response.ReasonPhrase));
                }
            }

            this.activitySource.Stop(activity);
        }
Пример #6
0
        private static void AddExceptionTags(Exception exception, Activity activity)
        {
            activity.SetCustomProperty("HttpWebRequest.Exception", exception);

            if (!activity.IsAllDataRequested)
            {
                return;
            }

            Status status;

            if (exception is WebException wexc)
            {
                if (wexc.Response is HttpWebResponse response)
                {
                    activity.AddTag(SemanticConventions.AttributeHttpStatusCode, HttpTagHelper.GetStatusCodeTagValueFromHttpStatusCode(response.StatusCode));

                    status = SpanHelper.ResolveSpanStatusForHttpStatusCode((int)response.StatusCode).WithDescription(response.StatusDescription);
                }
                else
                {
                    switch (wexc.Status)
                    {
                    case WebExceptionStatus.Timeout:
                        status = Status.DeadlineExceeded;
                        break;

                    case WebExceptionStatus.NameResolutionFailure:
                        status = Status.InvalidArgument.WithDescription(exception.Message);
                        break;

                    case WebExceptionStatus.SendFailure:
                    case WebExceptionStatus.ConnectFailure:
                    case WebExceptionStatus.SecureChannelFailure:
                    case WebExceptionStatus.TrustFailure:
                        status = Status.FailedPrecondition.WithDescription(exception.Message);
                        break;

                    case WebExceptionStatus.ServerProtocolViolation:
                        status = Status.Unimplemented.WithDescription(exception.Message);
                        break;

                    case WebExceptionStatus.RequestCanceled:
                        status = Status.Cancelled;
                        break;

                    case WebExceptionStatus.MessageLengthLimitExceeded:
                        status = Status.ResourceExhausted.WithDescription(exception.Message);
                        break;

                    default:
                        status = Status.Unknown.WithDescription(exception.Message);
                        break;
                    }
                }
            }
            else
            {
                status = Status.Unknown.WithDescription(exception.Message);
            }

            activity.SetStatus(status);
        }
        public override void OnStopActivity(Activity activity, object payload)
        {
            if (Sdk.SuppressInstrumentation)
            {
                return;
            }

            if (this.stopResponseFetcher.TryFetch(payload, out HttpResponseMessage response) && response != null)
            {
                var request = response.RequestMessage;

                // TODO: This is just a minimal set of attributes. See the spec for additional attributes:
                // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/http-metrics.md#http-client
                var tags = new KeyValuePair <string, object>[]
                {
                    new KeyValuePair <string, object>(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method)),
                    new KeyValuePair <string, object>(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme),
                    new KeyValuePair <string, object>(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode),
                    new KeyValuePair <string, object>(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version)),
                };

                this.httpClientDuration.Record(activity.Duration.TotalMilliseconds, tags);
            }
        }