-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
163 lines (148 loc) · 4.79 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
package caskadht
import (
"context"
"net/http"
"net/http/pprof"
"runtime"
"time"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/sdk/metric"
)
const (
meterName = "ipni/caskadht"
meterLookupRespTTFP = meterName + "/lookup_response_first_provider_time"
meterLookupRespResultCount = meterName + "/lookup_response_result_count"
meterLookupRespLatency = meterName + "/lookup_response_latency"
meterLookupReqCount = meterName + "/lookup_request_count"
)
var meterScope = instrumentation.Scope{Name: meterName}
type metrics struct {
c *Caskadht
server *http.Server
exporter *prometheus.Exporter
lookupRequestCounter instrument.Int64Counter
lookupResponseTTFPHistogram instrument.Int64Histogram
lookupResponseResultCountHistogram instrument.Int64Histogram
lookupResponseLatencyHistogram instrument.Int64Histogram
}
func newMetrics(c *Caskadht) (*metrics, error) {
m := metrics{
c: c,
server: &http.Server{
Addr: c.metricsHttpListenAddr,
// TODO add other metrics server options.
},
}
return &m, nil
}
func (m *metrics) Start(_ context.Context) error {
var err error
if m.exporter, err = prometheus.New(
prometheus.WithoutUnits(),
prometheus.WithoutScopeInfo(),
prometheus.WithoutTargetInfo()); err != nil {
return err
}
provider := metric.NewMeterProvider(
metric.WithReader(m.exporter),
metric.WithView(
metric.NewView(
metric.Instrument{Name: meterLookupRespLatency, Scope: meterScope},
metric.Stream{
Aggregation: aggregation.ExplicitBucketHistogram{
Boundaries: []float64{0, 50, 100, 200, 500, 1000, 5_000, 10_000, 20_000, 30_000, 60_000},
},
},
),
metric.NewView(
metric.Instrument{Name: meterLookupRespTTFP, Scope: meterScope},
metric.Stream{
Aggregation: aggregation.ExplicitBucketHistogram{
Boundaries: []float64{0, 50, 100, 200, 300, 400, 500, 1000, 2_000, 5_000, 10_000},
},
},
),
metric.NewView(
metric.Instrument{Name: meterLookupRespResultCount, Scope: meterScope},
metric.Stream{
Aggregation: aggregation.ExplicitBucketHistogram{
Boundaries: []float64{0, 1, 2, 5, 10, 15, 20, 25, 50, 100},
},
},
),
),
)
meter := provider.Meter(meterName)
if m.lookupRequestCounter, err = meter.Int64Counter(
meterLookupReqCount,
instrument.WithUnit("1"),
instrument.WithDescription("The number of lookup requests received."),
); err != nil {
return err
}
if m.lookupResponseTTFPHistogram, err = meter.Int64Histogram(
meterLookupRespTTFP,
instrument.WithUnit("ms"),
instrument.WithDescription("The elapsed to find the first provider in milliseconds."),
); err != nil {
return err
}
if m.lookupResponseResultCountHistogram, err = meter.Int64Histogram(
meterLookupRespResultCount,
instrument.WithUnit("1"),
instrument.WithDescription("The number of providers found per lookup."),
); err != nil {
return err
}
if m.lookupResponseLatencyHistogram, err = meter.Int64Histogram(
meterLookupRespLatency,
instrument.WithUnit("ms"),
instrument.WithDescription("The lookup response latency."),
); err != nil {
return err
}
m.server.Handler = m.serveMux()
go func() { _ = m.server.ListenAndServe() }()
m.server.RegisterOnShutdown(func() {
// TODO add timeout to exporter shutdown
if err := m.exporter.Shutdown(context.TODO()); err != nil {
logger.Errorw("Failed to shut down Prometheus exporter", "err", err)
}
})
logger.Infow("Metric server started", "addr", m.server.Addr)
return nil
}
func (m *metrics) serveMux() *http.ServeMux {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
if m.c.metricsEnablePprofDebug {
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.HandleFunc("/debug/pprof/gc",
func(w http.ResponseWriter, req *http.Request) {
runtime.GC()
},
)
}
return mux
}
func (m *metrics) notifyLookupRequested(ctx context.Context) {
m.lookupRequestCounter.Add(ctx, 1)
}
func (m *metrics) notifyLookupResponded(ctx context.Context, resultCount int64, timeToFirstResult time.Duration, latency time.Duration) {
if resultCount > 0 {
m.lookupResponseTTFPHistogram.Record(ctx, timeToFirstResult.Milliseconds())
}
m.lookupResponseResultCountHistogram.Record(ctx, resultCount)
m.lookupResponseLatencyHistogram.Record(ctx, latency.Milliseconds())
}
func (m *metrics) Shutdown(ctx context.Context) error {
return m.server.Shutdown(ctx)
}