diff --git a/src/OpenTelemetry.Exporter.Geneva/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.Geneva/.publicApi/net462/PublicAPI.Unshipped.txt index 1fd2dcd539..db58b7a99f 100644 --- a/src/OpenTelemetry.Exporter.Geneva/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Geneva/.publicApi/net462/PublicAPI.Unshipped.txt @@ -3,3 +3,8 @@ OpenTelemetry.Exporter.Geneva.EventNameExportMode.ExportAsPartAName = 1 -> OpenT OpenTelemetry.Exporter.Geneva.EventNameExportMode.None = 0 -> OpenTelemetry.Exporter.Geneva.EventNameExportMode OpenTelemetry.Exporter.Geneva.GenevaExporterOptions.EventNameExportMode.get -> OpenTelemetry.Exporter.Geneva.EventNameExportMode OpenTelemetry.Exporter.Geneva.GenevaExporterOptions.EventNameExportMode.set -> void +static OpenTelemetry.Exporter.Geneva.GenevaExporterHelperExtensions.AddGenevaTraceExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action configure) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Exporter.Geneva.GenevaMetricExporterExtensions.AddGenevaMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Exporter.Geneva.GenevaMetricExporterExtensions.AddGenevaMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configure) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Exporter.Geneva.GenevaMetricExporterExtensions.AddGenevaMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure) -> OpenTelemetry.Metrics.MeterProviderBuilder +*REMOVED*static OpenTelemetry.Exporter.Geneva.GenevaMetricExporterExtensions.AddGenevaMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.Geneva/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.Geneva/.publicApi/net6.0/PublicAPI.Unshipped.txt index 4ca0ea2c19..db58b7a99f 100644 --- a/src/OpenTelemetry.Exporter.Geneva/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Geneva/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -2,4 +2,9 @@ OpenTelemetry.Exporter.Geneva.EventNameExportMode OpenTelemetry.Exporter.Geneva.EventNameExportMode.ExportAsPartAName = 1 -> OpenTelemetry.Exporter.Geneva.EventNameExportMode OpenTelemetry.Exporter.Geneva.EventNameExportMode.None = 0 -> OpenTelemetry.Exporter.Geneva.EventNameExportMode OpenTelemetry.Exporter.Geneva.GenevaExporterOptions.EventNameExportMode.get -> OpenTelemetry.Exporter.Geneva.EventNameExportMode -OpenTelemetry.Exporter.Geneva.GenevaExporterOptions.EventNameExportMode.set -> void \ No newline at end of file +OpenTelemetry.Exporter.Geneva.GenevaExporterOptions.EventNameExportMode.set -> void +static OpenTelemetry.Exporter.Geneva.GenevaExporterHelperExtensions.AddGenevaTraceExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action configure) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Exporter.Geneva.GenevaMetricExporterExtensions.AddGenevaMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Exporter.Geneva.GenevaMetricExporterExtensions.AddGenevaMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configure) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Exporter.Geneva.GenevaMetricExporterExtensions.AddGenevaMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure) -> OpenTelemetry.Metrics.MeterProviderBuilder +*REMOVED*static OpenTelemetry.Exporter.Geneva.GenevaMetricExporterExtensions.AddGenevaMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.Geneva/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.Geneva/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 1fd2dcd539..db58b7a99f 100644 --- a/src/OpenTelemetry.Exporter.Geneva/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.Geneva/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -3,3 +3,8 @@ OpenTelemetry.Exporter.Geneva.EventNameExportMode.ExportAsPartAName = 1 -> OpenT OpenTelemetry.Exporter.Geneva.EventNameExportMode.None = 0 -> OpenTelemetry.Exporter.Geneva.EventNameExportMode OpenTelemetry.Exporter.Geneva.GenevaExporterOptions.EventNameExportMode.get -> OpenTelemetry.Exporter.Geneva.EventNameExportMode OpenTelemetry.Exporter.Geneva.GenevaExporterOptions.EventNameExportMode.set -> void +static OpenTelemetry.Exporter.Geneva.GenevaExporterHelperExtensions.AddGenevaTraceExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action configure) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Exporter.Geneva.GenevaMetricExporterExtensions.AddGenevaMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Exporter.Geneva.GenevaMetricExporterExtensions.AddGenevaMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action configure) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Exporter.Geneva.GenevaMetricExporterExtensions.AddGenevaMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure) -> OpenTelemetry.Metrics.MeterProviderBuilder +*REMOVED*static OpenTelemetry.Exporter.Geneva.GenevaMetricExporterExtensions.AddGenevaMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Metrics.MeterProviderBuilder diff --git a/src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md b/src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md index 693592d177..be1bc1cd30 100644 --- a/src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md @@ -2,6 +2,15 @@ ## Unreleased +* Add named options support for `GenevaTraceExporter` and + `GenevaMetricExporter`. + ([#1218](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1218)) + +* Add a new overload for `AddGenevaMetricExporter` without any parameters to + avoid warning + [RS0026](https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/Microsoft.CodeAnalysis.PublicApiAnalyzers.md#rs0026-do-not-add-multiple-public-overloads-with-optional-parameters). + ([#1218](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1218)) + ## 1.5.0-rc.1 Released 2023-Jun-05 @@ -13,6 +22,7 @@ Released 2023-Jun-05 * TldLogExporter to export `SpanId` value in `ext_dt_spanId` field instead of `TraceId` value. ([#1184](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1184)) + * Add support for abstract domain sockets. ([#1199](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1199)) diff --git a/src/OpenTelemetry.Exporter.Geneva/GenevaExporterHelperExtensions.cs b/src/OpenTelemetry.Exporter.Geneva/GenevaExporterHelperExtensions.cs index 266c1b11e4..c050b34c3b 100644 --- a/src/OpenTelemetry.Exporter.Geneva/GenevaExporterHelperExtensions.cs +++ b/src/OpenTelemetry.Exporter.Geneva/GenevaExporterHelperExtensions.cs @@ -16,6 +16,8 @@ using System; using System.Diagnostics; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using OpenTelemetry.Internal; using OpenTelemetry.Trace; @@ -23,13 +25,36 @@ namespace OpenTelemetry.Exporter.Geneva; public static class GenevaExporterHelperExtensions { + /// + /// Adds to the . + /// + /// builder to use. + /// Exporter configuration options. + /// The instance of to chain the calls. public static TracerProviderBuilder AddGenevaTraceExporter(this TracerProviderBuilder builder, Action configure) + => AddGenevaTraceExporter(builder, name: null, configure); + + /// + /// Adds to the . + /// + /// builder to use. + /// /// Name which is used when retrieving options. + /// Exporter configuration options. + /// The instance of to chain the calls. + public static TracerProviderBuilder AddGenevaTraceExporter(this TracerProviderBuilder builder, string name, Action configure) { Guard.ThrowIfNull(builder); + name ??= Options.DefaultName; + + if (configure != null) + { + builder.ConfigureServices(services => services.Configure(name, configure)); + } + return builder.AddProcessor(sp => { - var exporterOptions = sp.GetOptions(); + var exporterOptions = sp.GetRequiredService>().Get(name); return BuildGenevaTraceExporter(exporterOptions, configure); }); diff --git a/src/OpenTelemetry.Exporter.Geneva/Internal/ServiceProviderExtensions.cs b/src/OpenTelemetry.Exporter.Geneva/Internal/ServiceProviderExtensions.cs deleted file mode 100644 index 9175001b9f..0000000000 --- a/src/OpenTelemetry.Exporter.Geneva/Internal/ServiceProviderExtensions.cs +++ /dev/null @@ -1,46 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -#if NET462_OR_GREATER || NETSTANDARD2_0 || NETSTANDARD2_1 || NETCOREAPP3_1_OR_GREATER -using Microsoft.Extensions.Options; -#endif - -namespace System; - -/// -/// Extension methods for OpenTelemetry dependency injection support. -/// -internal static class ServiceProviderExtensions -{ - /// - /// Get options from the supplied . - /// - /// Options type. - /// . - /// Options instance. - public static T GetOptions(this IServiceProvider serviceProvider) - where T : class, new() - { -#if NET462_OR_GREATER || NETSTANDARD2_0 || NETSTANDARD2_1 || NETCOREAPP3_1_OR_GREATER - IOptions options = (IOptions)serviceProvider.GetService(typeof(IOptions)); - - // Note: options could be null if user never invoked services.AddOptions(). - return options?.Value ?? new T(); -#else - return new T(); -#endif - } -} diff --git a/src/OpenTelemetry.Exporter.Geneva/Metrics/GenevaMetricExporterExtensions.cs b/src/OpenTelemetry.Exporter.Geneva/Metrics/GenevaMetricExporterExtensions.cs index 67da1c315c..4933ad7a62 100644 --- a/src/OpenTelemetry.Exporter.Geneva/Metrics/GenevaMetricExporterExtensions.cs +++ b/src/OpenTelemetry.Exporter.Geneva/Metrics/GenevaMetricExporterExtensions.cs @@ -15,6 +15,8 @@ // using System; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using OpenTelemetry.Internal; using OpenTelemetry.Metrics; @@ -26,15 +28,40 @@ public static class GenevaMetricExporterExtensions /// Adds to the . /// /// builder to use. + /// The instance of to chain the calls. + public static MeterProviderBuilder AddGenevaMetricExporter(this MeterProviderBuilder builder) + => AddGenevaMetricExporter(builder, name: null, configure: null); + + /// + /// Adds to the . + /// + /// builder to use. + /// Exporter configuration options. + /// The instance of to chain the calls. + public static MeterProviderBuilder AddGenevaMetricExporter(this MeterProviderBuilder builder, Action configure) + => AddGenevaMetricExporter(builder, name: null, configure); + + /// + /// Adds to the . + /// + /// builder to use. + /// /// Name which is used when retrieving options. /// Exporter configuration options. /// The instance of to chain the calls. - public static MeterProviderBuilder AddGenevaMetricExporter(this MeterProviderBuilder builder, Action configure = null) + public static MeterProviderBuilder AddGenevaMetricExporter(this MeterProviderBuilder builder, string name, Action configure) { Guard.ThrowIfNull(builder); + name ??= Options.DefaultName; + + if (configure != null) + { + builder.ConfigureServices(services => services.Configure(name, configure)); + } + return builder.AddReader(sp => { - var exporterOptions = sp.GetOptions(); + var exporterOptions = sp.GetRequiredService>().Get(name); return BuildGenevaMetricExporter(exporterOptions, configure); }); diff --git a/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaMetricExporterTests.cs b/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaMetricExporterTests.cs index 2054179466..f9b3ead921 100644 --- a/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaMetricExporterTests.cs +++ b/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaMetricExporterTests.cs @@ -26,7 +26,9 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; using Kaitai; +using Microsoft.Extensions.DependencyInjection; using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; using Xunit; using static OpenTelemetry.Exporter.Geneva.Tests.MetricsContract; @@ -873,6 +875,48 @@ public void SuccessfulSerializationWithCustomAccountAndNamespace() } } + [Fact] + public void AddGenevaMetricExporterNamedOptionsSupport() + { + string connectionString; + string connectionStringForNamedOptions; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + connectionString = "Account=OTelMonitoringAccount;Namespace=OTelMetricNamespace"; + connectionStringForNamedOptions = "Account=OTelMonitoringAccount-NamedOptions;Namespace=OTelMetricNamespace-NamedOptions"; + } + else + { + var path = GenerateTempFilePath(); + connectionString = $"Endpoint=unix:{path};Account=OTelMonitoringAccount;Namespace=OTelMetricNamespace"; + connectionStringForNamedOptions = $"Endpoint=unix:{path};Account=OTelMonitoringAccount-NamedOptions;Namespace=OTelMetricNamespace-NamedOptions"; + } + + using var meterProvider = Sdk.CreateMeterProviderBuilder() + .ConfigureServices(services => + { + services.Configure(options => + { + options.ConnectionString = connectionString; + }); + services.Configure("ExporterWithNamedOptions", options => + { + options.ConnectionString = connectionStringForNamedOptions; + }); + }) + .AddGenevaMetricExporter(options => + { + // ConnectionString for the options is already set in `IServiceCollection Configure` calls above + Assert.Equal(connectionString, options.ConnectionString); + }) + .AddGenevaMetricExporter("ExporterWithNamedOptions", options => + { + // ConnectionString for the named options is already set in `IServiceCollection Configure` calls above + Assert.Equal(connectionStringForNamedOptions, options.ConnectionString); + }) + .Build(); + } + private static string GenerateTempFilePath() { while (true) diff --git a/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaTraceExporterTests.cs b/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaTraceExporterTests.cs index cb7c61447a..7d9dbde3d0 100644 --- a/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaTraceExporterTests.cs +++ b/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaTraceExporterTests.cs @@ -24,6 +24,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; +using Microsoft.Extensions.DependencyInjection; using OpenTelemetry.Trace; using Xunit; @@ -514,6 +515,48 @@ public void TLDTraceExporter_Success_Windows() } } + [Fact] + public void AddGenevaTraceExporterNamedOptionsSupport() + { + string connectionString; + string connectionStringForNamedOptions; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + connectionString = "EtwSession=OpenTelemetry"; + connectionStringForNamedOptions = "EtwSession=OpenTelemetry-NamedOptions"; + } + else + { + var path = GetRandomFilePath(); + connectionString = "Endpoint=unix:" + path; + connectionStringForNamedOptions = "Endpoint=unix:" + path + "NamedOptions"; + } + + using var tracerProvider = Sdk.CreateTracerProviderBuilder() + .ConfigureServices(services => + { + services.Configure(options => + { + options.ConnectionString = connectionString; + }); + services.Configure("ExporterWithNamedOptions", options => + { + options.ConnectionString = connectionStringForNamedOptions; + }); + }) + .AddGenevaTraceExporter(options => + { + // ConnectionString for the options is already set in `IServiceCollection Configure` calls above + Assert.Equal(connectionString, options.ConnectionString); + }) + .AddGenevaTraceExporter("ExporterWithNamedOptions", options => + { + // ConnectionString for the named options is already set in `IServiceCollection Configure` calls above + Assert.Equal(connectionStringForNamedOptions, options.ConnectionString); + }) + .Build(); + } + private static string GetRandomFilePath() { while (true)