/
metrics.go
165 lines (144 loc) · 4.62 KB
/
metrics.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
package telemetry
import (
"context"
"fmt"
"os"
"runtime"
"runtime/debug"
"time"
"github.com/jzelinskie/cobrautil"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
"github.com/authzed/spicedb/internal/datastore"
"github.com/authzed/spicedb/internal/middleware/usagemetrics"
)
// RegisterTelemetryCollector registers a collector for the various pieces of
// data required by SpiceDB telemetry.
func RegisterTelemetryCollector(datastoreEngine string, ds datastore.Datastore) (*prometheus.Registry, error) {
registry := prometheus.NewRegistry()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
nodeID, err := os.Hostname()
if err != nil {
return nil, fmt.Errorf("unable to get hostname: %w", err)
}
dbStats, err := ds.Statistics(ctx)
if err != nil {
return nil, fmt.Errorf("unable to query DB stats: %w", err)
}
clusterID := dbStats.UniqueID
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
panic("failed to read build info")
}
if err := registry.Register(&collector{
ds: ds,
infoDesc: prometheus.NewDesc(
prometheus.BuildFQName("spicedb", "telemetry", "info"),
"Information about the SpiceDB environment.",
nil,
prometheus.Labels{
"cluster_id": clusterID,
"node_id": nodeID,
"version": cobrautil.VersionWithFallbacks(buildInfo),
"os": runtime.GOOS,
"arch": runtime.GOARCH,
"go": buildInfo.GoVersion,
"vcpu": fmt.Sprintf("%d", runtime.NumCPU()),
"ds_engine": datastoreEngine,
},
),
objectDefsDesc: prometheus.NewDesc(
prometheus.BuildFQName("spicedb", "telemetry", "object_definitions_total"),
"Count of the number of objects defined by the schema.",
nil,
prometheus.Labels{
"cluster_id": clusterID,
"node_id": nodeID,
},
),
relationshipsDesc: prometheus.NewDesc(
prometheus.BuildFQName("spicedb", "telemetry", "relationships_estimate_total"),
"Count of the estimated number of stored relationships.",
nil,
prometheus.Labels{
"cluster_id": clusterID,
"node_id": nodeID,
},
),
dispatchedDesc: prometheus.NewDesc(
prometheus.BuildFQName("spicedb", "telemetry", "dispatches"),
"Histogram of cluster dispatches performed by the instance.",
usagemetrics.DispatchedCountLabels,
prometheus.Labels{
"cluster_id": clusterID,
"node_id": nodeID,
},
),
}); err != nil {
return nil, fmt.Errorf("unable to register telemetry collector: %w", err)
}
return registry, nil
}
type collector struct {
ds datastore.Datastore
infoDesc *prometheus.Desc
objectDefsDesc *prometheus.Desc
relationshipsDesc *prometheus.Desc
dispatchedDesc *prometheus.Desc
}
var _ prometheus.Collector = &collector{}
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.infoDesc
ch <- c.objectDefsDesc
ch <- c.relationshipsDesc
ch <- c.dispatchedDesc
}
func (c *collector) Collect(ch chan<- prometheus.Metric) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
dsStats, err := c.ds.Statistics(ctx)
if err != nil {
log.Warn().Err(err).Msg("unable to collect datastore statistics")
}
ch <- prometheus.MustNewConstMetric(c.infoDesc, prometheus.GaugeValue, 1)
ch <- prometheus.MustNewConstMetric(c.objectDefsDesc, prometheus.GaugeValue, float64(len(dsStats.ObjectTypeStatistics)))
ch <- prometheus.MustNewConstMetric(c.relationshipsDesc, prometheus.GaugeValue, float64(dsStats.EstimatedRelationshipCount))
dispatchedCountMetrics := make(chan prometheus.Metric)
g := errgroup.Group{}
g.Go(func() error {
for metric := range dispatchedCountMetrics {
var m dto.Metric
if err := metric.Write(&m); err != nil {
return fmt.Errorf("error writing metric: %w", err)
}
buckets := make(map[float64]uint64, len(m.Histogram.Bucket))
for _, bucket := range m.Histogram.Bucket {
buckets[*bucket.UpperBound] = *bucket.CumulativeCount
}
dynamicLabels := make([]string, len(usagemetrics.DispatchedCountLabels))
for i, labelName := range usagemetrics.DispatchedCountLabels {
for _, labelVal := range m.Label {
if *labelVal.Name == labelName {
dynamicLabels[i] = *labelVal.Value
}
}
}
ch <- prometheus.MustNewConstHistogram(
c.dispatchedDesc,
*m.Histogram.SampleCount,
*m.Histogram.SampleSum,
buckets,
dynamicLabels...,
)
}
return nil
})
usagemetrics.DispatchedCountHistogram.Collect(dispatchedCountMetrics)
close(dispatchedCountMetrics)
if err := g.Wait(); err != nil {
log.Error().Err(err).Msg("error collecting metrics")
}
}