This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.go
137 lines (121 loc) · 4.05 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
package gateway
import (
"net"
"net/http"
"time"
ocprom "contrib.go.opencensus.io/exporter/prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opencensus.io/stats/view"
"go.opencensus.io/zpages"
)
// MetricsScrapingOption adds the scraping endpoint which Prometheus uses to fetch metrics.
func MetricsScrapingOption(path string) ServeOption {
return func(_ API, _ *GatewayConfig, _ 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(_ API, _ *GatewayConfig, _ 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
}
}
// MetricsCollectionOption adds collection of net/http-related metrics.
func MetricsCollectionOption(handlerName string) ServeOption {
return func(_ API, _ *GatewayConfig, _ 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,
)
)
*/