-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
metrics.go
202 lines (177 loc) · 6.01 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package corehttp
import (
"net"
"net/http"
"time"
core "github.com/ipfs/kubo/core"
"go.opencensus.io/stats/view"
"go.opencensus.io/zpages"
ocprom "contrib.go.opencensus.io/exporter/prometheus"
prometheus "github.com/prometheus/client_golang/prometheus"
promhttp "github.com/prometheus/client_golang/prometheus/promhttp"
)
// MetricsScrapingOption adds the scraping endpoint which Prometheus uses to fetch metrics.
func MetricsScrapingOption(path string) ServeOption {
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
mux.Handle(path, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
return mux, nil
}
}
// This adds collection of OpenCensus metrics
func MetricsOpenCensusCollectionOption() ServeOption {
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
log.Info("Init OpenCensus")
promRegistry := prometheus.NewRegistry()
pe, err := ocprom.NewExporter(ocprom.Options{
Namespace: "ipfs_oc",
Registry: promRegistry,
OnError: func(err error) {
log.Errorw("OC ERROR", "error", err)
},
})
if err != nil {
return nil, err
}
// register prometheus with opencensus
view.RegisterExporter(pe)
view.SetReportingPeriod(2 * time.Second)
// Construct the mux
zpages.Handle(mux, "/debug/metrics/oc/debugz")
mux.Handle("/debug/metrics/oc", pe)
return mux, nil
}
}
// MetricsOpenCensusDefaultPrometheusRegistry registers the default prometheus
// registry as an exporter to OpenCensus metrics. This means that OpenCensus
// metrics will show up in the prometheus metrics endpoint
func MetricsOpenCensusDefaultPrometheusRegistry() ServeOption {
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
log.Info("Init OpenCensus with default prometheus registry")
pe, err := ocprom.NewExporter(ocprom.Options{
Registry: prometheus.DefaultRegisterer.(*prometheus.Registry),
OnError: func(err error) {
log.Errorw("OC default registry ERROR", "error", err)
},
})
if err != nil {
return nil, err
}
// register prometheus with opencensus
view.RegisterExporter(pe)
return mux, nil
}
}
// MetricsCollectionOption adds collection of net/http-related metrics.
func MetricsCollectionOption(handlerName string) ServeOption {
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
// Adapted from github.com/prometheus/client_golang/prometheus/http.go
// Work around https://github.com/prometheus/client_golang/pull/311
opts := prometheus.SummaryOpts{
Namespace: "ipfs",
Subsystem: "http",
ConstLabels: prometheus.Labels{"handler": handlerName},
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}
reqCnt := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: opts.Namespace,
Subsystem: opts.Subsystem,
Name: "requests_total",
Help: "Total number of HTTP requests made.",
ConstLabels: opts.ConstLabels,
},
[]string{"method", "code"},
)
if err := prometheus.Register(reqCnt); err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
reqCnt = are.ExistingCollector.(*prometheus.CounterVec)
} else {
return nil, err
}
}
opts.Name = "request_duration_seconds"
opts.Help = "The HTTP request latencies in seconds."
reqDur := prometheus.NewSummaryVec(opts, nil)
if err := prometheus.Register(reqDur); err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
reqDur = are.ExistingCollector.(*prometheus.SummaryVec)
} else {
return nil, err
}
}
opts.Name = "request_size_bytes"
opts.Help = "The HTTP request sizes in bytes."
reqSz := prometheus.NewSummaryVec(opts, nil)
if err := prometheus.Register(reqSz); err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
reqSz = are.ExistingCollector.(*prometheus.SummaryVec)
} else {
return nil, err
}
}
opts.Name = "response_size_bytes"
opts.Help = "The HTTP response sizes in bytes."
resSz := prometheus.NewSummaryVec(opts, nil)
if err := prometheus.Register(resSz); err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
resSz = are.ExistingCollector.(*prometheus.SummaryVec)
} else {
return nil, err
}
}
// Construct the mux
childMux := http.NewServeMux()
var promMux http.Handler = childMux
promMux = promhttp.InstrumentHandlerResponseSize(resSz, promMux)
promMux = promhttp.InstrumentHandlerRequestSize(reqSz, promMux)
promMux = promhttp.InstrumentHandlerDuration(reqDur, promMux)
promMux = promhttp.InstrumentHandlerCounter(reqCnt, promMux)
mux.Handle("/", promMux)
return childMux, nil
}
}
var peersTotalMetric = prometheus.NewDesc(
prometheus.BuildFQName("ipfs", "p2p", "peers_total"),
"Number of connected peers",
[]string{"transport"},
nil,
)
type IpfsNodeCollector struct {
Node *core.IpfsNode
}
func (IpfsNodeCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- peersTotalMetric
}
func (c IpfsNodeCollector) Collect(ch chan<- prometheus.Metric) {
for tr, val := range c.PeersTotalValues() {
ch <- prometheus.MustNewConstMetric(
peersTotalMetric,
prometheus.GaugeValue,
val,
tr,
)
}
}
func (c IpfsNodeCollector) PeersTotalValues() map[string]float64 {
vals := make(map[string]float64)
if c.Node.PeerHost == nil {
return vals
}
for _, peerID := range c.Node.PeerHost.Network().Peers() {
// Each peer may have more than one connection (see for an explanation
// https://github.com/libp2p/go-libp2p-swarm/commit/0538806), so we grab
// only one, the first (an arbitrary and non-deterministic choice), which
// according to ConnsToPeer is the oldest connection in the list
// (https://github.com/libp2p/go-libp2p-swarm/blob/v0.2.6/swarm.go#L362-L364).
conns := c.Node.PeerHost.Network().ConnsToPeer(peerID)
if len(conns) == 0 {
continue
}
tr := ""
for _, proto := range conns[0].RemoteMultiaddr().Protocols() {
tr = tr + "/" + proto.Name
}
vals[tr] = vals[tr] + 1
}
return vals
}