Skip to content

Commit

Permalink
Update to latest (#4023)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Taillefer <mataille@microsoft.com>
  • Loading branch information
geeknoid and Martin Taillefer authored May 31, 2023
1 parent 98673d4 commit 3d9bb86
Show file tree
Hide file tree
Showing 26 changed files with 936 additions and 267 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ internal static class Constants
public const string AttributeNetPeerName = "net.peer.name";
public const string AttributeNetPeerPort = "net.peer.port";
public const string AttributeUserAgent = "http.user_agent";
public const string AttributeExceptionType = "env_ex_type";
public const string AttributeExceptionMessage = "env_ex_msg";
public const string CustomPropertyHttpRequestMessage = "Tracing.CustomProperty.HttpRequestMessage";
public const string CustomPropertyHttpResponseMessage = "Tracing.CustomProperty.HttpResponseMessage";
public const string ActivityStartEvent = "OnStartActivity";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Diagnostics;
#if NETCOREAPP3_1_OR_GREATER
using System.Net.Http;
#else
using System.Net;
#endif
using Microsoft.Extensions.Compliance.Classification;
using Microsoft.Extensions.Http.Telemetry;
using Microsoft.Extensions.Http.Telemetry.Tracing.Internal;
Expand All @@ -15,10 +19,11 @@
using Microsoft.Extensions.Telemetry;
using Microsoft.Extensions.Telemetry.Internal;
using Microsoft.Shared.Diagnostics;
using OpenTelemetry;

namespace Microsoft.Extensions.Http.Telemetry.Tracing;

internal sealed class HttpClientRedactionProcessor
internal sealed class HttpClientRedactionProcessor : BaseProcessor<Activity>
{
private readonly ILogger<HttpClientRedactionProcessor> _logger;
private readonly IHttpPathRedactor _httpPathRedactor;
Expand Down Expand Up @@ -47,7 +52,36 @@ public HttpClientRedactionProcessor(
_logger.ConfiguredHttpClientTracingOptions(_options);
}

public void Process(Activity activity, HttpRequestMessage request)
public override void OnEnd(Activity activity)
{
var request = activity.GetRequest();

if (request != null)
{
activity.ClearRequest();
ProcessRequest(activity, request);
}
}

#pragma warning disable S3995 // URI return values should not be strings
private static string GetFormattedUrl(Uri requestUri, string path)
{
if (path.Length > 0 && path[0] == '/')
{
return $"{requestUri.Scheme}{Uri.SchemeDelimiter}{requestUri.Authority}{path}";
}
else
{
return $"{requestUri.Scheme}{Uri.SchemeDelimiter}{requestUri.Authority}/{path}";
}
}
#pragma warning restore S3995 // URI return values should not be strings

#if NETCOREAPP3_1_OR_GREATER
private void ProcessRequest(Activity activity, HttpRequestMessage request)
#else
private void ProcessRequest(Activity activity, HttpWebRequest request)
#endif
{
// Remove tags that shouldn't be exported as they may contain sensitive information.
_ = activity.SetTag(Constants.AttributeUserAgent, null);
Expand All @@ -70,7 +104,6 @@ public void Process(Activity activity, HttpRequestMessage request)
{
var path = request.RequestUri.AbsolutePath;
_ = activity.DisplayName = path;
_ = activity.SetTag(Constants.AttributeHttpRoute, path);
_ = activity.SetTag(Constants.AttributeHttpUrl, GetFormattedUrl(request.RequestUri, path));
return;
}
Expand All @@ -85,7 +118,6 @@ public void Process(Activity activity, HttpRequestMessage request)
_logger.RequestMetadataIsNotSetForTheRequest(request.RequestUri.AbsoluteUri);

_ = activity.DisplayName = TelemetryConstants.Unknown;
_ = activity.SetTag(Constants.AttributeHttpRoute, TelemetryConstants.Unknown);
_ = activity.SetTag(Constants.AttributeHttpUrl, GetFormattedUrl(request.RequestUri, TelemetryConstants.Unknown));
return;
}
Expand All @@ -94,7 +126,6 @@ public void Process(Activity activity, HttpRequestMessage request)
if (requestRoute == TelemetryConstants.Unknown)
{
_ = activity.DisplayName = requestMetadata.RequestName;
_ = activity.SetTag(Constants.AttributeHttpRoute, requestMetadata.RequestName);
_ = activity.SetTag(Constants.AttributeHttpUrl, GetFormattedUrl(request.RequestUri, requestMetadata.RequestName));
}
else
Expand All @@ -105,7 +136,7 @@ public void Process(Activity activity, HttpRequestMessage request)
if (routeParameterCount == 0)
{
// Route is either empty or has no parameters.
redactedUrl = _urlCache.GetOrAdd(requestRoute, (_) => GetFormattedUrl(request.RequestUri, redactedPath));
redactedUrl = _urlCache.GetOrAdd(requestRoute, _ => GetFormattedUrl(request.RequestUri, redactedPath));
}
else
{
Expand All @@ -115,22 +146,7 @@ public void Process(Activity activity, HttpRequestMessage request)
activity.DisplayName = requestMetadata.RequestName == TelemetryConstants.Unknown
? redactedPath : requestMetadata.RequestName;

_ = activity.SetTag(Constants.AttributeHttpRoute, requestRoute);
_ = activity.SetTag(Constants.AttributeHttpUrl, redactedUrl);
}
}

#pragma warning disable S3995 // URI return values should not be strings
private static string GetFormattedUrl(Uri requestUri, string path)
{
if (path.Length > 0 && path[0] == '/')
{
return $"{requestUri.Scheme}{Uri.SchemeDelimiter}{requestUri.Authority}{path}";
}
else
{
return $"{requestUri.Scheme}{Uri.SchemeDelimiter}{requestUri.Authority}/{path}";
}
}
#pragma warning restore S3995 // URI return values should not be strings
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using Microsoft.Extensions.Http.Telemetry.Tracing.Internal;
using OpenTelemetry;

namespace Microsoft.Extensions.Http.Telemetry.Tracing;

internal sealed class HttpClientTraceEnrichmentProcessor
internal sealed class HttpClientTraceEnrichmentProcessor : BaseProcessor<Activity>
{
private readonly IHttpClientTraceEnricher[] _traceEnrichers;

Expand All @@ -17,8 +18,18 @@ public HttpClientTraceEnrichmentProcessor(IEnumerable<IHttpClientTraceEnricher>
_traceEnrichers = traceEnrichers.ToArray();
}

public void Enrich(Activity activity, HttpRequestMessage request, HttpResponseMessage? response)
public override void OnEnd(Activity activity)
{
var request = activity.GetRequest();
var response = activity.GetResponse();

if (request is null && response is null)
{
return;
}

activity.ClearResponse();

foreach (var enricher in _traceEnrichers)
{
enricher.Enrich(activity, request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ private static TracerProviderBuilder AddHttpClientTracingInternal(this TracerPro
services.TryAddActivatedSingleton<HttpClientTraceEnrichmentProcessor>();
services.TryAddActivatedSingleton<HttpClientRedactionProcessor>();
})
.AddHttpClientInstrumentation();
.AddHttpClientInstrumentation()
.AddProcessor<HttpClientTraceEnrichmentProcessor>()
.AddProcessor<HttpClientRedactionProcessor>();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if NETCOREAPP3_1_OR_GREATER

using System.Diagnostics;
using System.Net.Http;

Expand All @@ -23,3 +25,5 @@ public interface IHttpClientTraceEnricher
/// </remarks>
void Enrich(Activity activity, HttpRequestMessage? request, HttpResponseMessage? response);
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NETCOREAPP3_1_OR_GREATER

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Net;

namespace Microsoft.Extensions.Http.Telemetry.Tracing;

/// <summary>
/// Interface for implementing enricher for enriching only traces for outgoing HTTP requests.
/// </summary>
public interface IHttpClientTraceEnricher
{
/// <summary>
/// Enrich trace with desired tags.
/// </summary>
/// <param name="activity"><see cref="Activity"/> object to be used to add the required tags to enrich the traces.</param>
/// <param name="webRequest"><see cref="HttpWebRequest"/> object associated with the outgoing request for the trace.</param>
/// <param name="webResponse"><see cref="HttpWebResponse"/> object associated with the outgoing request for the trace.</param>
/// <remarks>
/// If your enricher fetches some information from <see cref="HttpWebRequest"/> or <see cref="HttpWebResponse"/> to enrich HTTP traces, then make sure to check it for <see langword="null"/>.
/// </remarks>
[Experimental]
void Enrich(Activity activity, HttpWebRequest? webRequest, HttpWebResponse? webResponse);
}

#endif
Loading

0 comments on commit 3d9bb86

Please sign in to comment.