This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
forked from mgit-at/sql_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target.go
296 lines (261 loc) · 7.84 KB
/
target.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package main
import (
"context"
"database/sql"
"database/sql/driver"
"log/slog"
"reflect"
"sort"
"sync"
"time"
"github.com/imdario/mergo"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
)
const (
// Capacity for the channel to collect metrics.
capMetricChan = 1000
upMetricHelp = "if the target is reachable 1, else 0 if the scrape failed"
scrapeDurationName = "scrape_duration_seconds"
scrapeDurationHelp = "How long it took to scrape the target in seconds"
collectorStatusName = "collector_status"
collectorStatusHelp = "collector scripts status 0: error - 1: ok - 2: Invalid login 3: Timeout"
)
// Target collects SQL metrics from a single sql.DB instance. It aggregates one or more Collectors and it looks much
// like a prometheus.Collector, except its Collect() method takes a Context to run in.
type Target interface {
// Collect is the equivalent of prometheus.Collector.Collect(), but takes a context to run in.
Collect(ctx context.Context, ch chan<- Metric)
Name() string
Config() *TargetConfig
GetDeadline() time.Time
SetDeadline(time.Time)
SetSymbol(string, any) error
GetSymbolTable() map[string]any
SetLogger(*slog.Logger)
Lock()
Unlock()
}
// target implements Target. It wraps a sql.DB, which is initially nil but never changes once instantianted.
type target struct {
// name string
config *TargetConfig
// dsn string
collectors []Collector
// constLabels prometheus.Labels
globalConfig *GlobalConfig
upDesc MetricDesc
scrapeDurationDesc MetricDesc
collectorStatusDesc MetricDesc
logContext []interface{}
conn *sql.DB
logger *slog.Logger
deadline time.Time
symbols_table map[string]interface{}
// to protect the data during exchange
content_mutex *sync.Mutex
}
// NewTarget returns a new Target with the given instance name, data source name, collectors and constant labels.
// An empty target name means the exporter is running in single target mode: no synthetic metrics will be exported.
func NewTarget(
logContext []interface{},
tpar *TargetConfig,
ccs []*CollectorConfig,
constLabels prometheus.Labels,
gc *GlobalConfig,
logger *slog.Logger) (Target, error) {
if tpar.Name != "" {
logContext = append(logContext, "target", tpar.Name)
}
constLabelPairs := make([]*dto.LabelPair, 0, len(tpar.Labels))
for n, v := range constLabels {
constLabelPairs = append(constLabelPairs, &dto.LabelPair{
Name: proto.String(n),
Value: proto.String(v),
})
}
sort.Sort(labelPairSorter(constLabelPairs))
collectors := make([]Collector, 0, len(ccs))
for _, cc := range ccs {
c, err := NewCollector(logContext, logger, cc, constLabelPairs)
if err != nil {
return nil, err
}
collectors = append(collectors, c)
}
upDesc := NewAutomaticMetricDesc(
logContext,
gc.NameSpace+"_up",
upMetricHelp,
prometheus.GaugeValue,
constLabelPairs,
)
scrapeDurationDesc := NewAutomaticMetricDesc(logContext,
gc.NameSpace+"_"+scrapeDurationName,
scrapeDurationHelp,
prometheus.GaugeValue,
constLabelPairs,
)
collectorStatusDesc := NewAutomaticMetricDesc(logContext,
gc.NameSpace+"_"+collectorStatusName,
collectorStatusHelp,
prometheus.GaugeValue, constLabelPairs,
"collectorname")
symbols_table := make(map[string]interface{}, 2)
t := target{
config: tpar,
// name: tpar.Name,
// dsn: string(tpar.DSN),
collectors: collectors,
globalConfig: gc,
upDesc: upDesc,
scrapeDurationDesc: scrapeDurationDesc,
collectorStatusDesc: collectorStatusDesc,
logContext: logContext,
logger: logger,
symbols_table: symbols_table,
content_mutex: &sync.Mutex{},
}
return &t, nil
}
// Name implement Target.Name
// to obtain target name from interface
func (t *target) Name() string {
return t.config.Name
}
// Config implement Target.Name for target
// to obtain target name from interface
func (t *target) Config() *TargetConfig {
return t.config
}
// SetSymbol implement Target.SetSymbol
//
// add or update element in symbol table
//
// May be unitary key (.attribute) or sequence (.attr1.attr2.[...])
func (t *target) SetSymbol(key string, value any) error {
symtab := t.symbols_table
if r_val, ok := symtab[key]; ok {
vDst := reflect.ValueOf(r_val)
if vDst.Kind() == reflect.Map {
if m_val, ok := r_val.(map[string]any); ok {
opts := mergo.WithOverride
if err := mergo.Merge(&m_val, value, opts); err != nil {
return err
}
}
} else if vDst.Kind() == reflect.Slice {
if s_val, ok := r_val.([]any); ok {
opts := mergo.WithOverride
if err := mergo.Merge(&s_val, value, opts); err != nil {
return err
}
}
} else {
symtab[key] = value
}
} else {
symtab[key] = value
}
return nil
}
func (t *target) GetSymbolTable() map[string]any {
return t.symbols_table
}
// Getter for deadline
func (t *target) GetDeadline() time.Time {
return t.deadline
}
// Setter for deadline
func (t *target) SetDeadline(tt time.Time) {
t.deadline = tt
}
func (t *target) SetLogger(logger *slog.Logger) {
t.content_mutex.Lock()
t.logger = logger
t.content_mutex.Unlock()
}
func (t *target) Lock() {
t.content_mutex.Lock()
}
func (t *target) Unlock() {
t.content_mutex.Unlock()
}
// Collect implements Target.
func (t *target) Collect(ctx context.Context, ch chan<- Metric) {
var (
scrapeStart = time.Now()
targetUp = true
)
err := t.ping(ctx)
if err != nil {
ch <- NewInvalidMetric(t.logContext, err)
targetUp = false
}
if t.config.Name != "" {
// Export the target's `up` metric as early as we know what it should be.
ch <- NewMetric(t.upDesc, boolToFloat64(targetUp))
}
var wg sync.WaitGroup
// Don't bother with the collectors if target is down.
if targetUp {
wg.Add(len(t.collectors))
for _, c := range t.collectors {
// If using a single DB connection, collectors will likely run sequentially anyway. But we might have more.
go func(collector Collector) {
defer wg.Done()
collector.Collect(ctx, t.conn, t.symbols_table, ch)
}(c)
}
}
// Wait for all collectors (if any) to complete.
wg.Wait()
if t.config.Name != "" {
// And export a `scrape duration` metric once we're done scraping.
ch <- NewMetric(t.scrapeDurationDesc, float64(time.Since(scrapeStart))*1e-9)
}
}
func (t *target) ping(ctx context.Context) error {
// Create the DB handle, if necessary. It won't usually open an actual connection, so we'll need to ping afterwards.
// We cannot do this only once at creation time because the sql.Open() documentation says it "may" open an actual
// connection, so it "may" actually fail to open a handle to a DB that's initially down.
if t.conn == nil {
conn, err := OpenConnection(ctx, t.logContext, t.logger, string(t.config.DSN),
t.config.AuthConfig,
t.globalConfig.MaxConns, t.globalConfig.MaxIdleConns, t.symbols_table)
if err != nil {
if err != ctx.Err() {
return ErrorWrap(t.logContext, err)
}
// if err == ctx.Err() fall through
} else {
t.conn = conn
}
}
// If we have a handle and the context is not closed, test whether the database is up.
if t.conn != nil && ctx.Err() == nil {
var err error
// Ping up to max_connections + 1 times as long as the returned error is driver.ErrBadConn, to purge the connection
// pool of bad connections. This might happen if the previous scrape timed out and in-flight queries got canceled.
for i := 0; i <= t.globalConfig.MaxConns; i++ {
if err = PingDB(ctx, t.conn); err != driver.ErrBadConn {
break
}
}
if err != nil {
return ErrorWrap(t.logContext, err)
}
}
if ctx.Err() != nil {
return ErrorWrap(t.logContext, ctx.Err())
}
return nil
}
// boolToFloat64 converts a boolean flag to a float64 value (0.0 or 1.0).
func boolToFloat64(value bool) float64 {
if value {
return 1.0
}
return 0.0
}