forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTracingOptionsBuilder.cs
More file actions
112 lines (100 loc) · 4.36 KB
/
NpgsqlTracingOptionsBuilder.cs
File metadata and controls
112 lines (100 loc) · 4.36 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
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;
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;
}
internal NpgsqlTracingOptions Build() => new()
{
CommandFilter = _commandFilter,
BatchFilter = _batchFilter,
CommandEnrichmentCallback = _commandEnrichmentCallback,
BatchEnrichmentCallback = _batchEnrichmentCallback,
CommandSpanNameProvider = _commandSpanNameProvider,
BatchSpanNameProvider = _batchSpanNameProvider,
EnableFirstResponseEvent = _enableFirstResponseEvent
};
}
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; }
}