-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathoptions.go
153 lines (128 loc) · 4.82 KB
/
options.go
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
package otelpgx
import (
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)
// Option specifies instrumentation configuration options.
type Option interface {
apply(*tracerConfig)
}
type optionFunc func(*tracerConfig)
func (o optionFunc) apply(c *tracerConfig) {
o(c)
}
// WithTracerProvider specifies a tracer provider to use for creating a tracer.
// If none is specified, the global provider is used.
func WithTracerProvider(provider trace.TracerProvider) Option {
return optionFunc(func(cfg *tracerConfig) {
if provider != nil {
cfg.tracerProvider = provider
}
})
}
// WithMeterProvider specifies a meter provider to use for creating a meter.
// If none is specified, the global provider is used.
func WithMeterProvider(provider metric.MeterProvider) Option {
return optionFunc(func(cfg *tracerConfig) {
if provider != nil {
cfg.meterProvider = provider
}
})
}
// Deprecated: Use WithTracerAttributes.
//
// WithAttributes specifies additional attributes to be added to spans.
// This is exactly equivalent to using WithTracerAttributes.
func WithAttributes(attrs ...attribute.KeyValue) Option {
return WithTracerAttributes(attrs...)
}
// WithTracerAttributes specifies additional attributes to be added to spans.
func WithTracerAttributes(attrs ...attribute.KeyValue) Option {
return optionFunc(func(cfg *tracerConfig) {
cfg.tracerAttrs = append(cfg.tracerAttrs, attrs...)
})
}
// WithMeterAttributes specifies additional attributes to be added to metrics.
func WithMeterAttributes(attrs ...attribute.KeyValue) Option {
return optionFunc(func(cfg *tracerConfig) {
cfg.meterAttrs = append(cfg.meterAttrs, attrs...)
})
}
// WithTrimSQLInSpanName will use the SQL statement's first word as the span
// name. By default, the whole SQL statement is used as a span name, where
// applicable.
func WithTrimSQLInSpanName() Option {
return optionFunc(func(cfg *tracerConfig) {
cfg.trimQuerySpanName = true
})
}
// SpanNameFunc is a function that can be used to generate a span name for a
// SQL. The function will be called with the SQL statement as a parameter.
type SpanNameFunc func(stmt string) string
// WithSpanNameFunc will use the provided function to generate the span name for
// a SQL statement. The function will be called with the SQL statement as a
// parameter.
//
// By default, the whole SQL statement is used as a span name, where applicable.
func WithSpanNameFunc(fn SpanNameFunc) Option {
return optionFunc(func(cfg *tracerConfig) {
cfg.spanNameFunc = fn
})
}
// WithDisableQuerySpanNamePrefix will disable the default prefix for the span
// name. By default, the span name is prefixed with "batch query" or "query".
func WithDisableQuerySpanNamePrefix() Option {
return optionFunc(func(cfg *tracerConfig) {
cfg.prefixQuerySpanName = false
})
}
// WithDisableSQLStatementInAttributes will disable logging the SQL statement in the span's
// attributes.
func WithDisableSQLStatementInAttributes() Option {
return optionFunc(func(cfg *tracerConfig) {
cfg.logSQLStatement = false
})
}
// WithIncludeQueryParameters includes the SQL query parameters in the span attribute with key pgx.query.parameters.
// This is implicitly disabled if WithDisableSQLStatementInAttributes is used.
func WithIncludeQueryParameters() Option {
return optionFunc(func(cfg *tracerConfig) {
cfg.includeParams = true
})
}
// StatsOption allows for managing RecordStats configuration using functional options.
type StatsOption interface {
applyStatsOptions(o *statsOptions)
}
type statsOptions struct {
// meterProvider sets the metric.MeterProvider. If nil, the global Provider will be used.
meterProvider metric.MeterProvider
// minimumReadDBStatsInterval sets the minimum interval between calls to db.Stats(). Negative values are ignored.
minimumReadDBStatsInterval time.Duration
// defaultAttributes will be set to each metrics as default.
defaultAttributes []attribute.KeyValue
}
type statsOptionFunc func(o *statsOptions)
func (f statsOptionFunc) applyStatsOptions(o *statsOptions) {
f(o)
}
// WithStatsMeterProvider sets meter provider to use for pgx stat metric collection.
func WithStatsMeterProvider(provider metric.MeterProvider) StatsOption {
return statsOptionFunc(func(o *statsOptions) {
o.meterProvider = provider
})
}
// WithStatsAttributes specifies additional attributes to be added to pgx stat metrics.
func WithStatsAttributes(attrs ...attribute.KeyValue) StatsOption {
return statsOptionFunc(func(o *statsOptions) {
o.defaultAttributes = append(o.defaultAttributes, attrs...)
})
}
// WithMinimumReadDBStatsInterval sets the minimum interval between calls to db.Stats(). Negative values are ignored.
func WithMinimumReadDBStatsInterval(interval time.Duration) StatsOption {
return statsOptionFunc(func(o *statsOptions) {
o.minimumReadDBStatsInterval = interval
})
}