Exemple #1
0
        /// <inheritdoc />
        protected override async Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            // Set trace header if it hasn't already been set
            if (!request.Headers.Contains(SentryTraceHeader.HttpHeaderName) &&
                _hub.GetTraceHeader() is {} traceHeader)
            {
                request.Headers.Add(
                    SentryTraceHeader.HttpHeaderName,
                    traceHeader.ToString()
                    );
            }

            // Prevent null reference exception in the following call
            // in case the user didn't set an inner handler.
            InnerHandler ??= new HttpClientHandler();

            var requestMethod = request.Method.Method.ToUpperInvariant();
            var url           = request.RequestUri?.ToString() ?? string.Empty;

            // Start a span that tracks this request
            // (may be null if transaction is not set on the scope)
            var span = _hub.GetSpan()?.StartChild(
                "http.client",
                // e.g. "GET https://example.com"
                $"{requestMethod} {url}"
                );

            try
            {
                var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

                var breadcrumbData = new Dictionary <string, string>
                {
                    { "url", url },
                    { "method", requestMethod },
                    { "status_code", ((int)response.StatusCode).ToString() }
                };
                _hub.AddBreadcrumb(string.Empty, "http", "http", breadcrumbData);

                // This will handle unsuccessful status codes as well
                span?.Finish(
                    SpanStatusConverter.FromHttpStatusCode(response.StatusCode)
                    );

                return(response);
            }
            catch (Exception ex)
            {
                span?.Finish(ex);
                throw;
            }
        }
        /// <inheritdoc />
        protected override async Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            // Set trace header if it hasn't already been set
            if (!request.Headers.Contains(SentryTraceHeader.HttpHeaderName) &&
                _hub.GetTraceHeader() is {} traceHeader)
            {
                request.Headers.Add(
                    SentryTraceHeader.HttpHeaderName,
                    traceHeader.ToString()
                    );
            }

            // Prevent null reference exception in the following call
            // in case the user didn't set an inner handler.
            InnerHandler ??= new HttpClientHandler();

            // Start a span that tracks this request
            // (may be null if transaction is not set on the scope)
            var span = _hub.GetSpan()?.StartChild(
                "http.client",
                // e.g. "GET https://example.com"
                $"{request.Method.Method.ToUpperInvariant()} {request.RequestUri}"
                );

            try
            {
                var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

                // This will handle unsuccessful status codes as well
                span?.Finish(
                    SpanStatusConverter.FromHttpStatusCode(response.StatusCode)
                    );

                return(response);
            }
            catch
            {
                // TODO: attach the exception to the span, once
                // that API is available.
                span?.Finish(SpanStatus.UnknownError);

                throw;
            }
        }