-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathTracerProviderBuilderExtensions.cs
More file actions
67 lines (57 loc) · 3.07 KB
/
Copy pathTracerProviderBuilderExtensions.cs
File metadata and controls
67 lines (57 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Instrumentation.SqlClient;
using OpenTelemetry.Instrumentation.SqlClient.Implementation;
using OpenTelemetry.Internal;
namespace OpenTelemetry.Trace;
/// <summary>
/// Extension methods to simplify registering of dependency instrumentation.
/// </summary>
public static class TracerProviderBuilderExtensions
{
/// <summary>
/// Enables SqlClient instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddSqlClientInstrumentation(this TracerProviderBuilder builder)
=> AddSqlClientInstrumentation(builder, name: null, configureSqlClientTraceInstrumentationOptions: null);
/// <summary>
/// Enables SqlClient instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configureSqlClientTraceInstrumentationOptions">Callback action for configuring <see cref="SqlClientTraceInstrumentationOptions"/>.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddSqlClientInstrumentation(
this TracerProviderBuilder builder,
Action<SqlClientTraceInstrumentationOptions> configureSqlClientTraceInstrumentationOptions)
=> AddSqlClientInstrumentation(builder, name: null, configureSqlClientTraceInstrumentationOptions);
/// <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="configureSqlClientTraceInstrumentationOptions">Callback action for configuring <see cref="SqlClientTraceInstrumentationOptions"/>.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddSqlClientInstrumentation(
this TracerProviderBuilder builder,
string? name,
Action<SqlClientTraceInstrumentationOptions>? configureSqlClientTraceInstrumentationOptions)
{
Guard.ThrowIfNull(builder);
name ??= Options.DefaultName;
if (configureSqlClientTraceInstrumentationOptions != null)
{
builder.ConfigureServices(services => services.Configure(name, configureSqlClientTraceInstrumentationOptions));
}
builder.AddInstrumentation(sp =>
{
var sqlOptions = sp.GetRequiredService<IOptionsMonitor<SqlClientTraceInstrumentationOptions>>().Get(name);
return SqlClientInstrumentation.Instance.AddTracingHandle(sqlOptions);
});
builder.AddSource(SqlTelemetryHelper.ActivitySource.Name);
return builder;
}
}