-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
204 lines (166 loc) · 4.64 KB
/
Copy pathoptions.go
File metadata and controls
204 lines (166 loc) · 4.64 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package xpg
import (
"errors"
"fmt"
"maps"
"net"
"strconv"
"strings"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/multitracer"
"github.com/jackc/pgx/v5/tracelog"
)
// Option configures a Pool.
//
// The interface is sealed so options can only be created by this package.
type Option interface {
apply(*settings) error
}
// WithName assigns a stable logical name to the pool.
//
// Name is metadata for diagnostics and observability. It does not change the
// PostgreSQL application_name runtime parameter.
func WithName(name string) Option {
name = strings.TrimSpace(name)
return optionFunc(func(settings *settings) error {
if name == "" {
return errors.New("pool name must not be blank")
}
settings.name = name
return nil
})
}
// WithLabel adds or replaces one pool label.
func WithLabel(key, value string) Option {
return optionFunc(func(settings *settings) error {
if key == "" {
return errors.New("label key must not be empty")
}
settings.labels[key] = value
return nil
})
}
// WithLabels merges labels into the pool metadata.
//
// Labels are defensively copied. When the same key is configured more than
// once, the last value wins.
func WithLabels(labels map[string]string) Option {
labels = cloneLabels(labels)
return optionFunc(func(settings *settings) error {
for key, value := range labels {
if key == "" {
return errors.New("label key must not be empty")
}
settings.labels[key] = value
}
return nil
})
}
// WithLogger attaches a pgx-compatible logger to the pool.
//
// Logging uses pgx tracelog and participates in the same tracing pipeline as
// tracers configured through xpg. pgx tracelog may include SQL text and query
// arguments in log records; applications are responsible for choosing an
// appropriate level and handling sensitive values.
func WithLogger(logger tracelog.Logger, level tracelog.LogLevel) Option {
return optionFunc(func(settings *settings) error {
if logger == nil {
return errors.New("pool logger is nil")
}
settings.tracers = append(settings.tracers, &tracelog.TraceLog{
Logger: logger,
LogLevel: level,
})
return nil
})
}
// WithTracer attaches one pgx query tracer to the pool.
//
// The option may be specified multiple times. Configured loggers and tracers
// are combined through pgx multitracer. When xpg logging or tracing options are
// configured, the resulting tracing pipeline replaces any tracer already
// configured on the pgx connection config.
func WithTracer(tracer pgx.QueryTracer) Option {
return WithTracers(tracer)
}
// WithTracers attaches multiple pgx query tracers to the pool.
//
// Configured loggers and tracers are invoked in configuration order and
// combined through pgx multitracer. When xpg logging or tracing options are
// configured, the resulting tracing pipeline replaces any tracer already
// configured on the pgx connection config.
func WithTracers(tracers ...pgx.QueryTracer) Option {
return optionFunc(func(settings *settings) error {
for _, tracer := range tracers {
if tracer == nil {
return errors.New("pool tracer is nil")
}
}
settings.tracers = append(settings.tracers, tracers...)
return nil
})
}
// WithMetrics attaches one metrics implementation to the pool.
//
// Metrics are registered when the pool is created and unregistered
// automatically when the Pool is closed.
func WithMetrics(metrics Metrics) Option {
return optionFunc(func(settings *settings) error {
if metrics == nil {
return errors.New("pool metrics is nil")
}
settings.metrics = metrics
return nil
})
}
type optionFunc func(*settings) error
func (option optionFunc) apply(settings *settings) error {
return option(settings)
}
type settings struct {
name string
labels map[string]string
metrics Metrics
tracers []pgx.QueryTracer
}
func defaultSettings() *settings {
return &settings{
labels: make(map[string]string),
}
}
func applyOptions(settings *settings, opts ...Option) error {
for _, opt := range opts {
if opt == nil {
return errors.New("xpg: option is nil")
}
if err := opt.apply(settings); err != nil {
return fmt.Errorf("xpg: apply option: %w", err)
}
}
return nil
}
func (s *settings) poolName(host string, port uint16, database string) string {
if s.name != "" {
return s.name
}
address := net.JoinHostPort(
host,
strconv.Itoa(int(port)),
)
if database == "" {
return address
}
return address + "/" + database
}
func (s *settings) buildTracer() pgx.QueryTracer {
if len(s.tracers) == 0 {
return nil
}
return multitracer.New(s.tracers...)
}
func cloneLabels(labels map[string]string) map[string]string {
if len(labels) == 0 {
return nil
}
return maps.Clone(labels)
}