Skip to content

Commit

Permalink
Merge branch 'main' into yunl/OtlpLoggingExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
Yun-Ting committed Dec 13, 2023
2 parents d09f1b0 + ecaa601 commit fa2857a
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 129 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -47,7 +47,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
uses: github/codeql-action/autobuild@v3

# Command-line programs to run using the OS shell.
# https://git.io/JvXDl
Expand All @@ -61,4 +61,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions.F
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions.Filter.set -> void
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions.RecordException.get -> bool
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions.RecordException.set -> void
OpenTelemetry.Metrics.MeterProviderBuilderExtensions
OpenTelemetry.Trace.TracerProviderBuilderExtensions
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action<OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions> configureAspNetCoreTraceInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions> configureAspNetCoreTraceInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
OpenTelemetry.Metrics.AspNetCoreInstrumentationMeterProviderBuilderExtensions
OpenTelemetry.Trace.AspNetCoreInstrumentationTracerProviderBuilderExtensions
static OpenTelemetry.Metrics.AspNetCoreInstrumentationMeterProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Trace.AspNetCoreInstrumentationTracerProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.AspNetCoreInstrumentationTracerProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action<OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions> configureAspNetCoreTraceInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.AspNetCoreInstrumentationTracerProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions> configureAspNetCoreTraceInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace OpenTelemetry.Metrics;
/// <summary>
/// Extension methods to simplify registering of ASP.NET Core request instrumentation.
/// </summary>
public static class MeterProviderBuilderExtensions
public static class AspNetCoreInstrumentationMeterProviderBuilderExtensions
{
/// <summary>
/// Enables the incoming requests automatic data collection for ASP.NET Core.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace OpenTelemetry.Trace;
/// <summary>
/// Extension methods to simplify registering of ASP.NET Core request instrumentation.
/// </summary>
public static class TracerProviderBuilderExtensions
public static class AspNetCoreInstrumentationTracerProviderBuilderExtensions
{
/// <summary>
/// Enables the incoming requests automatic data collection for ASP.NET Core.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,12 @@ internal HttpInMetricsListener(string name)
{
}

public override void OnEventWritten(string name, object payload)
{
switch (name)
{
case OnUnhandledDiagnosticsExceptionEvent:
case OnUnhandledHostingExceptionEvent:
{
this.OnExceptionEventWritten(name, payload);
}

break;
case OnStopEvent:
{
this.OnStopEventWritten(name, payload);
}

break;
}
}

public void OnExceptionEventWritten(string name, object payload)
public static void OnExceptionEventWritten(string name, object payload)
{
// We need to use reflection here as the payload type is not a defined public type.
if (!TryFetchException(payload, out Exception exc) || !TryFetchHttpContext(payload, out HttpContext ctx))
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInMetricsListener), nameof(this.OnExceptionEventWritten), HttpServerRequestDurationMetricName);
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInMetricsListener), nameof(OnExceptionEventWritten), HttpServerRequestDurationMetricName);
return;
}

Expand All @@ -88,12 +68,12 @@ static bool TryFetchHttpContext(object payload, out HttpContext ctx)
=> HttpContextPropertyFetcher.TryFetch(payload, out ctx) && ctx != null;
}

public void OnStopEventWritten(string name, object payload)
public static void OnStopEventWritten(string name, object payload)
{
var context = payload as HttpContext;
if (context == null)
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInMetricsListener), EventName, HttpServerRequestDurationMetricName);
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInMetricsListener), nameof(OnStopEventWritten), HttpServerRequestDurationMetricName);
return;
}

Expand Down Expand Up @@ -124,4 +104,24 @@ public void OnStopEventWritten(string name, object payload)
// TODO: Follow up with .NET team if we can continue to rely on this behavior.
HttpServerRequestDuration.Record(Activity.Current.Duration.TotalSeconds, tags);
}

public override void OnEventWritten(string name, object payload)
{
switch (name)
{
case OnUnhandledDiagnosticsExceptionEvent:
case OnUnhandledHostingExceptionEvent:
{
OnExceptionEventWritten(name, payload);
}

break;
case OnStopEvent:
{
OnStopEventWritten(name, payload);
}

break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions.FilterH
OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions.HttpClientTraceInstrumentationOptions() -> void
OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions.RecordException.get -> bool
OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions.RecordException.set -> void
OpenTelemetry.Metrics.MeterProviderBuilderExtensions
OpenTelemetry.Trace.TracerProviderBuilderExtensions
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action<OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions> configureHttpClientTraceInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions> configureHttpClientTraceInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
OpenTelemetry.Metrics.HttpClientInstrumentationMeterProviderBuilderExtensions
OpenTelemetry.Trace.HttpClientInstrumentationTracerProviderBuilderExtensions
static OpenTelemetry.Metrics.HttpClientInstrumentationMeterProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Trace.HttpClientInstrumentationTracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.HttpClientInstrumentationTracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action<OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions> configureHttpClientTraceInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.HttpClientInstrumentationTracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions> configureHttpClientTraceInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace OpenTelemetry.Metrics;
/// <summary>
/// Extension methods to simplify registering of HttpClient instrumentation.
/// </summary>
public static class MeterProviderBuilderExtensions
public static class HttpClientInstrumentationMeterProviderBuilderExtensions
{
/// <summary>
/// Enables HttpClient instrumentation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace OpenTelemetry.Trace;
/// <summary>
/// Extension methods to simplify registering of HttpClient instrumentation.
/// </summary>
public static class TracerProviderBuilderExtensions
public static class HttpClientInstrumentationTracerProviderBuilderExtensions
{
/// <summary>
/// Enables HttpClient instrumentation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ namespace OpenTelemetry.Instrumentation.Http;

internal static class HttpRequestMessageContextPropagation
{
internal static Func<HttpRequestMessage, string, IEnumerable<string>> HeaderValuesGetter => (request, name) =>
{
if (request.Headers.TryGetValues(name, out var values))
{
return values;
}

return null;
};

internal static Action<HttpRequestMessage, string, string> HeaderValueSetter => (request, name, value) =>
{
request.Headers.Remove(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,23 @@ internal sealed class HttpHandlerMetricsDiagnosticListener : ListenerHandler
private static readonly PropertyFetcher<Exception> StopExceptionFetcher = new("Exception");
private static readonly PropertyFetcher<HttpRequestMessage> RequestFetcher = new("Request");
#if NET6_0_OR_GREATER
private static readonly HttpRequestOptionsKey<string> HttpRequestOptionsErrorKey = new HttpRequestOptionsKey<string>(SemanticConventions.AttributeErrorType);
private static readonly HttpRequestOptionsKey<string> HttpRequestOptionsErrorKey = new(SemanticConventions.AttributeErrorType);
#endif

public HttpHandlerMetricsDiagnosticListener(string name)
: base(name)
{
}

public override void OnEventWritten(string name, object payload)
{
if (name == OnUnhandledExceptionEvent)
{
this.OnExceptionEventWritten(Activity.Current, payload);
}
else if (name == OnStopEvent)
{
this.OnStopEventWritten(Activity.Current, payload);
}
}

public void OnStopEventWritten(Activity activity, object payload)
public static void OnStopEventWritten(Activity activity, object payload)
{
if (TryFetchRequest(payload, out HttpRequestMessage request))
{
// see the spec https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-metrics.md
TagList tags = default;

if (RequestMethodHelper.KnownMethods.TryGetValue(request.Method.Method, out var httpMethod))
{
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpRequestMethod, httpMethod));
}
else
{
// Set to default "_OTHER" as per spec.
// https://github.com/open-telemetry/semantic-conventions/blob/v1.22.0/docs/http/http-spans.md#common-attributes
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpRequestMethod, "_OTHER"));
}
var httpMethod = RequestMethodHelper.GetNormalizedHttpMethod(request.Method.Method);
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpRequestMethod, httpMethod));

tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeServerAddress, request.RequestUri.Host));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeUrlScheme, request.RequestUri.Scheme));
Expand Down Expand Up @@ -129,11 +109,11 @@ static bool TryFetchResponse(object payload, out HttpResponseMessage response) =
StopResponseFetcher.TryFetch(payload, out response) && response != null;
}

public void OnExceptionEventWritten(Activity activity, object payload)
public static void OnExceptionEventWritten(Activity activity, object payload)
{
if (!TryFetchException(payload, out Exception exc) || !TryFetchRequest(payload, out HttpRequestMessage request))
{
HttpInstrumentationEventSource.Log.NullPayload(nameof(HttpHandlerMetricsDiagnosticListener), nameof(this.OnExceptionEventWritten));
HttpInstrumentationEventSource.Log.NullPayload(nameof(HttpHandlerMetricsDiagnosticListener), nameof(OnExceptionEventWritten));
return;
}

Expand Down Expand Up @@ -173,4 +153,16 @@ static bool TryFetchRequest(object payload, out HttpRequestMessage request)
return true;
}
}

public override void OnEventWritten(string name, object payload)
{
if (name == OnStopEvent)
{
OnStopEventWritten(Activity.Current, payload);
}
else if (name == OnUnhandledExceptionEvent)
{
OnExceptionEventWritten(Activity.Current, payload);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,13 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Collections.Concurrent;
#if NETFRAMEWORK
using System.Net.Http;
#endif

namespace OpenTelemetry.Instrumentation.Http.Implementation;

/// <summary>
/// A collection of helper methods to be used when building Http activities.
/// </summary>
internal static class HttpTagHelper
{
private static readonly ConcurrentDictionary<string, string> MethodOperationNameCache = new();
private static readonly ConcurrentDictionary<HttpMethod, string> HttpMethodOperationNameCache = new();
private static readonly ConcurrentDictionary<HttpMethod, string> HttpMethodNameCache = new();

private static readonly Func<string, string> ConvertMethodToOperationNameRef = ConvertMethodToOperationName;
private static readonly Func<HttpMethod, string> ConvertHttpMethodToOperationNameRef = ConvertHttpMethodToOperationName;
private static readonly Func<HttpMethod, string> ConvertHttpMethodToNameRef = ConvertHttpMethodToName;

/// <summary>
/// Gets the OpenTelemetry standard name for an activity based on its Http method.
/// </summary>
/// <param name="method">Http method.</param>
/// <returns>Activity name.</returns>
public static string GetOperationNameForHttpMethod(string method) => MethodOperationNameCache.GetOrAdd(method, ConvertMethodToOperationNameRef);

/// <summary>
/// Gets the OpenTelemetry standard operation name for a span based on its <see cref="HttpMethod"/>.
/// </summary>
/// <param name="method"><see cref="HttpMethod"/>.</param>
/// <returns>Span operation name.</returns>
public static string GetOperationNameForHttpMethod(HttpMethod method) => HttpMethodOperationNameCache.GetOrAdd(method, ConvertHttpMethodToOperationNameRef);

/// <summary>
/// Gets the OpenTelemetry standard method name for a span based on its <see cref="HttpMethod"/>.
/// </summary>
/// <param name="method"><see cref="HttpMethod"/>.</param>
/// <returns>Span method name.</returns>
public static string GetNameForHttpMethod(HttpMethod method) => HttpMethodNameCache.GetOrAdd(method, ConvertHttpMethodToNameRef);

/// <summary>
/// Gets the OpenTelemetry standard uri tag value for a span based on its request <see cref="Uri"/>.
/// </summary>
Expand All @@ -65,10 +31,4 @@ public static string GetUriTagValueFromRequestUri(Uri uri)
(3, 0) => "3",
_ => httpVersion.ToString(),
};

private static string ConvertMethodToOperationName(string method) => $"HTTP {method}";

private static string ConvertHttpMethodToOperationName(HttpMethod method) => $"HTTP {method}";

private static string ConvertHttpMethodToName(HttpMethod method) => method.ToString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -425,16 +425,8 @@ private static void ProcessResult(IAsyncResult asyncResult, AsyncCallback asyncC

TagList tags = default;

if (RequestMethodHelper.KnownMethods.TryGetValue(request.Method, out var httpMethod))
{
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpRequestMethod, httpMethod));
}
else
{
// Set to default "_OTHER" as per spec.
// https://github.com/open-telemetry/semantic-conventions/blob/v1.22.0/docs/http/http-spans.md#common-attributes
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpRequestMethod, "_OTHER"));
}
var httpMethod = RequestMethodHelper.GetNormalizedHttpMethod(request.Method);
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpRequestMethod, httpMethod));

tags.Add(SemanticConventions.AttributeServerAddress, request.RequestUri.Host);
tags.Add(SemanticConventions.AttributeUrlScheme, request.RequestUri.Scheme);
Expand Down

0 comments on commit fa2857a

Please sign in to comment.