|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | + |
| 4 | +namespace Npgsql; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// A builder to configure Npgsql's support for OpenTelemetry tracing. |
| 8 | +/// </summary> |
| 9 | +public sealed class NpgsqlTracingOptionsBuilder |
| 10 | +{ |
| 11 | + Func<NpgsqlCommand, bool>? _commandFilter; |
| 12 | + Func<NpgsqlBatch, bool>? _batchFilter; |
| 13 | + Action<Activity, NpgsqlCommand>? _commandEnrichmentCallback; |
| 14 | + Action<Activity, NpgsqlBatch>? _batchEnrichmentCallback; |
| 15 | + Func<NpgsqlCommand, string?>? _commandSpanNameProvider; |
| 16 | + Func<NpgsqlBatch, string?>? _batchSpanNameProvider; |
| 17 | + bool _enableFirstResponseEvent = true; |
| 18 | + |
| 19 | + internal NpgsqlTracingOptionsBuilder() |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Configures a filter function that determines whether to emit tracing information for an <see cref="NpgsqlCommand"/>. |
| 25 | + /// By default, tracing information is emitted for all commands. |
| 26 | + /// </summary> |
| 27 | + public NpgsqlTracingOptionsBuilder ConfigureCommandFilter(Func<NpgsqlCommand, bool>? commandFilter) |
| 28 | + { |
| 29 | + _commandFilter = commandFilter; |
| 30 | + return this; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Configures a filter function that determines whether to emit tracing information for an <see cref="NpgsqlBatch"/>. |
| 35 | + /// By default, tracing information is emitted for all batches. |
| 36 | + /// </summary> |
| 37 | + public NpgsqlTracingOptionsBuilder ConfigureBatchFilter(Func<NpgsqlBatch, bool>? batchFilter) |
| 38 | + { |
| 39 | + _batchFilter = batchFilter; |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Configures a callback that can enrich the <see cref="Activity"/> emitted for the given <see cref="NpgsqlCommand"/>. |
| 45 | + /// </summary> |
| 46 | + public NpgsqlTracingOptionsBuilder ConfigureCommandEnrichmentCallback(Action<Activity, NpgsqlCommand>? commandEnrichmentCallback) |
| 47 | + { |
| 48 | + _commandEnrichmentCallback = commandEnrichmentCallback; |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Configures a callback that can enrich the <see cref="Activity"/> emitted for the given <see cref="NpgsqlBatch"/>. |
| 54 | + /// </summary> |
| 55 | + public NpgsqlTracingOptionsBuilder ConfigureBatchEnrichmentCallback(Action<Activity, NpgsqlBatch>? batchEnrichmentCallback) |
| 56 | + { |
| 57 | + _batchEnrichmentCallback = batchEnrichmentCallback; |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Configures a callback that provides the tracing span's name for an <see cref="NpgsqlCommand"/>. If <c>null</c>, the default standard |
| 63 | + /// span name is used, which is the database name. |
| 64 | + /// </summary> |
| 65 | + public NpgsqlTracingOptionsBuilder ConfigureCommandSpanNameProvider(Func<NpgsqlCommand, string?>? commandSpanNameProvider) |
| 66 | + { |
| 67 | + _commandSpanNameProvider = commandSpanNameProvider; |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Configures a callback that provides the tracing span's name for an <see cref="NpgsqlBatch"/>. If <c>null</c>, the default standard |
| 73 | + /// span name is used, which is the database name. |
| 74 | + /// </summary> |
| 75 | + public NpgsqlTracingOptionsBuilder ConfigureBatchSpanNameProvider(Func<NpgsqlBatch, string?>? batchSpanNameProvider) |
| 76 | + { |
| 77 | + _batchSpanNameProvider = batchSpanNameProvider; |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Gets or sets a value indicating whether to enable the "time-to-first-read" event. |
| 83 | + /// Default is true to preserve existing behavior. |
| 84 | + /// </summary> |
| 85 | + public NpgsqlTracingOptionsBuilder EnableFirstResponseEvent(bool enable = true) |
| 86 | + { |
| 87 | + _enableFirstResponseEvent = enable; |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + internal NpgsqlTracingOptions Build() => new() |
| 92 | + { |
| 93 | + CommandFilter = _commandFilter, |
| 94 | + BatchFilter = _batchFilter, |
| 95 | + CommandEnrichmentCallback = _commandEnrichmentCallback, |
| 96 | + BatchEnrichmentCallback = _batchEnrichmentCallback, |
| 97 | + CommandSpanNameProvider = _commandSpanNameProvider, |
| 98 | + BatchSpanNameProvider = _batchSpanNameProvider, |
| 99 | + EnableFirstResponseEvent = _enableFirstResponseEvent |
| 100 | + }; |
| 101 | +} |
| 102 | + |
| 103 | +sealed class NpgsqlTracingOptions |
| 104 | +{ |
| 105 | + internal Func<NpgsqlCommand, bool>? CommandFilter { get; init; } |
| 106 | + internal Func<NpgsqlBatch, bool>? BatchFilter { get; init; } |
| 107 | + internal Action<Activity, NpgsqlCommand>? CommandEnrichmentCallback { get; init; } |
| 108 | + internal Action<Activity, NpgsqlBatch>? BatchEnrichmentCallback { get; init; } |
| 109 | + internal Func<NpgsqlCommand, string?>? CommandSpanNameProvider { get; init; } |
| 110 | + internal Func<NpgsqlBatch, string?>? BatchSpanNameProvider { get; init; } |
| 111 | + internal bool EnableFirstResponseEvent { get; init; } |
| 112 | +} |
0 commit comments