Skip to content

Commit 3e4291d

Browse files
committed
wip: metrics
Signed-off-by: Hank Donnay <hdonnay@redhat.com> Change-Id: Ia470ea555b62eb9c21d4069c7f596abd87e114d0
1 parent dbca7c4 commit 3e4291d

6 files changed

Lines changed: 340 additions & 54 deletions

File tree

datastore/metrics/attributes.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package metrics
2+
3+
import "go.opentelemetry.io/otel/attribute"
4+
5+
var (
6+
DeltaKey = attribute.Key("delta_update")
7+
FailedKey = attribute.Key("failed")
8+
OperationKey = attribute.Key("operation")
9+
QueryKey = attribute.Key("query")
10+
)
11+
12+
func Delta(d bool) attribute.KeyValue {
13+
return DeltaKey.Bool(d)
14+
}
15+
16+
func Failed(err error) attribute.KeyValue {
17+
return FailedKey.Bool(err != nil)
18+
}
19+
20+
func Operation(op string) attribute.KeyValue {
21+
return OperationKey.String(op)
22+
}
23+
24+
func Query(which string) attribute.KeyValue {
25+
return QueryKey.String(which)
26+
}

datastore/metrics/metrics.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Package metrics holds helpers to register datastore metrics.
2+
//
3+
// This is split out so that datastore implementations can "just" provide the
4+
// meters and have them configured uniformly.
5+
package metrics
6+
7+
import (
8+
"sync"
9+
10+
"go.opentelemetry.io/otel/metric"
11+
)
12+
13+
var (
14+
// NormalBucketBoundaries ...
15+
//
16+
// 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10
17+
NormalBucketBoundaries = sync.OnceValue(func() []float64 {
18+
return []float64{0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10}
19+
})
20+
// LongBucketBoundaries ...
21+
//
22+
// 0.05, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10, 25, 50, 75, 100
23+
LongBucketBoundaries = sync.OnceValue(func() []float64 {
24+
n := NormalBucketBoundaries()
25+
l := make([]float64, len(n))
26+
copy(l, n)
27+
for i := range l {
28+
l[i] *= 10
29+
}
30+
return l
31+
})
32+
// VeryLongBucketBoundaries ...
33+
//
34+
// 0.1, 0.2, 0.5, 1, 1.5, 2, 5, 10, 15, 20, 50, 100, 150, 200, 300
35+
VeryLongBucketBoundaries = sync.OnceValue(func() []float64 {
36+
n := NormalBucketBoundaries()
37+
v := make([]float64, len(n))
38+
copy(v, n)
39+
for i := range v {
40+
v[i] *= 20
41+
}
42+
return append(v, 300)
43+
})
44+
)
45+
46+
const Version = `0.1.0`
47+
48+
type Metrics struct {
49+
UpdateVulnerabilities struct {
50+
CallDuration metric.Float64Histogram
51+
QueryCounter metric.Int64Counter
52+
QueryDuration metric.Float64Histogram
53+
}
54+
}
55+
56+
func Init(m *Metrics, meter metric.Meter) (err error) {
57+
if err := updateVulnerabilities(m, meter); err != nil {
58+
return err
59+
}
60+
61+
return nil
62+
}
63+
64+
const (
65+
prefix = `claircore.datastore.`
66+
)
67+
68+
func updateVulnerabilities(m *Metrics, meter metric.Meter) (err error) {
69+
m.UpdateVulnerabilities.CallDuration, err = meter.Float64Histogram(
70+
prefix+"update_vulnerabilities.duration",
71+
metric.WithUnit("s"),
72+
metric.WithDescription("Duration of UpdateVulnerabilities requests."),
73+
metric.WithExplicitBucketBoundaries(NormalBucketBoundaries()...),
74+
)
75+
if err != nil {
76+
return err
77+
}
78+
79+
m.UpdateVulnerabilities.QueryCounter, err = meter.Int64Counter(
80+
prefix+"update_vulnerabilities.queries",
81+
metric.WithUnit("{query}"),
82+
metric.WithDescription("Number of UpdateVulnerabilities database queries."),
83+
)
84+
if err != nil {
85+
return err
86+
}
87+
88+
m.UpdateVulnerabilities.QueryDuration, err = meter.Float64Histogram(
89+
prefix+"update_vulnerabilities.query.duration",
90+
metric.WithUnit("s"),
91+
metric.WithDescription("Duration of UpdateVulnerabilities database queries."),
92+
metric.WithExplicitBucketBoundaries(NormalBucketBoundaries()...),
93+
)
94+
if err != nil {
95+
return err
96+
}
97+
98+
return nil
99+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package postgres
2+
3+
import (
4+
"go.opentelemetry.io/otel"
5+
"go.opentelemetry.io/otel/metric"
6+
"go.opentelemetry.io/otel/trace"
7+
8+
dsmetrics "github.com/quay/claircore/datastore/metrics"
9+
)
10+
11+
var (
12+
tracer trace.Tracer
13+
meter metric.Meter
14+
15+
metrics dsmetrics.Metrics
16+
)
17+
18+
func init() {
19+
const name = `github.com/quay/claircore/datastore/postgres`
20+
tracer = otel.Tracer(name)
21+
meter = otel.Meter(name,
22+
metric.WithInstrumentationVersion(dsmetrics.Version),
23+
)
24+
25+
if err := dsmetrics.Init(&metrics, meter); err != nil {
26+
panic(err)
27+
}
28+
}

datastore/postgres/main_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package postgres
22

33
import (
4-
"os"
54
"testing"
65

7-
"github.com/quay/claircore/test/integration"
6+
"github.com/quay/claircore/test"
87
)
98

109
func TestMain(m *testing.M) {
11-
var c int
12-
defer func() { os.Exit(c) }()
13-
defer integration.DBSetup()()
14-
c = m.Run()
10+
test.Main(m, test.DBSetup)
1511
}

0 commit comments

Comments
 (0)