Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable enrich for HttpWebRequest #1407

Merged
merged 5 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

* Instrumentation for `HttpWebRequest` no longer store raw objects like
`HttpWebRequest` in Activity.CustomProperty. To enrich activity, use the
Enrich action on the instrumentation.
([#1261](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1407))

## 0.7.0-beta.1

Released 2020-Oct-16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#if NETFRAMEWORK
using System;
using System.Diagnostics;
using System.Net;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.Http.Implementation;
Expand Down Expand Up @@ -50,6 +51,17 @@ public class HttpWebRequestInstrumentationOptions
/// </summary>
public Func<HttpWebRequest, bool> Filter { get; set; }

/// <summary>
/// Gets or sets an action to enrich an Activity.
/// </summary>
/// <remarks>
/// <para><see cref="Activity"/>: the activity being enriched.</para>
/// <para>string: the name of the event.</para>
/// <para>object: the raw object from which additional information can be extracted to enrich the activity.
/// The type of this object depends on the event, which is given by the above parameter.</para>
/// </remarks>
public Action<Activity, string, object> Enrich { get; set; }

internal bool EventFilter(HttpWebRequest request)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Text;
using OpenTelemetry.Context;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;

Expand All @@ -41,10 +39,6 @@ internal static class HttpWebRequestActivitySource
public const string ActivitySourceName = "OpenTelemetry.HttpWebRequest";
public const string ActivityName = ActivitySourceName + ".HttpRequestOut";

public const string RequestCustomPropertyName = "OTel.HttpWebRequest.Request";
public const string ResponseCustomPropertyName = "OTel.HttpWebRequest.Response";
public const string ExceptionCustomPropertyName = "OTel.HttpWebRequest.Exception";

internal static readonly Func<HttpWebRequest, string, IEnumerable<string>> HttpWebRequestHeaderValuesGetter = (request, name) => request.Headers.GetValues(name);
internal static readonly Action<HttpWebRequest, string, string> HttpWebRequestHeaderValuesSetter = (request, name, value) => request.Headers.Add(name, value);

Expand Down Expand Up @@ -104,7 +98,15 @@ private static void AddRequestTagsAndInstrumentRequest(HttpWebRequest request, A

if (activity.IsAllDataRequested)
{
activity.SetCustomProperty(RequestCustomPropertyName, request);
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);
Expand All @@ -120,7 +122,15 @@ private static void AddResponseTags(HttpWebResponse response, Activity activity)
{
if (activity.IsAllDataRequested)
{
activity.SetCustomProperty(ResponseCustomPropertyName, response);
try
{
Options.Enrich?.Invoke(activity, "OnStopActivity", response);
}
catch (Exception ex)
{
HttpInstrumentationEventSource.Log.EnrichmentException(ex);
}

activity.SetTag(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode);

activity.SetStatus(
Expand All @@ -138,7 +148,14 @@ private static void AddExceptionTags(Exception exception, Activity activity)
return;
}

activity.SetCustomProperty(ExceptionCustomPropertyName, exception);
try
{
Options.Enrich?.Invoke(activity, "OnException", exception);
}
catch (Exception ex)
{
HttpInstrumentationEventSource.Log.EnrichmentException(ex);
}

Status status;
if (exception is WebException wexc)
Expand Down
Loading