forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTracerProviderBuilderExtensions.cs
More file actions
68 lines (58 loc) · 2.82 KB
/
TracerProviderBuilderExtensions.cs
File metadata and controls
68 lines (58 loc) · 2.82 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
68
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Instrumentation.GrpcNetClient;
using OpenTelemetry.Instrumentation.GrpcNetClient.Implementation;
using OpenTelemetry.Internal;
namespace OpenTelemetry.Trace;
/// <summary>
/// Extension methods to simplify registering of gRPC client
/// instrumentation.
/// </summary>
public static class TracerProviderBuilderExtensions
{
/// <summary>
/// Enables gRPC client 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 AddGrpcClientInstrumentation(this TracerProviderBuilder builder)
=> AddGrpcClientInstrumentation(builder, name: null, configure: null);
/// <summary>
/// Enables gRPC client instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configure">Callback action for configuring <see cref="GrpcClientInstrumentationOptions"/>.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddGrpcClientInstrumentation(
this TracerProviderBuilder builder,
Action<GrpcClientInstrumentationOptions> configure)
=> AddGrpcClientInstrumentation(builder, name: null, configure);
/// <summary>
/// Enables gRPC client instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="name">Name which is used when retrieving options.</param>
/// <param name="configure">Callback action for configuring <see cref="GrpcClientInstrumentationOptions"/>.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddGrpcClientInstrumentation(
this TracerProviderBuilder builder,
string name,
Action<GrpcClientInstrumentationOptions> configure)
{
Guard.ThrowIfNull(builder);
name ??= Options.DefaultName;
if (configure != null)
{
builder.ConfigureServices(services => services.Configure(name, configure));
}
builder.AddSource(GrpcClientDiagnosticListener.ActivitySourceName);
builder.AddLegacySource("Grpc.Net.Client.GrpcOut");
return builder.AddInstrumentation(sp =>
{
var options = sp.GetRequiredService<IOptionsMonitor<GrpcClientInstrumentationOptions>>().Get(name);
return new GrpcClientInstrumentation(options);
});
}
}