Skip to content

Commit 72eeae7

Browse files
committed
fixing #6 and #5
1 parent 2f6806d commit 72eeae7

File tree

7 files changed

+104
-61
lines changed

7 files changed

+104
-61
lines changed

go.mod

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,47 @@
11
module github.com/cockroachlabs/metrics-exporter
22

3-
go 1.16
4-
5-
replace internal/lib => ./internal/lib
3+
go 1.17
64

75
require (
86
github.com/NYTimes/gziphandler v1.1.1
97
github.com/cockroachdb/crlfmt v0.0.0-20210128092314-b3eff0b87c79
10-
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
11-
github.com/jackc/pgx/v4 v4.16.1 // indirect
8+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
9+
github.com/jackc/pgproto3/v2 v2.3.0
10+
github.com/jackc/pgx/v4 v4.16.1
1211
github.com/prometheus/client_golang v1.12.2
12+
github.com/prometheus/client_model v0.2.0
1313
github.com/prometheus/common v0.34.0
1414
github.com/sirupsen/logrus v1.8.1
15+
github.com/stretchr/testify v1.8.0
1516
golang.org/x/lint v0.0.0-20200302205851-738671d3881b
1617
golang.org/x/tools v0.0.0-20200923014426-f5e916c686e1
18+
google.golang.org/protobuf v1.26.0
19+
gopkg.in/yaml.v2 v2.4.0
1720
honnef.co/go/tools v0.0.1-2020.1.4
18-
internal/lib v1.0.0
21+
)
22+
23+
require (
24+
github.com/BurntSushi/toml v0.3.1 // indirect
25+
github.com/beorn7/perks v1.0.1 // indirect
26+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
27+
github.com/cockroachdb/gostdlib v1.13.0 // indirect
28+
github.com/cockroachdb/ttycolor v0.0.0-20180709150743-a1d5aaeb377d // indirect
29+
github.com/davecgh/go-spew v1.1.1 // indirect
30+
github.com/golang/protobuf v1.5.2 // indirect
31+
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
32+
github.com/jackc/pgconn v1.12.1 // indirect
33+
github.com/jackc/pgio v1.0.0 // indirect
34+
github.com/jackc/pgpassfile v1.0.0 // indirect
35+
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
36+
github.com/jackc/pgtype v1.11.0 // indirect
37+
github.com/jackc/puddle v1.2.1 // indirect
38+
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
39+
github.com/pmezard/go-difflib v1.0.0 // indirect
40+
github.com/prometheus/procfs v0.7.3 // indirect
41+
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
42+
golang.org/x/mod v0.3.0 // indirect
43+
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
44+
golang.org/x/text v0.3.7 // indirect
45+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
46+
gopkg.in/yaml.v3 v3.0.1 // indirect
1947
)

go.sum

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,16 @@ github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
282282
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
283283
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
284284
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
285+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
285286
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
286287
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
287288
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
288289
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
289290
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
290-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
291291
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
292+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
293+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
294+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
292295
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
293296
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
294297
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -606,8 +609,9 @@ gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
606609
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
607610
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
608611
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
609-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
610612
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
613+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
614+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
611615
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
612616
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
613617
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

internal/lib/custom.go

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"errors"
1414
"fmt"
1515
"strconv"
16+
"strings"
1617
"time"
1718

1819
"github.com/golang/groupcache/lru"
@@ -123,6 +124,14 @@ var (
123124
}, []string{"type"})
124125
)
125126

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+
}
126135
type activity struct {
127136
time int64
128137
id string
@@ -140,38 +149,35 @@ type activity struct {
140149
rowsRead *float64
141150
}
142151

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
157158
}
158159

159160
// NewCollector creates a new collector for retrieving sql activity from the
160161
// internal CRDB tables
161162
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+
166164
var pool *pgxpool.Pool
167165
sleep := 5
168166
for {
169-
pool, err = pgxpool.ConnectConfig(ctx, poolConfig)
167+
poolConfig, err := pgxpool.ParseConfig(config.URL)
170168
if err != nil {
169+
log.Error(err)
171170
log.Warnf("Unable to connect to the db. Retrying in %d seconds", sleep)
172171
time.Sleep(time.Duration(sleep * int(time.Second)))
173172
} 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+
}
175181
}
176182
if sleep < 60 {
177183
sleep += 5
@@ -180,11 +186,21 @@ func NewCollector(ctx context.Context, config Custom) (*Collector, error) {
180186
if config.Limit == 0 {
181187
config.Limit = defaultLimit
182188
}
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+
}
183199
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,
188204
}, nil
189205
}
190206

@@ -280,18 +296,16 @@ func (c *Collector) getActivity(ctx context.Context) error {
280296

281297
labels := []string{r.id, r.app, r.database}
282298
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))
287304
}
288-
// } else {
289-
// requests.WithLabelValues(labels...).Add(float64(r.cnt))
290-
// }
291305
} else if !c.first {
292306
requests.WithLabelValues(labels...).Add(float64(r.cnt))
293307
}
294-
c.countCache.Add(key, r.cnt)
308+
c.metricsCache.Add(key, r)
295309
if r.maxDiskUsage != nil {
296310
maxDiskUsage.WithLabelValues(labels...).Set(*r.maxDiskUsage)
297311
}
@@ -320,7 +334,7 @@ func (c *Collector) getActivity(ctx context.Context) error {
320334
contTime.WithLabelValues(labels...).Set(*r.contTime)
321335
}
322336
}
323-
log.Tracef("Cache len:%d", c.countCache.Len())
337+
log.Tracef("Cache len:%d", c.metricsCache.Len())
324338
return nil
325339
}
326340

@@ -346,27 +360,27 @@ func (c *Collector) getEfficiency(ctx context.Context) error {
346360
defer rows.Close()
347361

348362
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)
351365
if err != nil {
352366
log.Tracef("getEfficiency Scan %s", err.Error())
353367
return err
354368
}
355369
log.Debugf("time:%d, explicitTotal:%d, adding: %f",
356370
time.Now().Unix(),
357-
rowArray.explicitLio,
358-
noNegVals(rowArray.explicitLio, c.rowArrayLast.explicitLio))
371+
lio.explicit,
372+
noNegVals(lio.explicit, c.logicalIOLast.explicit))
359373
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))
364378
}
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
370384
}
371385
return nil
372386
}

internal/lib/go.mod

Lines changed: 0 additions & 3 deletions
This file was deleted.

internal/lib/reader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func (r *MetricsReader) fetch(ctx context.Context) (*http.Response, error) {
3434
client := http.Client{
3535
Transport: r.Transport,
3636
}
37+
3738
req, err := http.NewRequest(http.MethodGet, r.Config.URL, nil)
3839
if err != nil {
3940
return nil, err

local.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,3 @@ bucket:
88
include: ^(exec_latency|txn_durations)$
99
custom:
1010
url: postgresql://root@localhost:26257?sslmode=disable
11-
limit: 30
12-
disablegetstatement: true

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"crypto/tls"
1515
"flag"
1616
"fmt"
17-
"internal/lib"
1817
"net/http"
1918
"os"
2019
"runtime"
@@ -23,6 +22,7 @@ import (
2322
"time"
2423

2524
"github.com/NYTimes/gziphandler"
25+
"github.com/cockroachlabs/metrics-exporter/internal/lib"
2626
"github.com/prometheus/client_golang/prometheus"
2727
"github.com/prometheus/client_golang/prometheus/promhttp"
2828
"github.com/prometheus/common/expfmt"
@@ -145,7 +145,8 @@ func main() {
145145
go func() {
146146
db, err := lib.NewCollector(ctx, config.Custom)
147147
if err != nil {
148-
log.Fatal("error connecting to the database", err)
148+
log.Error("error connecting to the database", err)
149+
return
149150
}
150151
go func() {
151152
for {

0 commit comments

Comments
 (0)