@@ -30,7 +30,8 @@ import (
30
30
31
31
"github.com/google/certificate-transparency-go/tls"
32
32
"github.com/google/certificate-transparency-go/x509"
33
- "github.com/google/trillian/monitoring"
33
+ "github.com/prometheus/client_golang/prometheus"
34
+ "github.com/prometheus/client_golang/prometheus/promauto"
34
35
"github.com/transparency-dev/static-ct/modules/dedup"
35
36
tessera "github.com/transparency-dev/trillian-tessera"
36
37
"github.com/transparency-dev/trillian-tessera/ctonly"
@@ -62,20 +63,53 @@ var (
62
63
// Metrics are all per-log (label "origin"), but may also be
63
64
// per-entrypoint (label "ep") or per-return-code (label "rc").
64
65
once sync.Once
65
- knownLogs monitoring.Gauge // origin => value (always 1.0)
66
- lastSCTTimestamp monitoring.Gauge // origin => value
67
- reqsCounter monitoring.Counter // origin, ep => value
68
- rspsCounter monitoring.Counter // origin, ep, rc => value
69
- rspLatency monitoring.Histogram // origin, ep, rc => value
66
+ knownLogs * prometheus.GaugeVec // origin => value (always 1.0)
67
+ lastSCTIndex * prometheus.GaugeVec // origin => value
68
+ lastSCTTimestamp * prometheus.GaugeVec // origin => value
69
+ reqsCounter * prometheus.CounterVec // origin, op => value
70
+ rspsCounter * prometheus.CounterVec // origin, op, code => value
71
+ rspLatency * prometheus.HistogramVec // origin, op, code => value
70
72
)
71
73
72
74
// setupMetrics initializes all the exported metrics.
73
- func setupMetrics (mf monitoring.MetricFactory ) {
74
- knownLogs = mf .NewGauge ("known_logs" , "Set to 1 for known logs" , "logid" )
75
- lastSCTTimestamp = mf .NewGauge ("last_sct_timestamp" , "Time of last SCT in ms since epoch" , "logid" )
76
- reqsCounter = mf .NewCounter ("http_reqs" , "Number of requests" , "logid" , "ep" )
77
- rspsCounter = mf .NewCounter ("http_rsps" , "Number of responses" , "logid" , "ep" , "rc" )
78
- rspLatency = mf .NewHistogram ("http_latency" , "Latency of responses in seconds" , "logid" , "ep" , "rc" )
75
+ func setupMetrics () {
76
+ // TODO(phboneff): add metrics for deduplication and chain storage.
77
+ knownLogs = promauto .NewGaugeVec (
78
+ prometheus.GaugeOpts {
79
+ Name : "known_logs" ,
80
+ Help : "Set to 1 for known logs" ,
81
+ },
82
+ []string {"origin" })
83
+ lastSCTTimestamp = promauto .NewGaugeVec (
84
+ prometheus.GaugeOpts {
85
+ Name : "last_sct_timestamp" ,
86
+ Help : "Time of last SCT in ms since epoch" ,
87
+ },
88
+ []string {"origin" })
89
+ lastSCTIndex = promauto .NewGaugeVec (
90
+ prometheus.GaugeOpts {
91
+ Name : "last_sct_index" ,
92
+ Help : "Index of last SCT" ,
93
+ },
94
+ []string {"origin" })
95
+ reqsCounter = promauto .NewCounterVec (
96
+ prometheus.CounterOpts {
97
+ Name : "http_reqs" ,
98
+ Help : "Number of requests" ,
99
+ },
100
+ []string {"origin" , "ep" })
101
+ rspsCounter = promauto .NewCounterVec (
102
+ prometheus.CounterOpts {
103
+ Name : "http_rsps" ,
104
+ Help : "Number of responses" ,
105
+ },
106
+ []string {"origin" , "op" , "code" })
107
+ rspLatency = promauto .NewHistogramVec (
108
+ prometheus.HistogramOpts {
109
+ Name : "http_latency" ,
110
+ Help : "Latency of responses in seconds" ,
111
+ },
112
+ []string {"origin" , "op" , "code" })
79
113
}
80
114
81
115
// entrypoints is a list of entrypoint names as exposed in statistics/logging.
@@ -100,13 +134,13 @@ func (a appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
100
134
var statusCode int
101
135
label0 := a .log .origin
102
136
label1 := string (a .name )
103
- reqsCounter .Inc (label0 , label1 )
137
+ reqsCounter .WithLabelValues (label0 , label1 ). Inc ( )
104
138
startTime := a .opts .TimeSource .Now ()
105
139
logCtx := a .opts .RequestLog .start (r .Context ())
106
140
a .opts .RequestLog .origin (logCtx , a .log .origin )
107
141
defer func () {
108
142
latency := a .opts .TimeSource .Now ().Sub (startTime ).Seconds ()
109
- rspLatency .Observe ( latency , label0 , label1 , strconv .Itoa (statusCode ))
143
+ rspLatency .WithLabelValues ( label0 , label1 , strconv .Itoa (statusCode )). Observe ( latency )
110
144
}()
111
145
klog .V (2 ).Infof ("%s: request %v %q => %s" , a .log .origin , r .Method , r .URL , a .name )
112
146
// TODO(phboneff): add a.Method directly on the handler path and remove this test.
@@ -135,7 +169,7 @@ func (a appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
135
169
statusCode , err = a .handler (ctx , a .opts , a .log , w , r )
136
170
a .opts .RequestLog .status (ctx , statusCode )
137
171
klog .V (2 ).Infof ("%s: %s <= st=%d" , a .log .origin , a .name , statusCode )
138
- rspsCounter .Inc (label0 , label1 , strconv .Itoa (statusCode ))
172
+ rspsCounter .WithLabelValues (label0 , label1 , strconv .Itoa (statusCode )). Inc ( )
139
173
if err != nil {
140
174
klog .Warningf ("%s: %s handler error: %v" , a .log .origin , a .name , err )
141
175
a .opts .sendHTTPError (w , statusCode , err )
@@ -154,8 +188,6 @@ func (a appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
154
188
type HandlerOptions struct {
155
189
// Deadline is a timeout for HTTP requests.
156
190
Deadline time.Duration
157
- // MetricFactory allows creating metrics.
158
- MetricFactory monitoring.MetricFactory
159
191
// RequestLog provides structured logging of CTFE requests.
160
192
RequestLog requestLog
161
193
// MaskInternalErrors indicates if internal server errors should be masked
@@ -166,8 +198,8 @@ type HandlerOptions struct {
166
198
}
167
199
168
200
func NewPathHandlers (opts * HandlerOptions , log * log ) pathHandlers {
169
- once .Do (func () { setupMetrics (opts . MetricFactory ) })
170
- knownLogs .Set (1.0 , log . origin )
201
+ once .Do (func () { setupMetrics () })
202
+ knownLogs .WithLabelValues ( log . origin ). Set (1.0 )
171
203
172
204
prefix := strings .TrimRight (log .origin , "/" )
173
205
@@ -314,7 +346,8 @@ func addChainInternal(ctx context.Context, opts *HandlerOptions, log *log, w htt
314
346
}
315
347
klog .V (3 ).Infof ("%s: %s <= SCT" , log .origin , method )
316
348
if sct .Timestamp == timeMillis {
317
- lastSCTTimestamp .Set (float64 (sct .Timestamp ), log .origin )
349
+ lastSCTTimestamp .WithLabelValues (log .origin ).Set (float64 (sct .Timestamp ))
350
+ lastSCTIndex .WithLabelValues (log .origin ).Set (float64 (idx ))
318
351
}
319
352
320
353
return http .StatusOK , nil
0 commit comments