-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathNpgsqlTracingOptionsBuilder.cs
More file actions
164 lines (147 loc) · 6.69 KB
/
NpgsqlTracingOptionsBuilder.cs
File metadata and controls
164 lines (147 loc) · 6.69 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Diagnostics;
namespace Npgsql;
/// <summary>
/// A builder to configure Npgsql's support for OpenTelemetry tracing.
/// </summary>
public sealed class NpgsqlTracingOptionsBuilder
{
Func<NpgsqlCommand, bool>? _commandFilter;
Func<NpgsqlBatch, bool>? _batchFilter;
Action<Activity, NpgsqlCommand>? _commandEnrichmentCallback;
Action<Activity, NpgsqlBatch>? _batchEnrichmentCallback;
Func<NpgsqlCommand, string?>? _commandSpanNameProvider;
Func<NpgsqlBatch, string?>? _batchSpanNameProvider;
bool _enableFirstResponseEvent = true;
bool _enablePhysicalOpenTracing = true;
Func<string, bool>? _copyOperationFilter;
Action<Activity, string>? _copyOperationEnrichmentCallback;
Func<string, string?>? _copyOperationSpanNameProvider;
internal NpgsqlTracingOptionsBuilder()
{
}
/// <summary>
/// Configures a filter function that determines whether to emit tracing information for an <see cref="NpgsqlCommand"/>.
/// By default, tracing information is emitted for all commands.
/// </summary>
public NpgsqlTracingOptionsBuilder ConfigureCommandFilter(Func<NpgsqlCommand, bool>? commandFilter)
{
_commandFilter = commandFilter;
return this;
}
/// <summary>
/// Configures a filter function that determines whether to emit tracing information for an <see cref="NpgsqlBatch"/>.
/// By default, tracing information is emitted for all batches.
/// </summary>
public NpgsqlTracingOptionsBuilder ConfigureBatchFilter(Func<NpgsqlBatch, bool>? batchFilter)
{
_batchFilter = batchFilter;
return this;
}
/// <summary>
/// Configures a callback that can enrich the <see cref="Activity"/> emitted for the given <see cref="NpgsqlCommand"/>.
/// </summary>
public NpgsqlTracingOptionsBuilder ConfigureCommandEnrichmentCallback(Action<Activity, NpgsqlCommand>? commandEnrichmentCallback)
{
_commandEnrichmentCallback = commandEnrichmentCallback;
return this;
}
/// <summary>
/// Configures a callback that can enrich the <see cref="Activity"/> emitted for the given <see cref="NpgsqlBatch"/>.
/// </summary>
public NpgsqlTracingOptionsBuilder ConfigureBatchEnrichmentCallback(Action<Activity, NpgsqlBatch>? batchEnrichmentCallback)
{
_batchEnrichmentCallback = batchEnrichmentCallback;
return this;
}
/// <summary>
/// Configures a callback that provides the tracing span's name for an <see cref="NpgsqlCommand"/>. If <c>null</c>, the default standard
/// span name is used, which is the database name.
/// </summary>
public NpgsqlTracingOptionsBuilder ConfigureCommandSpanNameProvider(Func<NpgsqlCommand, string?>? commandSpanNameProvider)
{
_commandSpanNameProvider = commandSpanNameProvider;
return this;
}
/// <summary>
/// Configures a callback that provides the tracing span's name for an <see cref="NpgsqlBatch"/>. If <c>null</c>, the default standard
/// span name is used, which is the database name.
/// </summary>
public NpgsqlTracingOptionsBuilder ConfigureBatchSpanNameProvider(Func<NpgsqlBatch, string?>? batchSpanNameProvider)
{
_batchSpanNameProvider = batchSpanNameProvider;
return this;
}
/// <summary>
/// Gets or sets a value indicating whether to enable the "time-to-first-read" event.
/// Default is true to preserve existing behavior.
/// </summary>
public NpgsqlTracingOptionsBuilder EnableFirstResponseEvent(bool enable = true)
{
_enableFirstResponseEvent = enable;
return this;
}
/// <summary>
/// Gets or sets a value indicating whether to trace physical connection open.
/// Default is true to preserve existing behavior.
/// </summary>
public NpgsqlTracingOptionsBuilder EnablePhysicalOpenTracing(bool enable = true)
{
_enablePhysicalOpenTracing = enable;
return this;
}
/// <summary>
/// Configures a filter function that determines whether to emit tracing information for a copy operation.
/// By default, tracing information is emitted for all copy operations.
/// </summary>
public NpgsqlTracingOptionsBuilder ConfigureCopyOperationFilter(Func<string, bool>? copyOperationFilter)
{
_copyOperationFilter = copyOperationFilter;
return this;
}
/// <summary>
/// Configures a callback that can enrich the <see cref="Activity"/> emitted for a given copy operation.
/// </summary>
public NpgsqlTracingOptionsBuilder ConfigureCopyOperationEnrichmentCallback(Action<Activity, string>? copyOperationEnrichmentCallback)
{
_copyOperationEnrichmentCallback = copyOperationEnrichmentCallback;
return this;
}
/// <summary>
/// Configures a callback that provides the tracing span's name for a copy operation. If <c>null</c>, the default standard
/// span name is used, which is the database name.
/// </summary>
public NpgsqlTracingOptionsBuilder ConfigureCopyOperationSpanNameProvider(Func<string, string?>? copyOperationSpanNameProvider)
{
_copyOperationSpanNameProvider = copyOperationSpanNameProvider;
return this;
}
internal NpgsqlTracingOptions Build() => new()
{
CommandFilter = _commandFilter,
BatchFilter = _batchFilter,
CommandEnrichmentCallback = _commandEnrichmentCallback,
BatchEnrichmentCallback = _batchEnrichmentCallback,
CommandSpanNameProvider = _commandSpanNameProvider,
BatchSpanNameProvider = _batchSpanNameProvider,
EnableFirstResponseEvent = _enableFirstResponseEvent,
EnablePhysicalOpenTracing = _enablePhysicalOpenTracing,
CopyOperationFilter = _copyOperationFilter,
CopyOperationEnrichmentCallback = _copyOperationEnrichmentCallback,
CopyOperationSpanNameProvider = _copyOperationSpanNameProvider
};
}
sealed class NpgsqlTracingOptions
{
internal Func<NpgsqlCommand, bool>? CommandFilter { get; init; }
internal Func<NpgsqlBatch, bool>? BatchFilter { get; init; }
internal Action<Activity, NpgsqlCommand>? CommandEnrichmentCallback { get; init; }
internal Action<Activity, NpgsqlBatch>? BatchEnrichmentCallback { get; init; }
internal Func<NpgsqlCommand, string?>? CommandSpanNameProvider { get; init; }
internal Func<NpgsqlBatch, string?>? BatchSpanNameProvider { get; init; }
internal bool EnableFirstResponseEvent { get; init; }
internal bool EnablePhysicalOpenTracing { get; init; }
internal Func<string, bool>? CopyOperationFilter { get; init; }
internal Action<Activity, string>? CopyOperationEnrichmentCallback { get; init; }
internal Func<string, string?>? CopyOperationSpanNameProvider { get; init; }
}