Skip to content

Commit

Permalink
[Instrumentations] Use IConfiguration for semantic conventions compat…
Browse files Browse the repository at this point in the history
…ibility check (#4627)
  • Loading branch information
vishweshbankwar authored Jul 6, 2023
1 parent 104cf32 commit 56fdb16
Show file tree
Hide file tree
Showing 29 changed files with 325 additions and 141 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageVersion Include="Microsoft.AspNetCore.Http.Features" Version="[2.1.1,6.0)" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="[3.3.3]" />
<PackageVersion Include="Microsoft.CodeCoverage" Version="[17.4.1]" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="[3.1.0,)" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="[3.1.0,)" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="[3.1.0,)" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="[2.1.0,)" />
Expand Down
9 changes: 1 addition & 8 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{A49299FB-C5CD-4E0E-B7E1-B7867BBD67CC}"
ProjectSection(SolutionItems) = preProject
src\Shared\ActivityInstrumentationHelper.cs = src\Shared\ActivityInstrumentationHelper.cs
src\Shared\HttpSemanticConventionHelper.cs = src\Shared\HttpSemanticConventionHelper.cs
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DiagnosticSourceInstrumentation", "DiagnosticSourceInstrumentation", "{28F3EC79-660C-4659-8B73-F90DC1173316}"
Expand Down Expand Up @@ -336,10 +337,6 @@ Global
{A38AC295-2745-4B85-8B6B-DCA864CEDD5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A38AC295-2745-4B85-8B6B-DCA864CEDD5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A38AC295-2745-4B85-8B6B-DCA864CEDD5B}.Release|Any CPU.Build.0 = Release|Any CPU
{56A34828-621A-478B-A0B8-C065FE938383}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{56A34828-621A-478B-A0B8-C065FE938383}.Debug|Any CPU.Build.0 = Debug|Any CPU
{56A34828-621A-478B-A0B8-C065FE938383}.Release|Any CPU.ActiveCfg = Release|Any CPU
{56A34828-621A-478B-A0B8-C065FE938383}.Release|Any CPU.Build.0 = Release|Any CPU
{1AFFF251-3B0C-47CA-BE94-937083732C0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1AFFF251-3B0C-47CA-BE94-937083732C0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AFFF251-3B0C-47CA-BE94-937083732C0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -356,10 +353,6 @@ Global
{305E9DFD-E73B-4A28-8769-795C25551020}.Debug|Any CPU.Build.0 = Debug|Any CPU
{305E9DFD-E73B-4A28-8769-795C25551020}.Release|Any CPU.ActiveCfg = Release|Any CPU
{305E9DFD-E73B-4A28-8769-795C25551020}.Release|Any CPU.Build.0 = Release|Any CPU
{98F9556B-116F-49B5-9211-BB1D418446FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{98F9556B-116F-49B5-9211-BB1D418446FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98F9556B-116F-49B5-9211-BB1D418446FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98F9556B-116F-49B5-9211-BB1D418446FF}.Release|Any CPU.Build.0 = Release|Any CPU
{FF3E6E08-E8E4-4523-B526-847CD989279F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF3E6E08-E8E4-4523-B526-847CD989279F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF3E6E08-E8E4-4523-B526-847CD989279F}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using static OpenTelemetry.Internal.HttpSemanticConventionHelper;

namespace OpenTelemetry.Instrumentation.AspNetCore
{
Expand All @@ -24,6 +26,23 @@ namespace OpenTelemetry.Instrumentation.AspNetCore
/// </summary>
public class AspNetCoreInstrumentationOptions
{
internal readonly HttpSemanticConvention HttpSemanticConvention;

/// <summary>
/// Initializes a new instance of the <see cref="AspNetCoreInstrumentationOptions"/> class.
/// </summary>
public AspNetCoreInstrumentationOptions()
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build())
{
}

internal AspNetCoreInstrumentationOptions(IConfiguration configuration)
{
Debug.Assert(configuration != null, "configuration was null");

this.HttpSemanticConvention = GetSemanticConventionOptIn(configuration);
}

/// <summary>
/// Gets or sets a filter function that determines whether or not to
/// collect telemetry on a per request basis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using static OpenTelemetry.Internal.HttpSemanticConventionHelper;

namespace OpenTelemetry.Instrumentation.AspNetCore
{
Expand All @@ -24,6 +26,23 @@ namespace OpenTelemetry.Instrumentation.AspNetCore
/// </summary>
public class AspNetCoreMetricsInstrumentationOptions
{
internal readonly HttpSemanticConvention HttpSemanticConvention;

/// <summary>
/// Initializes a new instance of the <see cref="AspNetCoreMetricsInstrumentationOptions"/> class.
/// </summary>
public AspNetCoreMetricsInstrumentationOptions()
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build())
{
}

internal AspNetCoreMetricsInstrumentationOptions(IConfiguration configuration)
{
Debug.Assert(configuration != null, "configuration was null");

this.HttpSemanticConvention = GetSemanticConventionOptIn(configuration);
}

/// <summary>
/// Delegate for enrichment of recorded metric with additional tags.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,13 @@ internal class HttpInListener : ListenerHandler
#endif
private readonly PropertyFetcher<Exception> stopExceptionFetcher = new("Exception");
private readonly AspNetCoreInstrumentationOptions options;
private readonly HttpSemanticConvention httpSemanticConvention;

public HttpInListener(AspNetCoreInstrumentationOptions options)
: base(DiagnosticSourceName)
{
Guard.ThrowIfNull(options);

this.options = options;

this.httpSemanticConvention = GetSemanticConventionOptIn();
}

public override void OnEventWritten(string name, object payload)
Expand Down Expand Up @@ -198,7 +195,7 @@ public void OnStartActivity(Activity activity, object payload)
activity.DisplayName = path;

// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md
if (this.httpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
{
if (request.Host.HasValue)
{
Expand Down Expand Up @@ -227,7 +224,7 @@ public void OnStartActivity(Activity activity, object payload)
}

// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.21.0/specification/trace/semantic_conventions/http.md
if (this.httpSemanticConvention.HasFlag(HttpSemanticConvention.New))
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
{
if (request.Host.HasValue)
{
Expand Down Expand Up @@ -284,12 +281,12 @@ public void OnStopActivity(Activity activity, object payload)

var response = context.Response;

if (this.httpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
{
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
}

if (this.httpSemanticConvention.HasFlag(HttpSemanticConvention.New))
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
{
activity.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
}
Expand Down Expand Up @@ -488,12 +485,12 @@ private void AddGrpcAttributes(Activity activity, string grpcMethod, HttpContext
activity.SetTag(SemanticConventions.AttributeNetPeerIp, context.Connection.RemoteIpAddress.ToString());
}

if (this.httpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
{
activity.SetTag(SemanticConventions.AttributeNetPeerPort, context.Connection.RemotePort);
}

if (this.httpSemanticConvention.HasFlag(HttpSemanticConvention.New))
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
{
activity.SetTag(SemanticConventions.AttributeServerPort, context.Connection.RemotePort);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,12 @@ internal sealed class HttpInMetricsListener : ListenerHandler
private readonly AspNetCoreMetricsInstrumentationOptions options;
private readonly Histogram<double> httpServerDuration;

private readonly HttpSemanticConvention httpSemanticConvention;

internal HttpInMetricsListener(string name, Meter meter, AspNetCoreMetricsInstrumentationOptions options)
: base(name)
{
this.meter = meter;
this.options = options;
this.httpServerDuration = meter.CreateHistogram<double>(HttpServerDurationMetricName, "ms", "Measures the duration of inbound HTTP requests.");

this.httpSemanticConvention = GetSemanticConventionOptIn();
}

public override void OnEventWritten(string name, object payload)
Expand Down Expand Up @@ -83,7 +79,7 @@ public override void OnEventWritten(string name, object payload)
TagList tags = default;

// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md
if (this.httpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
{
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocol(context.Request.Protocol)));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpScheme, context.Request.Scheme));
Expand All @@ -102,7 +98,7 @@ public override void OnEventWritten(string name, object payload)
}

// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.21.0/specification/trace/semantic_conventions/http.md
if (this.httpSemanticConvention.HasFlag(HttpSemanticConvention.New))
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
{
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeNetworkProtocolVersion, HttpTagHelper.GetFlavorTagValueFromProtocol(context.Request.Protocol)));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeUrlScheme, context.Request.Scheme));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ public static MeterProviderBuilder AddAspNetCoreInstrumentation(

name ??= Options.DefaultName;

if (configureAspNetCoreInstrumentationOptions != null)
builder.ConfigureServices(services =>
{
builder.ConfigureServices(services => services.Configure(name, configureAspNetCoreInstrumentationOptions));
}
if (configureAspNetCoreInstrumentationOptions != null)
{
services.Configure(name, configureAspNetCoreInstrumentationOptions);
}

services.RegisterOptionsFactory(configuration => new AspNetCoreMetricsInstrumentationOptions(configuration));
});

builder.AddMeter(AspNetCoreMetrics.InstrumentationName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcNetClient\GrpcTagHelper.cs" Link="Includes\GrpcTagHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcNetClient\StatusCanonicalCode.cs" Link="Includes\StatusCanonicalCode.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\HttpSemanticConventionHelper.cs" Link="Includes\HttpSemanticConventionHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\EnvironmentVariables\*.cs" Link="Includes\EnvironmentVariables\%(Filename).cs" Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netstandard2.1'"/>
<Compile Include="$(RepoRoot)\src\Shared\HttpSemanticConventionHelper.cs" Link="Includes\HttpSemanticConventionHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Options\*.cs" Link="Includes\Options\%(Filename).cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Shims\UnconditionalSuppressMessageAttribute.cs" Link="Includes\UnconditionalSuppressMessageAttribute.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ public static TracerProviderBuilder AddAspNetCoreInstrumentation(

name ??= Options.DefaultName;

if (configureAspNetCoreInstrumentationOptions != null)
builder.ConfigureServices(services =>
{
builder.ConfigureServices(services => services.Configure(name, configureAspNetCoreInstrumentationOptions));
}
if (configureAspNetCoreInstrumentationOptions != null)
{
services.Configure(name, configureAspNetCoreInstrumentationOptions);
}

services.RegisterOptionsFactory(configuration => new AspNetCoreInstrumentationOptions(configuration));
});

if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// </copyright>

using System.Diagnostics;
using Microsoft.Extensions.Configuration;
using static OpenTelemetry.Internal.HttpSemanticConventionHelper;

namespace OpenTelemetry.Instrumentation.GrpcNetClient
{
Expand All @@ -23,6 +25,23 @@ namespace OpenTelemetry.Instrumentation.GrpcNetClient
/// </summary>
public class GrpcClientInstrumentationOptions
{
internal readonly HttpSemanticConvention HttpSemanticConvention;

/// <summary>
/// Initializes a new instance of the <see cref="GrpcClientInstrumentationOptions"/> class.
/// </summary>
public GrpcClientInstrumentationOptions()
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build())
{
}

internal GrpcClientInstrumentationOptions(IConfiguration configuration)
{
Debug.Assert(configuration != null, "configuration was null");

this.HttpSemanticConvention = GetSemanticConventionOptIn(configuration);
}

/// <summary>
/// Gets or sets a value indicating whether down stream instrumentation is suppressed (disabled).
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.Http;
using OpenTelemetry.Trace;
using static OpenTelemetry.Internal.HttpSemanticConventionHelper;

namespace OpenTelemetry.Instrumentation.GrpcNetClient.Implementation
{
Expand All @@ -37,14 +36,10 @@ internal sealed class GrpcClientDiagnosticListener : ListenerHandler
private readonly PropertyFetcher<HttpRequestMessage> startRequestFetcher = new("Request");
private readonly PropertyFetcher<HttpResponseMessage> stopRequestFetcher = new("Response");

private readonly HttpSemanticConvention httpSemanticConvention;

public GrpcClientDiagnosticListener(GrpcClientInstrumentationOptions options)
: base("Grpc.Net.Client")
{
this.options = options;

this.httpSemanticConvention = GetSemanticConventionOptIn();
}

public override void OnEventWritten(string name, object payload)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.Http\HttpRequestMessageContextPropagation.cs" Link="Includes\HttpRequestMessageContextPropagation.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\HttpSemanticConventionHelper.cs" Link="Includes\HttpSemanticConventionHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\EnvironmentVariables\*.cs" />
<Compile Include="$(RepoRoot)\src\Shared\HttpSemanticConventionHelper.cs" Link="Includes\HttpSemanticConventionHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Options\*.cs" Link="Includes\Options\%(Filename).cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,15 @@ public static TracerProviderBuilder AddGrpcClientInstrumentation(

name ??= Options.DefaultName;

if (configure != null)
builder.ConfigureServices(services =>
{
builder.ConfigureServices(services => services.Configure(name, configure));
}
if (configure != null)
{
services.Configure(name, configure);
}

services.RegisterOptionsFactory(configuration => new GrpcClientInstrumentationOptions(configuration));
});

builder.AddSource(GrpcClientDiagnosticListener.ActivitySourceName);
builder.AddLegacySource("Grpc.Net.Client.GrpcOut");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
using System.Net.Http;
#endif
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Configuration;
using OpenTelemetry.Instrumentation.Http.Implementation;
using static OpenTelemetry.Internal.HttpSemanticConventionHelper;

namespace OpenTelemetry.Instrumentation.Http
{
Expand All @@ -29,6 +31,23 @@ namespace OpenTelemetry.Instrumentation.Http
/// </summary>
public class HttpClientInstrumentationOptions
{
internal readonly HttpSemanticConvention HttpSemanticConvention;

/// <summary>
/// Initializes a new instance of the <see cref="HttpClientInstrumentationOptions"/> class.
/// </summary>
public HttpClientInstrumentationOptions()
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build())
{
}

internal HttpClientInstrumentationOptions(IConfiguration configuration)
{
Debug.Assert(configuration != null, "configuration was null");

this.HttpSemanticConvention = GetSemanticConventionOptIn(configuration);
}

/// <summary>
/// Gets or sets a filter function that determines whether or not to
/// collect telemetry on a per request basis.
Expand Down
Loading

0 comments on commit 56fdb16

Please sign in to comment.