diff --git a/src/OpenTelemetry.Exporter.OneCollector/CHANGELOG.md b/src/OpenTelemetry.Exporter.OneCollector/CHANGELOG.md index a3b6cab3dd..c8befa9ccd 100644 --- a/src/OpenTelemetry.Exporter.OneCollector/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.OneCollector/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +## 0.1.0-alpha.2 + +Released 2023-Mar-6 + * Update OpenTelemetry to 1.4.0 ([#1038](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1038)) diff --git a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/.publicApi/net462/PublicAPI.Unshipped.txt index 079ec48823..95461d9b80 100644 --- a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/.publicApi/net462/PublicAPI.Unshipped.txt @@ -11,4 +11,6 @@ OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumenta OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.SuppressDownstreamInstrumentation.get -> bool OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.SuppressDownstreamInstrumentation.set -> void OpenTelemetry.Trace.TracerProviderBuilderExtensions -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder \ No newline at end of file +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action configure) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action configure) -> OpenTelemetry.Trace.TracerProviderBuilder \ No newline at end of file diff --git a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 079ec48823..95461d9b80 100644 --- a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -11,4 +11,6 @@ OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumenta OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.SuppressDownstreamInstrumentation.get -> bool OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.SuppressDownstreamInstrumentation.set -> void OpenTelemetry.Trace.TracerProviderBuilderExtensions -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder \ No newline at end of file +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action configure) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action configure) -> OpenTelemetry.Trace.TracerProviderBuilder \ No newline at end of file diff --git a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/CHANGELOG.md index a5885260dd..4bfcb84e5d 100644 --- a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/CHANGELOG.md @@ -2,12 +2,24 @@ ## Unreleased -* Updated OTel SDK package version to 1.2.0 +## 1.0.0-beta.4 + +Released 2023-Mar-06 + +* Updated OTel SDK package version to 1.4.0 + ([#1019](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1019)) * Update minimum full framework support to net462 * Requests that get an HTTP status code of 404 are not marked as an error span status * Add MaxDbStatementLength option with default of 4096 * Remove duplicated HTTP method and URL from db.statement attribute value -* Fix faulty logic of MaxDbStatementLength option [(#425)](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/425) +* Fix faulty logic of MaxDbStatementLength option + ([#425](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/425)) +* Remove method with default attribute + ([#1019](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1019)) +* Added overloads which accept a name to the `TracerProviderBuilder` + `AddElasticsearchClientInstrumentation` extension to allow for more fine-grained + options management + ([#1019](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1019)) ## 1.0.0-beta.3 diff --git a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/OpenTelemetry.Instrumentation.ElasticsearchClient.csproj b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/OpenTelemetry.Instrumentation.ElasticsearchClient.csproj index 64150f4f68..b0323bbc18 100644 --- a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/OpenTelemetry.Instrumentation.ElasticsearchClient.csproj +++ b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/OpenTelemetry.Instrumentation.ElasticsearchClient.csproj @@ -8,7 +8,7 @@ true - + diff --git a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/README.md b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/README.md index 9c2d8c555e..95934edaf5 100644 --- a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/README.md +++ b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/README.md @@ -5,28 +5,105 @@ [![NuGet](https://img.shields.io/nuget/v/OpenTelemetry.Instrumentation.ElasticsearchClient.svg)](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.ElasticsearchClient) [![NuGet](https://img.shields.io/nuget/dt/OpenTelemetry.Instrumentation.ElasticsearchClient.svg)](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.ElasticsearchClient) -Automatically instruments events emitted by the [NEST/Elasticsearch.Net](https://www.nuget.org/packages/NEST) -client library. +This is an [Instrumentation +Library](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/glossary.md#instrumentation-library), +which instruments [NEST/Elasticsearch.Net](https://www.nuget.org/packages/NEST) +and collects traces about outgoing requests. -### Installation +**Note: This component is based on the OpenTelemetry semantic conventions for +[metrics](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/metrics/semantic_conventions) +and +[traces](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/trace/semantic_conventions). +These conventions are +[Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/document-status.md), +and hence, this package is a +[pre-release](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/VERSIONING.md#pre-releases). +Until a [stable +version](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/telemetry-stability.md) +is released, there can be [breaking changes](./CHANGELOG.md). You can track the +progress from +[milestones](https://github.com/open-telemetry/opentelemetry-dotnet/milestone/23).** + +## Steps to enable OpenTelemetry.Instrumentation.ElasticsearchClient + +### Step 1: Install Package + +Add a reference to the +[`OpenTelemetry.Instrumentation.ElasticsearchClient`](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.ElasticsearchClient) +package. Also, add any other instrumentations & exporters you will need. ```shell -dotnet add package OpenTelemetry.Instrumentation.ElasticsearchClient +dotnet add package --prerelease OpenTelemetry.Instrumentation.ElasticsearchClient +``` + +### Step 2: Enable NEST/Elasticsearch.Net Instrumentation at application startup + +`NEST/Elasticsearch.Net` instrumentation must be enabled at application startup. + +The following example demonstrates adding `NEST/Elasticsearch.Net` +instrumentation to a console application. This example also sets up the +OpenTelemetry Console exporter, which requires adding the package +[`OpenTelemetry.Exporter.Console`](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Exporter.Console/README.md) +to the application. + +```csharp +using OpenTelemetry; +using OpenTelemetry.Trace; + +public class Program +{ + public static void Main(string[] args) + { + using var tracerProvider = Sdk.CreateTracerProviderBuilder() + .AddElasticsearchClientInstrumentation() + .AddConsoleExporter() + .Build(); + } +} ``` -### Configuration +For an ASP.NET Core application, adding instrumentation is typically done in the +`ConfigureServices` of your `Startup` class. Refer to documentation for +[OpenTelemetry.Instrumentation.AspNetCore](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Instrumentation.AspNetCore/README.md). + +For an ASP.NET application, adding instrumentation is typically done in the +`Global.asax.cs`. Refer to the documentation for +[OpenTelemetry.Instrumentation.AspNet](../OpenTelemetry.Instrumentation.AspNet/README.md). -ASP.NET Core instrumentation example: +## Advanced configuration + +This instrumentation can be configured to change the default behavior by using +`ElasticsearchClientInstrumentationOptions`. ```csharp -// Add OpenTelemetry and Elasticsearch client instrumentation -services.AddOpenTelemetry().WithTracing(x => +services.AddOpenTelemetry() + .WithTracing(builder => builder + .AddElasticsearchClientInstrumentation(options => + { + // add request json as db.statement attribute tag + options.SetDbStatementForRequest = true; + }) + .AddConsoleExporter()); +``` + +When used with +[`OpenTelemetry.Extensions.Hosting`](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Extensions.Hosting/README.md), +all configurations to `ElasticsearchClientInstrumentationOptions` +can be done in the `ConfigureServices` method of you applications `Startup` +class as shown below. + +```csharp +// Configure +services.Configure(options => { - x.AddElasticsearchClientInstrumentation(); - x.AddOtlpExporter(config => { - // Configure OTLP - }); + // add request json as db.statement attribute tag + options.SetDbStatementForRequest = true; }); + +services.AddOpenTelemetry() + .WithTracing(builder => builder + .AddElasticsearchClientInstrumentation() + .AddConsoleExporter()); ``` ## Elastic.Clients.Elasticsearch diff --git a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/TracerProviderBuilderExtensions.cs index 6a14569a0b..8933b157f4 100644 --- a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/TracerProviderBuilderExtensions.cs @@ -15,6 +15,8 @@ // using System; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using OpenTelemetry.Instrumentation.ElasticsearchClient; using OpenTelemetry.Instrumentation.ElasticsearchClient.Implementation; using OpenTelemetry.Internal; @@ -30,18 +32,49 @@ public static class TracerProviderBuilderExtensions /// Enables Elasticsearch client Instrumentation. /// /// being configured. + /// The instance of to chain the calls. + public static TracerProviderBuilder AddElasticsearchClientInstrumentation( + this TracerProviderBuilder builder) => + AddElasticsearchClientInstrumentation(builder, name: null, configure: null); + + /// + /// Enables Elasticsearch client Instrumentation. + /// + /// being configured. + /// Elasticsearch client configuration options. + /// The instance of to chain the calls. + public static TracerProviderBuilder AddElasticsearchClientInstrumentation( + this TracerProviderBuilder builder, + Action configure) => + AddElasticsearchClientInstrumentation(builder, name: null, configure); + + /// + /// Enables Elasticsearch client Instrumentation. + /// + /// being configured. + /// Name which is used when retrieving options. /// Elasticsearch client configuration options. /// The instance of to chain the calls. public static TracerProviderBuilder AddElasticsearchClientInstrumentation( this TracerProviderBuilder builder, - Action configure = null) + string name, + Action configure) { Guard.ThrowIfNull(builder); - var elasticsearchClientOptions = new ElasticsearchClientInstrumentationOptions(); - configure?.Invoke(elasticsearchClientOptions); + name ??= Options.DefaultName; + + if (configure != null) + { + builder.ConfigureServices(services => services.Configure(name, configure)); + } + + builder.AddInstrumentation(sp => + { + var options = sp.GetRequiredService>().Get(name); + return new ElasticsearchClientInstrumentation(options); + }); - builder.AddInstrumentation(() => new ElasticsearchClientInstrumentation(elasticsearchClientOptions)); builder.AddSource(ElasticsearchRequestPipelineDiagnosticListener.ActivitySourceName); builder.AddLegacySource("CallElasticsearch"); diff --git a/test/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests/DependencyInjectionConfigTests.cs b/test/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests/DependencyInjectionConfigTests.cs new file mode 100644 index 0000000000..0205eb1729 --- /dev/null +++ b/test/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests/DependencyInjectionConfigTests.cs @@ -0,0 +1,65 @@ +// +// 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. +// + +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using OpenTelemetry.Trace; +using Xunit; + +namespace OpenTelemetry.Instrumentation.ElasticsearchClient.Tests; + +public class DependencyInjectionConfigTests +{ + [Theory] + [InlineData(null)] + [InlineData("CustomName")] + public async Task TestTracingOptionsDiConfig(string name) + { + bool optionsPickedFromDi = false; + + var services = new ServiceCollection(); + + services + .Configure(name, _ => optionsPickedFromDi = true) + .AddOpenTelemetry() + .WithTracing(builder => + builder.AddElasticsearchClientInstrumentation(name, configure: null)); + + var sp = services.BuildServiceProvider(); + + try + { + foreach (var hostedService in sp.GetServices()) + { + await hostedService.StartAsync(CancellationToken.None); + } + + Assert.True(optionsPickedFromDi); + } + finally + { + foreach (var hostedService in sp.GetServices().Reverse()) + { + await hostedService.StopAsync(CancellationToken.None); + } + + await sp.DisposeAsync(); + } + } +} diff --git a/test/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests.csproj b/test/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests.csproj index 853bf851a4..592864c354 100644 --- a/test/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests.csproj +++ b/test/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests.csproj @@ -1,14 +1,15 @@ - + Unit test project for OpenTelemetry Elasticsearch client instrumentation - net7.0;net6.0;netcoreapp3.1 + net7.0;net6.0 true + all