@@ -13,6 +13,7 @@ import (
13
13
"errors"
14
14
"fmt"
15
15
"strconv"
16
+ "strings"
16
17
"time"
17
18
18
19
"github.com/golang/groupcache/lru"
@@ -123,6 +124,14 @@ var (
123
124
}, []string {"type" })
124
125
)
125
126
127
+ // Collector queries the database to collect custom metrics
128
+ type Collector struct {
129
+ first bool
130
+ config Custom
131
+ pool * pgxpool.Pool
132
+ logicalIOLast logicalIO
133
+ metricsCache * lru.Cache
134
+ }
126
135
type activity struct {
127
136
time int64
128
137
id string
@@ -140,38 +149,35 @@ type activity struct {
140
149
rowsRead * float64
141
150
}
142
151
143
- // Collector queries the database to collect custom metrics
144
- type Collector struct {
145
- first bool
146
- config Custom
147
- pool * pgxpool.Pool
148
- rowArrayLast rowLioSample
149
- countCache * lru.Cache
150
- }
151
- type rowLioSample struct {
152
- lioTotal int
153
- fullLio int
154
- iJoinLio int
155
- explicitLio int
156
- healthyLio int
152
+ type logicalIO struct {
153
+ total int
154
+ full int
155
+ indexJoin int
156
+ explicit int
157
+ healthy int
157
158
}
158
159
159
160
// NewCollector creates a new collector for retrieving sql activity from the
160
161
// internal CRDB tables
161
162
func NewCollector (ctx context.Context , config Custom ) (* Collector , error ) {
162
- poolConfig , err := pgxpool .ParseConfig (config .URL )
163
- if err != nil {
164
- return nil , err
165
- }
163
+
166
164
var pool * pgxpool.Pool
167
165
sleep := 5
168
166
for {
169
- pool , err = pgxpool .ConnectConfig ( ctx , poolConfig )
167
+ poolConfig , err : = pgxpool .ParseConfig ( config . URL )
170
168
if err != nil {
169
+ log .Error (err )
171
170
log .Warnf ("Unable to connect to the db. Retrying in %d seconds" , sleep )
172
171
time .Sleep (time .Duration (sleep * int (time .Second )))
173
172
} else {
174
- break
173
+ pool , err = pgxpool .ConnectConfig (ctx , poolConfig )
174
+ if err != nil {
175
+ log .Error (err )
176
+ log .Warnf ("Unable to connect to the db. Retrying in %d seconds" , sleep )
177
+ time .Sleep (time .Duration (sleep * int (time .Second )))
178
+ } else {
179
+ break
180
+ }
175
181
}
176
182
if sleep < 60 {
177
183
sleep += 5
@@ -180,11 +186,21 @@ func NewCollector(ctx context.Context, config Custom) (*Collector, error) {
180
186
if config .Limit == 0 {
181
187
config .Limit = defaultLimit
182
188
}
189
+ countCache := lru .New (config .Limit * 2 )
190
+ countCache .OnEvicted = func (key lru.Key , value interface {}) {
191
+ switch k := key .(type ) {
192
+ case string :
193
+ deleted := requests .DeleteLabelValues (strings .Split (k , "|" )... )
194
+ if deleted {
195
+ log .Debugf ("%s removed from cache" , k )
196
+ }
197
+ }
198
+ }
183
199
return & Collector {
184
- first : true ,
185
- config : config ,
186
- pool : pool ,
187
- countCache : lru . New ( config . Limit * 4 ) ,
200
+ first : true ,
201
+ config : config ,
202
+ pool : pool ,
203
+ metricsCache : countCache ,
188
204
}, nil
189
205
}
190
206
@@ -280,18 +296,16 @@ func (c *Collector) getActivity(ctx context.Context) error {
280
296
281
297
labels := []string {r .id , r .app , r .database }
282
298
key := r .id + "|" + r .app + "|" + r .database
283
- if cached , ok := c .countCache .Get (key ); ok {
284
- lastCount := cached .(int64 )
285
- if r .cnt >= lastCount {
286
- requests .WithLabelValues (labels ... ).Add (float64 (r .cnt - lastCount ))
299
+ log .Tracef ("key:%s" , key )
300
+ if cached , ok := c .metricsCache .Get (key ); ok {
301
+ last := cached .(* activity )
302
+ if r .cnt > last .cnt {
303
+ requests .WithLabelValues (labels ... ).Add (float64 (r .cnt - last .cnt ))
287
304
}
288
- // } else {
289
- // requests.WithLabelValues(labels...).Add(float64(r.cnt))
290
- // }
291
305
} else if ! c .first {
292
306
requests .WithLabelValues (labels ... ).Add (float64 (r .cnt ))
293
307
}
294
- c .countCache .Add (key , r . cnt )
308
+ c .metricsCache .Add (key , r )
295
309
if r .maxDiskUsage != nil {
296
310
maxDiskUsage .WithLabelValues (labels ... ).Set (* r .maxDiskUsage )
297
311
}
@@ -320,7 +334,7 @@ func (c *Collector) getActivity(ctx context.Context) error {
320
334
contTime .WithLabelValues (labels ... ).Set (* r .contTime )
321
335
}
322
336
}
323
- log .Tracef ("Cache len:%d" , c .countCache .Len ())
337
+ log .Tracef ("Cache len:%d" , c .metricsCache .Len ())
324
338
return nil
325
339
}
326
340
@@ -346,27 +360,27 @@ func (c *Collector) getEfficiency(ctx context.Context) error {
346
360
defer rows .Close ()
347
361
348
362
for rows .Next () {
349
- rowArray := rowLioSample {}
350
- err := rows .Scan (& rowArray . lioTotal , & rowArray . fullLio , & rowArray . iJoinLio , & rowArray . explicitLio , & rowArray . healthyLio )
363
+ lio := & logicalIO {}
364
+ err := rows .Scan (& lio . total , & lio . full , & lio . indexJoin , & lio . explicit , & lio . healthy )
351
365
if err != nil {
352
366
log .Tracef ("getEfficiency Scan %s" , err .Error ())
353
367
return err
354
368
}
355
369
log .Debugf ("time:%d, explicitTotal:%d, adding: %f" ,
356
370
time .Now ().Unix (),
357
- rowArray . explicitLio ,
358
- noNegVals (rowArray . explicitLio , c .rowArrayLast . explicitLio ))
371
+ lio . explicit ,
372
+ noNegVals (lio . explicit , c .logicalIOLast . explicit ))
359
373
if ! c .first {
360
- stmtStats .WithLabelValues ("full" ).Add (noNegVals (rowArray . fullLio , c .rowArrayLast . fullLio ))
361
- stmtStats .WithLabelValues ("ijoin" ).Add (noNegVals (rowArray . iJoinLio , c .rowArrayLast . iJoinLio ))
362
- stmtStats .WithLabelValues ("explicit" ).Add (noNegVals (rowArray . explicitLio , c .rowArrayLast . explicitLio ))
363
- stmtStats .WithLabelValues ("optimized" ).Add (noNegVals (rowArray . healthyLio , c .rowArrayLast . healthyLio ))
374
+ stmtStats .WithLabelValues ("full" ).Add (noNegVals (lio . full , c .logicalIOLast . full ))
375
+ stmtStats .WithLabelValues ("ijoin" ).Add (noNegVals (lio . indexJoin , c .logicalIOLast . indexJoin ))
376
+ stmtStats .WithLabelValues ("explicit" ).Add (noNegVals (lio . explicit , c .logicalIOLast . explicit ))
377
+ stmtStats .WithLabelValues ("optimized" ).Add (noNegVals (lio . healthy , c .logicalIOLast . healthy ))
364
378
}
365
- c .rowArrayLast . lioTotal = rowArray . lioTotal
366
- c .rowArrayLast . fullLio = rowArray . fullLio
367
- c .rowArrayLast . iJoinLio = rowArray . iJoinLio
368
- c .rowArrayLast . explicitLio = rowArray . explicitLio
369
- c .rowArrayLast . healthyLio = rowArray . healthyLio
379
+ c .logicalIOLast . total = lio . total
380
+ c .logicalIOLast . full = lio . full
381
+ c .logicalIOLast . indexJoin = lio . indexJoin
382
+ c .logicalIOLast . explicit = lio . explicit
383
+ c .logicalIOLast . healthy = lio . healthy
370
384
}
371
385
return nil
372
386
}
0 commit comments