From 723ad0bd654dfb9817efd93d7af65d4b9302eed3 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Thu, 8 Dec 2022 17:03:05 -0800 Subject: [PATCH 1/2] [AspNetCore Instrumentation] AspNetCoreMetricsInstrumentationOptions follow-ups & nits (#3982) * Updates to take care of nits. * Public API fixes. * CHANGELOG patch. --- .../.publicApi/net6.0/PublicAPI.Unshipped.txt | 2 +- .../.publicApi/net7.0/PublicAPI.Unshipped.txt | 2 +- .../netcoreapp3.1/PublicAPI.Shipped.txt | 0 .../netcoreapp3.1/PublicAPI.Unshipped.txt | 23 ----------------- .../netstandard2.0/PublicAPI.Unshipped.txt | 2 +- .../netstandard2.1/PublicAPI.Unshipped.txt | 2 +- .../AspNetCoreInstrumentationOptions.cs | 18 ++++++++++--- ...AspNetCoreMetricsInstrumentationOptions.cs | 25 ++++++++++++++----- .../CHANGELOG.md | 8 +++--- .../AspNetCoreInstrumentationEventSource.cs | 2 +- .../Implementation/HttpInMetricsListener.cs | 9 ++++--- .../HttpClientInstrumentationOptions.cs | 4 +-- .../MetricTests.cs | 4 +-- 13 files changed, 52 insertions(+), 49 deletions(-) delete mode 100644 src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netcoreapp3.1/PublicAPI.Shipped.txt delete mode 100644 src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netcoreapp3.1/PublicAPI.Unshipped.txt diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/net6.0/PublicAPI.Unshipped.txt index 78aac3791b2..b65b99a84c4 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -17,7 +17,7 @@ OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricsInstrumentationOptions() -> void OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Enrich.get -> OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricEnrichmentFunc OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Enrich.set -> void -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.get -> System.Func +OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.get -> System.Func OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.set -> void OpenTelemetry.Metrics.MeterProviderBuilderExtensions OpenTelemetry.Trace.TracerProviderBuilderExtensions diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/net7.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/net7.0/PublicAPI.Unshipped.txt index 78aac3791b2..b65b99a84c4 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/net7.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/net7.0/PublicAPI.Unshipped.txt @@ -17,7 +17,7 @@ OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricsInstrumentationOptions() -> void OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Enrich.get -> OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricEnrichmentFunc OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Enrich.set -> void -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.get -> System.Func +OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.get -> System.Func OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.set -> void OpenTelemetry.Metrics.MeterProviderBuilderExtensions OpenTelemetry.Trace.TracerProviderBuilderExtensions diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netcoreapp3.1/PublicAPI.Shipped.txt b/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netcoreapp3.1/PublicAPI.Shipped.txt deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netcoreapp3.1/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netcoreapp3.1/PublicAPI.Unshipped.txt deleted file mode 100644 index 13189c0c9d4..00000000000 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netcoreapp3.1/PublicAPI.Unshipped.txt +++ /dev/null @@ -1,23 +0,0 @@ -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.AspNetCoreInstrumentationOptions() -> void -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.EnableGrpcAspNetCoreSupport.get -> bool -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.EnableGrpcAspNetCoreSupport.set -> void -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.Enrich.get -> System.Action -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.Enrich.set -> void -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.Filter.get -> System.Func -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.Filter.set -> void -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.RecordException.get -> bool -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.RecordException.set -> void -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricEnrichmentFunc -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricsInstrumentationOptions() -> void -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Enrich.get -> OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricEnrichmentFunc -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Enrich.set -> void -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.get -> System.Func -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.set -> void -OpenTelemetry.Metrics.MeterProviderBuilderExtensions -OpenTelemetry.Trace.TracerProviderBuilderExtensions -static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configureAspNetCoreInstrumentationOptions) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configureAspNetCoreInstrumentationOptions) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action configureAspNetCoreInstrumentationOptions = null) -> OpenTelemetry.Trace.TracerProviderBuilder diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index a806506f1d4..68b0a198212 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -15,7 +15,7 @@ OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricsInstrumentationOptions() -> void OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Enrich.get -> OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricEnrichmentFunc OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Enrich.set -> void -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.get -> System.Func +OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.get -> System.Func OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.set -> void OpenTelemetry.Metrics.MeterProviderBuilderExtensions OpenTelemetry.Trace.TracerProviderBuilderExtensions diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt index 78aac3791b2..b65b99a84c4 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt @@ -17,7 +17,7 @@ OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricsInstrumentationOptions() -> void OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Enrich.get -> OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricEnrichmentFunc OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Enrich.set -> void -OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.get -> System.Func +OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.get -> System.Func OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.set -> void OpenTelemetry.Metrics.MeterProviderBuilderExtensions OpenTelemetry.Trace.TracerProviderBuilderExtensions diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationOptions.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationOptions.cs index f134acb0d88..283126e488a 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationOptions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationOptions.cs @@ -25,11 +25,21 @@ namespace OpenTelemetry.Instrumentation.AspNetCore public class AspNetCoreInstrumentationOptions { /// - /// Gets or sets a Filter function that determines whether or not to collect telemetry about requests on a per request basis. - /// The Filter gets the HttpContext, and should return a boolean. - /// If Filter returns true, the request is collected. - /// If Filter returns false or throw exception, the request is filtered out. + /// Gets or sets a filter function that determines whether or not to + /// collect telemetry on a per request basis. /// + /// + /// Notes: + /// + /// The return value for the filter function is interpreted as: + /// + /// If filter returns , the request is + /// collected. + /// If filter returns or throws an + /// exception the request is NOT collected. + /// + /// + /// public Func Filter { get; set; } /// diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreMetricsInstrumentationOptions.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreMetricsInstrumentationOptions.cs index a7819d55f32..23abdeec670 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreMetricsInstrumentationOptions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreMetricsInstrumentationOptions.cs @@ -27,17 +27,30 @@ public class AspNetCoreMetricsInstrumentationOptions /// /// Delegate for enrichment of recorded metric with additional tags. /// + /// The name of the metric being enriched. /// : the HttpContext object. Both Request and Response are available. /// : List of current tags. You can add additional tags to this list. - public delegate void AspNetCoreMetricEnrichmentFunc(HttpContext context, ref TagList tags); + public delegate void AspNetCoreMetricEnrichmentFunc(string name, HttpContext context, ref TagList tags); /// - /// Gets or sets a Filter function that determines whether or not to collect telemetry about requests on a per request basis. - /// The Filter gets the HttpContext, and should return a boolean. - /// If Filter returns true, the request is collected. - /// If Filter returns false or throw exception, the request is filtered out. + /// Gets or sets a filter function that determines whether or not to + /// collect telemetry on a per request basis. /// - public Func Filter { get; set; } + /// + /// Notes: + /// + /// The first parameter is the name of the metric being + /// filtered. + /// The return value for the filter function is interpreted as: + /// + /// If filter returns , the request is + /// collected. + /// If filter returns or throws an + /// exception the request is NOT collected. + /// + /// + /// + public Func Filter { get; set; } /// /// Gets or sets an function to enrich a recorded metric with additional custom tags. diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md index 2b076f79716..39f2da81bd9 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md @@ -18,10 +18,12 @@ * `http.host` will no longer be populated on activity. `net.host.name` and `net.host.port` attributes will be populated instead. ([#3858](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3858)) - * Extension method `AddAspNetCoreInstrumentation` on `MeterProviderBuilder` now + +* Extension method `AddAspNetCoreInstrumentation` on `MeterProviderBuilder` now supports `AspNetCoreMetricsInstrumentationOptions`. This option class exposes - configuration properties for metric filtering and tag enrichment. - ([#3948](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3948)) + configuration properties for metric filtering and tag enrichment. + ([#3948](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3948), + [#3982](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3982)) ## 1.0.0-rc9.9 diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/AspNetCoreInstrumentationEventSource.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/AspNetCoreInstrumentationEventSource.cs index d830208923d..cd5b1802119 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/AspNetCoreInstrumentationEventSource.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/AspNetCoreInstrumentationEventSource.cs @@ -42,7 +42,7 @@ public void NullPayload(string handlerName, string eventName) this.WriteEvent(1, handlerName, eventName); } - [Event(2, Message = "Request is filtered out.", Level = EventLevel.Verbose)] + [Event(2, Message = "Request is filtered out. EventName: '{0}'.", Level = EventLevel.Verbose)] public void RequestIsFilteredOut(string eventName) { this.WriteEvent(2, eventName); diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs index 8300a536242..d087777cb25 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs @@ -26,6 +26,7 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation { internal sealed class HttpInMetricsListener : ListenerHandler { + private const string HttpServerDurationMetricName = "http.server.duration"; private const string OnStopEvent = "Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop"; private readonly Meter meter; @@ -37,7 +38,7 @@ internal HttpInMetricsListener(string name, Meter meter, AspNetCoreMetricsInstru { this.meter = meter; this.options = options; - this.httpServerDuration = meter.CreateHistogram("http.server.duration", "ms", "measures the duration of the inbound HTTP request"); + this.httpServerDuration = meter.CreateHistogram(HttpServerDurationMetricName, "ms", "measures the duration of the inbound HTTP request"); } public override void OnEventWritten(string name, object payload) @@ -53,9 +54,9 @@ public override void OnEventWritten(string name, object payload) try { - if (this.options.Filter?.Invoke(context) == false) + if (this.options.Filter?.Invoke(HttpServerDurationMetricName, context) == false) { - AspNetCoreInstrumentationEventSource.Log.RequestIsFilteredOut(Activity.Current.OperationName); + AspNetCoreInstrumentationEventSource.Log.RequestIsFilteredOut(HttpServerDurationMetricName); return; } } @@ -100,7 +101,7 @@ public override void OnEventWritten(string name, object payload) { try { - this.options.Enrich(context, ref tags); + this.options.Enrich(HttpServerDurationMetricName, context, ref tags); } catch (Exception ex) { diff --git a/src/OpenTelemetry.Instrumentation.Http/HttpClientInstrumentationOptions.cs b/src/OpenTelemetry.Instrumentation.Http/HttpClientInstrumentationOptions.cs index 193b2141b13..1084bbdc44d 100644 --- a/src/OpenTelemetry.Instrumentation.Http/HttpClientInstrumentationOptions.cs +++ b/src/OpenTelemetry.Instrumentation.Http/HttpClientInstrumentationOptions.cs @@ -31,7 +31,7 @@ public class HttpClientInstrumentationOptions { /// /// Gets or sets a filter function that determines whether or not to - /// collect telemetry about requests on a per request basis. + /// collect telemetry on a per request basis. /// /// /// FilterHttpRequestMessage is only executed on .NET and .NET @@ -83,7 +83,7 @@ public class HttpClientInstrumentationOptions /// /// Gets or sets a filter function that determines whether or not to - /// collect telemetry about requests on a per request basis. + /// collect telemetry on a per request basis. /// /// /// FilterHttpWebRequest is only executed on .NET Framework diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/MetricTests.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/MetricTests.cs index f4bc5e6b54c..baf08486031 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/MetricTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/MetricTests.cs @@ -98,7 +98,7 @@ public async Task MetricNotCollectedWhenFilterIsApplied() void ConfigureTestServices(IServiceCollection services) { this.meterProvider = Sdk.CreateMeterProviderBuilder() - .AddAspNetCoreInstrumentation(opt => opt.Filter = (ctx) => ctx.Request.Path != "/api/values/2") + .AddAspNetCoreInstrumentation(opt => opt.Filter = (name, ctx) => ctx.Request.Path != "/api/values/2") .AddInMemoryExporter(metricItems) .Build(); } @@ -150,7 +150,7 @@ public async Task MetricEnrichedWithCustomTags() void ConfigureTestServices(IServiceCollection services) { this.meterProvider = Sdk.CreateMeterProviderBuilder() - .AddAspNetCoreInstrumentation(opt => opt.Enrich = (HttpContext _, ref TagList tags) => + .AddAspNetCoreInstrumentation(opt => opt.Enrich = (string _, HttpContext _, ref TagList tags) => { foreach (var keyValuePair in tagsToAdd) { From 8d908b9d2bbb294e94f32a9e2fff425cfedd71a0 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Fri, 9 Dec 2022 10:06:44 -0800 Subject: [PATCH 2/2] [Traces] Support named options in SqlClient instrumentation (#3994) * Introduce OpenTelemetry.DependencyInjection and refactor SDK. * Update SqlClient instrumentation to use DependencyInjection package. * Tweak. * Manual merge fixes. * Some refactoring. * Renames. * Bug fixes and refactoring. * Cleanup and docs. * More updates. * More cleanup. * API updates. * Namespace tweaks. * Refactoring and API changes. * Example tweaks. * Example cleanup. * XML doc updates. * XML doc updates. * Bug fix. * Test fixes. * Example fixup. * Test fixup. * Test fixup. * Example fixup. * Test fixup. * Test fixup. * Test fixup. * CHANGELOG patch. * Add test project. * Cleanup. * Added configure tests. * MeterProviderBuilder extensions tests. * TracerProviderBuilder extensions tests * Some README content. * CHANGELOG patch. * Doc updates. * Example code fixes. * Doc updates. * Doc updates. * Doc updates and cleanup. * Doc updates. * Skip ApiCompat on new project. * API tweaks. * Test fixes. * XML doc improvements. * Tweaks. * Project file updates. * Test fix. * Project rename. * Updated for new project name. * Manual fixes for merge from main. * More manual fixes for merge from main. * Updates. * Unit test. * CHANGELOG patch. --- .../.publicApi/net462/PublicAPI.Unshipped.txt | 4 +- .../netstandard2.0/PublicAPI.Unshipped.txt | 4 +- .../CHANGELOG.md | 5 +++ ...Telemetry.Instrumentation.SqlClient.csproj | 6 ++- .../TracerProviderBuilderExtensions.cs | 43 ++++++++++++++++--- .../SqlClientTests.cs | 25 +++++++++-- 6 files changed, 76 insertions(+), 11 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.SqlClient/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.SqlClient/.publicApi/net462/PublicAPI.Unshipped.txt index 4958bb839fd..ee167e8afff 100644 --- a/src/OpenTelemetry.Instrumentation.SqlClient/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.SqlClient/.publicApi/net462/PublicAPI.Unshipped.txt @@ -13,4 +13,6 @@ OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.SetDbSta OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.SetDbStatementForText.set -> void OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.SqlClientInstrumentationOptions() -> void OpenTelemetry.Trace.TracerProviderBuilderExtensions -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddSqlClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action configureSqlClientInstrumentationOptions = null) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddSqlClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddSqlClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action configureSqlClientInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddSqlClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action configureSqlClientInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder diff --git a/src/OpenTelemetry.Instrumentation.SqlClient/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.SqlClient/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 4958bb839fd..ee167e8afff 100644 --- a/src/OpenTelemetry.Instrumentation.SqlClient/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.SqlClient/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -13,4 +13,6 @@ OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.SetDbSta OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.SetDbStatementForText.set -> void OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.SqlClientInstrumentationOptions() -> void OpenTelemetry.Trace.TracerProviderBuilderExtensions -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddSqlClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action configureSqlClientInstrumentationOptions = null) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddSqlClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddSqlClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action configureSqlClientInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddSqlClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action configureSqlClientInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder diff --git a/src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md index aa7938f49d3..fe76f55e8b6 100644 --- a/src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md @@ -10,6 +10,11 @@ Framework. ([#3900](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3900)) +* Added overloads which accept a name to the `TracerProviderBuilder` + `AddSqlClientInstrumentation` extension to allow for more fine-grained options + management + ([#3994](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3994)) + ## 1.0.0-rc9.9 Released 2022-Nov-07 diff --git a/src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj b/src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj index d800a38d9c9..e2d22fad28e 100644 --- a/src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj +++ b/src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj @@ -16,7 +16,11 @@ - + + + + + diff --git a/src/OpenTelemetry.Instrumentation.SqlClient/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.SqlClient/TracerProviderBuilderExtensions.cs index 2279e97f5fc..5435dd8c1c1 100644 --- a/src/OpenTelemetry.Instrumentation.SqlClient/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.SqlClient/TracerProviderBuilderExtensions.cs @@ -14,6 +14,8 @@ // limitations under the License. // +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using OpenTelemetry.Instrumentation.SqlClient; using OpenTelemetry.Instrumentation.SqlClient.Implementation; using OpenTelemetry.Internal; @@ -29,18 +31,49 @@ public static class TracerProviderBuilderExtensions /// Enables SqlClient instrumentation. /// /// being configured. - /// SqlClient configuration options. + /// The instance of to chain the calls. + public static TracerProviderBuilder AddSqlClientInstrumentation(this TracerProviderBuilder builder) + => AddSqlClientInstrumentation(builder, name: null, configureSqlClientInstrumentationOptions: null); + + /// + /// Enables SqlClient instrumentation. + /// + /// being configured. + /// Callback action for configuring . /// The instance of to chain the calls. public static TracerProviderBuilder AddSqlClientInstrumentation( this TracerProviderBuilder builder, - Action configureSqlClientInstrumentationOptions = null) + Action configureSqlClientInstrumentationOptions) + => AddSqlClientInstrumentation(builder, name: null, configureSqlClientInstrumentationOptions); + + /// + /// Enables SqlClient instrumentation. + /// + /// being configured. + /// Name which is used when retrieving options. + /// Callback action for configuring . + /// The instance of to chain the calls. + public static TracerProviderBuilder AddSqlClientInstrumentation( + this TracerProviderBuilder builder, + string name, + Action configureSqlClientInstrumentationOptions) { Guard.ThrowIfNull(builder); - var sqlOptions = new SqlClientInstrumentationOptions(); - configureSqlClientInstrumentationOptions?.Invoke(sqlOptions); + name ??= Options.DefaultName; + + if (configureSqlClientInstrumentationOptions != null) + { + builder.ConfigureServices(services => services.Configure(name, configureSqlClientInstrumentationOptions)); + } + + builder.AddInstrumentation(sp => + { + var sqlOptions = sp.GetRequiredService>().Get(name); + + return new SqlClientInstrumentation(sqlOptions); + }); - builder.AddInstrumentation(() => new SqlClientInstrumentation(sqlOptions)); builder.AddSource(SqlActivitySourceHelper.ActivitySourceName); return builder; diff --git a/test/OpenTelemetry.Instrumentation.SqlClient.Tests/SqlClientTests.cs b/test/OpenTelemetry.Instrumentation.SqlClient.Tests/SqlClientTests.cs index cba60ba6e7a..b72bcbacf5a 100644 --- a/test/OpenTelemetry.Instrumentation.SqlClient.Tests/SqlClientTests.cs +++ b/test/OpenTelemetry.Instrumentation.SqlClient.Tests/SqlClientTests.cs @@ -14,12 +14,10 @@ // limitations under the License. // -using System; -using System.Collections.Generic; using System.Data; using System.Diagnostics; -using System.Linq; using Microsoft.Data.SqlClient; +using Microsoft.Extensions.DependencyInjection; using OpenTelemetry.Instrumentation.SqlClient.Implementation; using OpenTelemetry.Tests; using OpenTelemetry.Trace; @@ -62,6 +60,27 @@ public void SqlClient_BadArgs() Assert.Throws(() => builder.AddSqlClientInstrumentation()); } + [Fact] + public void SqlClient_NamedOptions() + { + int defaultExporterOptionsConfigureOptionsInvocations = 0; + int namedExporterOptionsConfigureOptionsInvocations = 0; + + using var tracerProvider = Sdk.CreateTracerProviderBuilder() + .ConfigureServices(services => + { + services.Configure(o => defaultExporterOptionsConfigureOptionsInvocations++); + + services.Configure("Instrumentation2", o => namedExporterOptionsConfigureOptionsInvocations++); + }) + .AddSqlClientInstrumentation() + .AddSqlClientInstrumentation("Instrumentation2", configureSqlClientInstrumentationOptions: null) + .Build(); + + Assert.Equal(1, defaultExporterOptionsConfigureOptionsInvocations); + Assert.Equal(1, namedExporterOptionsConfigureOptionsInvocations); + } + [Trait("CategoryName", "SqlIntegrationTests")] [SkipUnlessEnvVarFoundTheory(SqlConnectionStringEnvVarName)] [InlineData(CommandType.Text, "select 1/1", false)]