@@ -17,16 +17,10 @@ import (
17
17
log "github.com/authzed/spicedb/internal/logging"
18
18
"github.com/authzed/spicedb/internal/middleware/usagemetrics"
19
19
"github.com/authzed/spicedb/pkg/datastore"
20
+ "github.com/authzed/spicedb/pkg/promutil"
20
21
)
21
22
22
- // RegisterTelemetryCollector registers a collector for the various pieces of
23
- // data required by SpiceDB telemetry.
24
- func RegisterTelemetryCollector (datastoreEngine string , ds datastore.Datastore ) (* prometheus.Registry , error ) {
25
- registry := prometheus .NewRegistry ()
26
-
27
- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
28
- defer cancel ()
29
-
23
+ func SpiceDBClusterInfoCollector (ctx context.Context , subsystem , dsEngine string , ds datastore.Datastore ) (promutil.CollectorFunc , error ) {
30
24
nodeID , err := os .Hostname ()
31
25
if err != nil {
32
26
return nil , fmt .Errorf ("unable to get hostname: %w" , err )
@@ -43,10 +37,9 @@ func RegisterTelemetryCollector(datastoreEngine string, ds datastore.Datastore)
43
37
return nil , fmt .Errorf ("failed to read BuildInfo" )
44
38
}
45
39
46
- if err := registry .Register (& collector {
47
- ds : ds ,
48
- infoDesc : prometheus .NewDesc (
49
- prometheus .BuildFQName ("spicedb" , "telemetry" , "info" ),
40
+ return func (ch chan <- prometheus.Metric ) {
41
+ ch <- prometheus .MustNewConstMetric (prometheus .NewDesc (
42
+ prometheus .BuildFQName ("spicedb" , subsystem , "info" ),
50
43
"Information about the SpiceDB environment." ,
51
44
nil ,
52
45
prometheus.Labels {
@@ -57,9 +50,42 @@ func RegisterTelemetryCollector(datastoreEngine string, ds datastore.Datastore)
57
50
"arch" : runtime .GOARCH ,
58
51
"go" : buildInfo .GoVersion ,
59
52
"vcpu" : strconv .Itoa (runtime .NumCPU ()),
60
- "ds_engine" : datastoreEngine ,
53
+ "ds_engine" : dsEngine ,
61
54
},
62
- ),
55
+ ), prometheus .GaugeValue , 1 )
56
+ }, nil
57
+ }
58
+
59
+ // RegisterTelemetryCollector registers a collector for the various pieces of
60
+ // data required by SpiceDB telemetry.
61
+ func RegisterTelemetryCollector (datastoreEngine string , ds datastore.Datastore ) (* prometheus.Registry , error ) {
62
+ registry := prometheus .NewRegistry ()
63
+
64
+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
65
+ defer cancel ()
66
+
67
+ infoCollector , err := SpiceDBClusterInfoCollector (ctx , "telemetry" , datastoreEngine , ds )
68
+ if err != nil {
69
+ return nil , fmt .Errorf ("unable create info collector: %w" , err )
70
+ }
71
+
72
+ if err := registry .Register (infoCollector ); err != nil {
73
+ return nil , fmt .Errorf ("unable to register telemetry collector: %w" , err )
74
+ }
75
+
76
+ nodeID , err := os .Hostname ()
77
+ if err != nil {
78
+ return nil , fmt .Errorf ("unable to get hostname: %w" , err )
79
+ }
80
+
81
+ dbStats , err := ds .Statistics (ctx )
82
+ if err != nil {
83
+ return nil , fmt .Errorf ("unable to query DB stats: %w" , err )
84
+ }
85
+ clusterID := dbStats .UniqueID
86
+
87
+ if err := registry .Register (& collector {
88
+ ds : ds ,
63
89
objectDefsDesc : prometheus .NewDesc (
64
90
prometheus .BuildFQName ("spicedb" , "telemetry" , "object_definitions_total" ),
65
91
"Count of the number of objects defined by the schema." ,
@@ -96,7 +122,6 @@ func RegisterTelemetryCollector(datastoreEngine string, ds datastore.Datastore)
96
122
97
123
type collector struct {
98
124
ds datastore.Datastore
99
- infoDesc * prometheus.Desc
100
125
objectDefsDesc * prometheus.Desc
101
126
relationshipsDesc * prometheus.Desc
102
127
dispatchedDesc * prometheus.Desc
@@ -105,7 +130,6 @@ type collector struct {
105
130
var _ prometheus.Collector = & collector {}
106
131
107
132
func (c * collector ) Describe (ch chan <- * prometheus.Desc ) {
108
- ch <- c .infoDesc
109
133
ch <- c .objectDefsDesc
110
134
ch <- c .relationshipsDesc
111
135
ch <- c .dispatchedDesc
@@ -120,7 +144,6 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
120
144
log .Warn ().Err (err ).Msg ("unable to collect datastore statistics" )
121
145
}
122
146
123
- ch <- prometheus .MustNewConstMetric (c .infoDesc , prometheus .GaugeValue , 1 )
124
147
ch <- prometheus .MustNewConstMetric (c .objectDefsDesc , prometheus .GaugeValue , float64 (len (dsStats .ObjectTypeStatistics )))
125
148
ch <- prometheus .MustNewConstMetric (c .relationshipsDesc , prometheus .GaugeValue , float64 (dsStats .EstimatedRelationshipCount ))
126
149
0 commit comments