Skip to content

Commit 56fdb16

Browse files
[Instrumentations] Use IConfiguration for semantic conventions compatibility check (#4627)
1 parent 104cf32 commit 56fdb16

29 files changed

+325
-141
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<PackageVersion Include="Microsoft.AspNetCore.Http.Features" Version="[2.1.1,6.0)" />
1414
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="[3.3.3]" />
1515
<PackageVersion Include="Microsoft.CodeCoverage" Version="[17.4.1]" />
16+
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="[3.1.0,)" />
1617
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="[3.1.0,)" />
1718
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="[3.1.0,)" />
1819
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="[2.1.0,)" />

OpenTelemetry.sln

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ EndProject
267267
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{A49299FB-C5CD-4E0E-B7E1-B7867BBD67CC}"
268268
ProjectSection(SolutionItems) = preProject
269269
src\Shared\ActivityInstrumentationHelper.cs = src\Shared\ActivityInstrumentationHelper.cs
270+
src\Shared\HttpSemanticConventionHelper.cs = src\Shared\HttpSemanticConventionHelper.cs
270271
EndProjectSection
271272
EndProject
272273
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DiagnosticSourceInstrumentation", "DiagnosticSourceInstrumentation", "{28F3EC79-660C-4659-8B73-F90DC1173316}"
@@ -336,10 +337,6 @@ Global
336337
{A38AC295-2745-4B85-8B6B-DCA864CEDD5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
337338
{A38AC295-2745-4B85-8B6B-DCA864CEDD5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
338339
{A38AC295-2745-4B85-8B6B-DCA864CEDD5B}.Release|Any CPU.Build.0 = Release|Any CPU
339-
{56A34828-621A-478B-A0B8-C065FE938383}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
340-
{56A34828-621A-478B-A0B8-C065FE938383}.Debug|Any CPU.Build.0 = Debug|Any CPU
341-
{56A34828-621A-478B-A0B8-C065FE938383}.Release|Any CPU.ActiveCfg = Release|Any CPU
342-
{56A34828-621A-478B-A0B8-C065FE938383}.Release|Any CPU.Build.0 = Release|Any CPU
343340
{1AFFF251-3B0C-47CA-BE94-937083732C0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
344341
{1AFFF251-3B0C-47CA-BE94-937083732C0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
345342
{1AFFF251-3B0C-47CA-BE94-937083732C0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -356,10 +353,6 @@ Global
356353
{305E9DFD-E73B-4A28-8769-795C25551020}.Debug|Any CPU.Build.0 = Debug|Any CPU
357354
{305E9DFD-E73B-4A28-8769-795C25551020}.Release|Any CPU.ActiveCfg = Release|Any CPU
358355
{305E9DFD-E73B-4A28-8769-795C25551020}.Release|Any CPU.Build.0 = Release|Any CPU
359-
{98F9556B-116F-49B5-9211-BB1D418446FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
360-
{98F9556B-116F-49B5-9211-BB1D418446FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
361-
{98F9556B-116F-49B5-9211-BB1D418446FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
362-
{98F9556B-116F-49B5-9211-BB1D418446FF}.Release|Any CPU.Build.0 = Release|Any CPU
363356
{FF3E6E08-E8E4-4523-B526-847CD989279F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
364357
{FF3E6E08-E8E4-4523-B526-847CD989279F}.Debug|Any CPU.Build.0 = Debug|Any CPU
365358
{FF3E6E08-E8E4-4523-B526-847CD989279F}.Release|Any CPU.ActiveCfg = Release|Any CPU

src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationOptions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
using System.Diagnostics;
1818
using Microsoft.AspNetCore.Http;
19+
using Microsoft.Extensions.Configuration;
20+
using static OpenTelemetry.Internal.HttpSemanticConventionHelper;
1921

2022
namespace OpenTelemetry.Instrumentation.AspNetCore
2123
{
@@ -24,6 +26,23 @@ namespace OpenTelemetry.Instrumentation.AspNetCore
2426
/// </summary>
2527
public class AspNetCoreInstrumentationOptions
2628
{
29+
internal readonly HttpSemanticConvention HttpSemanticConvention;
30+
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="AspNetCoreInstrumentationOptions"/> class.
33+
/// </summary>
34+
public AspNetCoreInstrumentationOptions()
35+
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build())
36+
{
37+
}
38+
39+
internal AspNetCoreInstrumentationOptions(IConfiguration configuration)
40+
{
41+
Debug.Assert(configuration != null, "configuration was null");
42+
43+
this.HttpSemanticConvention = GetSemanticConventionOptIn(configuration);
44+
}
45+
2746
/// <summary>
2847
/// Gets or sets a filter function that determines whether or not to
2948
/// collect telemetry on a per request basis.

src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreMetricsInstrumentationOptions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
using System.Diagnostics;
1818
using Microsoft.AspNetCore.Http;
19+
using Microsoft.Extensions.Configuration;
20+
using static OpenTelemetry.Internal.HttpSemanticConventionHelper;
1921

2022
namespace OpenTelemetry.Instrumentation.AspNetCore
2123
{
@@ -24,6 +26,23 @@ namespace OpenTelemetry.Instrumentation.AspNetCore
2426
/// </summary>
2527
public class AspNetCoreMetricsInstrumentationOptions
2628
{
29+
internal readonly HttpSemanticConvention HttpSemanticConvention;
30+
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="AspNetCoreMetricsInstrumentationOptions"/> class.
33+
/// </summary>
34+
public AspNetCoreMetricsInstrumentationOptions()
35+
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build())
36+
{
37+
}
38+
39+
internal AspNetCoreMetricsInstrumentationOptions(IConfiguration configuration)
40+
{
41+
Debug.Assert(configuration != null, "configuration was null");
42+
43+
this.HttpSemanticConvention = GetSemanticConventionOptIn(configuration);
44+
}
45+
2746
/// <summary>
2847
/// Delegate for enrichment of recorded metric with additional tags.
2948
/// </summary>

src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,13 @@ internal class HttpInListener : ListenerHandler
6363
#endif
6464
private readonly PropertyFetcher<Exception> stopExceptionFetcher = new("Exception");
6565
private readonly AspNetCoreInstrumentationOptions options;
66-
private readonly HttpSemanticConvention httpSemanticConvention;
6766

6867
public HttpInListener(AspNetCoreInstrumentationOptions options)
6968
: base(DiagnosticSourceName)
7069
{
7170
Guard.ThrowIfNull(options);
7271

7372
this.options = options;
74-
75-
this.httpSemanticConvention = GetSemanticConventionOptIn();
7673
}
7774

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

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

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

285282
var response = context.Response;
286283

287-
if (this.httpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
284+
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
288285
{
289286
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
290287
}
291288

292-
if (this.httpSemanticConvention.HasFlag(HttpSemanticConvention.New))
289+
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
293290
{
294291
activity.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
295292
}
@@ -488,12 +485,12 @@ private void AddGrpcAttributes(Activity activity, string grpcMethod, HttpContext
488485
activity.SetTag(SemanticConventions.AttributeNetPeerIp, context.Connection.RemoteIpAddress.ToString());
489486
}
490487

491-
if (this.httpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
488+
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
492489
{
493490
activity.SetTag(SemanticConventions.AttributeNetPeerPort, context.Connection.RemotePort);
494491
}
495492

496-
if (this.httpSemanticConvention.HasFlag(HttpSemanticConvention.New))
493+
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
497494
{
498495
activity.SetTag(SemanticConventions.AttributeServerPort, context.Connection.RemotePort);
499496
}

src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,12 @@ internal sealed class HttpInMetricsListener : ListenerHandler
3535
private readonly AspNetCoreMetricsInstrumentationOptions options;
3636
private readonly Histogram<double> httpServerDuration;
3737

38-
private readonly HttpSemanticConvention httpSemanticConvention;
39-
4038
internal HttpInMetricsListener(string name, Meter meter, AspNetCoreMetricsInstrumentationOptions options)
4139
: base(name)
4240
{
4341
this.meter = meter;
4442
this.options = options;
4543
this.httpServerDuration = meter.CreateHistogram<double>(HttpServerDurationMetricName, "ms", "Measures the duration of inbound HTTP requests.");
46-
47-
this.httpSemanticConvention = GetSemanticConventionOptIn();
4844
}
4945

5046
public override void OnEventWritten(string name, object payload)
@@ -83,7 +79,7 @@ public override void OnEventWritten(string name, object payload)
8379
TagList tags = default;
8480

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

104100
// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.21.0/specification/trace/semantic_conventions/http.md
105-
if (this.httpSemanticConvention.HasFlag(HttpSemanticConvention.New))
101+
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
106102
{
107103
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeNetworkProtocolVersion, HttpTagHelper.GetFlavorTagValueFromProtocol(context.Request.Protocol)));
108104
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeUrlScheme, context.Request.Scheme));

src/OpenTelemetry.Instrumentation.AspNetCore/MeterProviderBuilderExtensions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,15 @@ public static MeterProviderBuilder AddAspNetCoreInstrumentation(
6666

6767
name ??= Options.DefaultName;
6868

69-
if (configureAspNetCoreInstrumentationOptions != null)
69+
builder.ConfigureServices(services =>
7070
{
71-
builder.ConfigureServices(services => services.Configure(name, configureAspNetCoreInstrumentationOptions));
72-
}
71+
if (configureAspNetCoreInstrumentationOptions != null)
72+
{
73+
services.Configure(name, configureAspNetCoreInstrumentationOptions);
74+
}
75+
76+
services.RegisterOptionsFactory(configuration => new AspNetCoreMetricsInstrumentationOptions(configuration));
77+
});
7378

7479
builder.AddMeter(AspNetCoreMetrics.InstrumentationName);
7580

src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcNetClient\GrpcTagHelper.cs" Link="Includes\GrpcTagHelper.cs" />
1515
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcNetClient\StatusCanonicalCode.cs" Link="Includes\StatusCanonicalCode.cs" />
1616
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
17-
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\HttpSemanticConventionHelper.cs" Link="Includes\HttpSemanticConventionHelper.cs" />
17+
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\EnvironmentVariables\*.cs" Link="Includes\EnvironmentVariables\%(Filename).cs" Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netstandard2.1'"/>
18+
<Compile Include="$(RepoRoot)\src\Shared\HttpSemanticConventionHelper.cs" Link="Includes\HttpSemanticConventionHelper.cs" />
19+
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Options\*.cs" Link="Includes\Options\%(Filename).cs" />
1820
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\Shims\UnconditionalSuppressMessageAttribute.cs" Link="Includes\UnconditionalSuppressMessageAttribute.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
1921
</ItemGroup>
2022

src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ public static TracerProviderBuilder AddAspNetCoreInstrumentation(
6868

6969
name ??= Options.DefaultName;
7070

71-
if (configureAspNetCoreInstrumentationOptions != null)
71+
builder.ConfigureServices(services =>
7272
{
73-
builder.ConfigureServices(services => services.Configure(name, configureAspNetCoreInstrumentationOptions));
74-
}
73+
if (configureAspNetCoreInstrumentationOptions != null)
74+
{
75+
services.Configure(name, configureAspNetCoreInstrumentationOptions);
76+
}
77+
78+
services.RegisterOptionsFactory(configuration => new AspNetCoreInstrumentationOptions(configuration));
79+
});
7580

7681
if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder)
7782
{

src/OpenTelemetry.Instrumentation.GrpcNetClient/GrpcClientInstrumentationOptions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// </copyright>
1616

1717
using System.Diagnostics;
18+
using Microsoft.Extensions.Configuration;
19+
using static OpenTelemetry.Internal.HttpSemanticConventionHelper;
1820

1921
namespace OpenTelemetry.Instrumentation.GrpcNetClient
2022
{
@@ -23,6 +25,23 @@ namespace OpenTelemetry.Instrumentation.GrpcNetClient
2325
/// </summary>
2426
public class GrpcClientInstrumentationOptions
2527
{
28+
internal readonly HttpSemanticConvention HttpSemanticConvention;
29+
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="GrpcClientInstrumentationOptions"/> class.
32+
/// </summary>
33+
public GrpcClientInstrumentationOptions()
34+
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build())
35+
{
36+
}
37+
38+
internal GrpcClientInstrumentationOptions(IConfiguration configuration)
39+
{
40+
Debug.Assert(configuration != null, "configuration was null");
41+
42+
this.HttpSemanticConvention = GetSemanticConventionOptIn(configuration);
43+
}
44+
2645
/// <summary>
2746
/// Gets or sets a value indicating whether down stream instrumentation is suppressed (disabled).
2847
/// </summary>

0 commit comments

Comments
 (0)