[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.
This commit is contained in:
Mikel Blanchard 2022-12-09 10:06:44 -08:00 committed by GitHub
parent 723ad0bd65
commit 8d908b9d2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 76 additions and 11 deletions

View File

@ -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<OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions> 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<OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions> configureSqlClientInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddSqlClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions> configureSqlClientInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder

View File

@ -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<OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions> 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<OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions> configureSqlClientInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddSqlClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions> configureSqlClientInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder

View File

@ -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

View File

@ -16,7 +16,11 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Api\OpenTelemetry.Api.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Extensions.DependencyInjection\OpenTelemetry.Extensions.DependencyInjection.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options" Version="$(MicrosoftExtensionsOptionsPkgVer)" />
</ItemGroup>
</Project>

View File

@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Instrumentation.SqlClient;
using OpenTelemetry.Instrumentation.SqlClient.Implementation;
using OpenTelemetry.Internal;
@ -29,18 +31,49 @@ namespace OpenTelemetry.Trace
/// Enables SqlClient instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configureSqlClientInstrumentationOptions">SqlClient configuration options.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddSqlClientInstrumentation(this TracerProviderBuilder builder)
=> AddSqlClientInstrumentation(builder, name: null, configureSqlClientInstrumentationOptions: null);
/// <summary>
/// Enables SqlClient instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configureSqlClientInstrumentationOptions">Callback action for configuring <see cref="SqlClientInstrumentationOptions"/>.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddSqlClientInstrumentation(
this TracerProviderBuilder builder,
Action<SqlClientInstrumentationOptions> configureSqlClientInstrumentationOptions = null)
Action<SqlClientInstrumentationOptions> configureSqlClientInstrumentationOptions)
=> AddSqlClientInstrumentation(builder, name: null, configureSqlClientInstrumentationOptions);
/// <summary>
/// Enables SqlClient instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="name">Name which is used when retrieving options.</param>
/// <param name="configureSqlClientInstrumentationOptions">Callback action for configuring <see cref="SqlClientInstrumentationOptions"/>.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddSqlClientInstrumentation(
this TracerProviderBuilder builder,
string name,
Action<SqlClientInstrumentationOptions> 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<IOptionsMonitor<SqlClientInstrumentationOptions>>().Get(name);
return new SqlClientInstrumentation(sqlOptions);
});
builder.AddInstrumentation(() => new SqlClientInstrumentation(sqlOptions));
builder.AddSource(SqlActivitySourceHelper.ActivitySourceName);
return builder;

View File

@ -14,12 +14,10 @@
// limitations under the License.
// </copyright>
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 @@ namespace OpenTelemetry.Instrumentation.SqlClient.Tests
Assert.Throws<ArgumentNullException>(() => builder.AddSqlClientInstrumentation());
}
[Fact]
public void SqlClient_NamedOptions()
{
int defaultExporterOptionsConfigureOptionsInvocations = 0;
int namedExporterOptionsConfigureOptionsInvocations = 0;
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.ConfigureServices(services =>
{
services.Configure<SqlClientInstrumentationOptions>(o => defaultExporterOptionsConfigureOptionsInvocations++);
services.Configure<SqlClientInstrumentationOptions>("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)]