Skip to content

Commit 85d23bf

Browse files
authored
Drop dependency on trillian/monitoring (#117)
* migrate monitoring to prometheus # Conflicts: # internal/scti/handlers_test.go # Conflicts: # internal/scti/handlers.go * add metrics for the last SCT index # Conflicts: # internal/scti/handlers.go # Conflicts: # internal/scti/handlers.go * add TODO
1 parent b09d230 commit 85d23bf

File tree

2 files changed

+56
-25
lines changed

2 files changed

+56
-25
lines changed

internal/scti/handlers.go

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import (
3030

3131
"github.com/google/certificate-transparency-go/tls"
3232
"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"
3435
"github.com/transparency-dev/static-ct/modules/dedup"
3536
tessera "github.com/transparency-dev/trillian-tessera"
3637
"github.com/transparency-dev/trillian-tessera/ctonly"
@@ -62,20 +63,53 @@ var (
6263
// Metrics are all per-log (label "origin"), but may also be
6364
// per-entrypoint (label "ep") or per-return-code (label "rc").
6465
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
7072
)
7173

7274
// 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"})
79113
}
80114

81115
// 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) {
100134
var statusCode int
101135
label0 := a.log.origin
102136
label1 := string(a.name)
103-
reqsCounter.Inc(label0, label1)
137+
reqsCounter.WithLabelValues(label0, label1).Inc()
104138
startTime := a.opts.TimeSource.Now()
105139
logCtx := a.opts.RequestLog.start(r.Context())
106140
a.opts.RequestLog.origin(logCtx, a.log.origin)
107141
defer func() {
108142
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)
110144
}()
111145
klog.V(2).Infof("%s: request %v %q => %s", a.log.origin, r.Method, r.URL, a.name)
112146
// 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) {
135169
statusCode, err = a.handler(ctx, a.opts, a.log, w, r)
136170
a.opts.RequestLog.status(ctx, statusCode)
137171
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()
139173
if err != nil {
140174
klog.Warningf("%s: %s handler error: %v", a.log.origin, a.name, err)
141175
a.opts.sendHTTPError(w, statusCode, err)
@@ -154,8 +188,6 @@ func (a appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
154188
type HandlerOptions struct {
155189
// Deadline is a timeout for HTTP requests.
156190
Deadline time.Duration
157-
// MetricFactory allows creating metrics.
158-
MetricFactory monitoring.MetricFactory
159191
// RequestLog provides structured logging of CTFE requests.
160192
RequestLog requestLog
161193
// MaskInternalErrors indicates if internal server errors should be masked
@@ -166,8 +198,8 @@ type HandlerOptions struct {
166198
}
167199

168200
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)
171203

172204
prefix := strings.TrimRight(log.origin, "/")
173205

@@ -314,7 +346,8 @@ func addChainInternal(ctx context.Context, opts *HandlerOptions, log *log, w htt
314346
}
315347
klog.V(3).Infof("%s: %s <= SCT", log.origin, method)
316348
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))
318351
}
319352

320353
return http.StatusOK, nil

internal/scti/handlers_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"github.com/google/certificate-transparency-go/x509util"
3535
"github.com/google/go-cmp/cmp"
3636
"github.com/google/go-cmp/cmp/cmpopts"
37-
"github.com/google/trillian/monitoring"
3837
"github.com/transparency-dev/static-ct/internal/testdata"
3938
"github.com/transparency-dev/static-ct/mockstorage"
4039
"github.com/transparency-dev/static-ct/modules/dedup"
@@ -96,10 +95,9 @@ func setupTest(t *testing.T, pemRoots []string, signer crypto.Signer) handlerTes
9695
}
9796

9897
hOpts := HandlerOptions{
99-
Deadline: time.Millisecond * 500,
100-
MetricFactory: monitoring.InertMetricFactory{},
101-
RequestLog: new(DefaultRequestLog),
102-
TimeSource: fakeTimeSource,
98+
Deadline: time.Millisecond * 500,
99+
RequestLog: new(DefaultRequestLog),
100+
TimeSource: fakeTimeSource,
103101
}
104102
signSCT := func(leaf *ct.MerkleTreeLeaf) (*ct.SignedCertificateTimestamp, error) {
105103
return buildV1SCT(signer, leaf)

0 commit comments

Comments
 (0)