using System;
using System.Diagnostics;
namespace Npgsql;
///
/// A builder to configure Npgsql's support for OpenTelemetry tracing.
///
public sealed class NpgsqlTracingOptionsBuilder
{
Func? _commandFilter;
Func? _batchFilter;
Action? _commandEnrichmentCallback;
Action? _batchEnrichmentCallback;
Func? _commandSpanNameProvider;
Func? _batchSpanNameProvider;
bool _enableFirstResponseEvent = true;
bool _enablePhysicalOpenTracing = true;
Func? _copyOperationFilter;
Action? _copyOperationEnrichmentCallback;
Func? _copyOperationSpanNameProvider;
internal NpgsqlTracingOptionsBuilder()
{
}
///
/// Configures a filter function that determines whether to emit tracing information for an .
/// By default, tracing information is emitted for all commands.
///
public NpgsqlTracingOptionsBuilder ConfigureCommandFilter(Func? commandFilter)
{
_commandFilter = commandFilter;
return this;
}
///
/// Configures a filter function that determines whether to emit tracing information for an .
/// By default, tracing information is emitted for all batches.
///
public NpgsqlTracingOptionsBuilder ConfigureBatchFilter(Func? batchFilter)
{
_batchFilter = batchFilter;
return this;
}
///
/// Configures a callback that can enrich the emitted for the given .
///
public NpgsqlTracingOptionsBuilder ConfigureCommandEnrichmentCallback(Action? commandEnrichmentCallback)
{
_commandEnrichmentCallback = commandEnrichmentCallback;
return this;
}
///
/// Configures a callback that can enrich the emitted for the given .
///
public NpgsqlTracingOptionsBuilder ConfigureBatchEnrichmentCallback(Action? batchEnrichmentCallback)
{
_batchEnrichmentCallback = batchEnrichmentCallback;
return this;
}
///
/// Configures a callback that provides the tracing span's name for an . If null, the default standard
/// span name is used, which is the database name.
///
public NpgsqlTracingOptionsBuilder ConfigureCommandSpanNameProvider(Func? commandSpanNameProvider)
{
_commandSpanNameProvider = commandSpanNameProvider;
return this;
}
///
/// Configures a callback that provides the tracing span's name for an . If null, the default standard
/// span name is used, which is the database name.
///
public NpgsqlTracingOptionsBuilder ConfigureBatchSpanNameProvider(Func? batchSpanNameProvider)
{
_batchSpanNameProvider = batchSpanNameProvider;
return this;
}
///
/// Gets or sets a value indicating whether to enable the "time-to-first-read" event.
/// Default is true to preserve existing behavior.
///
public NpgsqlTracingOptionsBuilder EnableFirstResponseEvent(bool enable = true)
{
_enableFirstResponseEvent = enable;
return this;
}
///
/// Gets or sets a value indicating whether to trace physical connection open.
/// Default is true to preserve existing behavior.
///
public NpgsqlTracingOptionsBuilder EnablePhysicalOpenTracing(bool enable = true)
{
_enablePhysicalOpenTracing = enable;
return this;
}
///
/// 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.
///
public NpgsqlTracingOptionsBuilder ConfigureCopyOperationFilter(Func? copyOperationFilter)
{
_copyOperationFilter = copyOperationFilter;
return this;
}
///
/// Configures a callback that can enrich the emitted for a given copy operation.
///
public NpgsqlTracingOptionsBuilder ConfigureCopyOperationEnrichmentCallback(Action? copyOperationEnrichmentCallback)
{
_copyOperationEnrichmentCallback = copyOperationEnrichmentCallback;
return this;
}
///
/// Configures a callback that provides the tracing span's name for a copy operation. If null, the default standard
/// span name is used, which is the database name.
///
public NpgsqlTracingOptionsBuilder ConfigureCopyOperationSpanNameProvider(Func? 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? CommandFilter { get; init; }
internal Func? BatchFilter { get; init; }
internal Action? CommandEnrichmentCallback { get; init; }
internal Action? BatchEnrichmentCallback { get; init; }
internal Func? CommandSpanNameProvider { get; init; }
internal Func? BatchSpanNameProvider { get; init; }
internal bool EnableFirstResponseEvent { get; init; }
internal bool EnablePhysicalOpenTracing { get; init; }
internal Func? CopyOperationFilter { get; init; }
internal Action? CopyOperationEnrichmentCallback { get; init; }
internal Func? CopyOperationSpanNameProvider { get; init; }
}