@cryptotaxi247 / kubo / commits / 02823935a

feat: register first block metric by default

Adrian Lanzafame committed Jul 30, 2019 at 15:14 UTC 02823935aaf5920148059eb5c58a80bd9a36e36c
2 files changed +30 -13
core/corehttp/gateway_handler.go
+24 -3
@@ -28,6 +28,7 @@ import (
28 coreiface "github.com/ipfs/interface-go-ipfs-core"
29 ipath "github.com/ipfs/interface-go-ipfs-core/path"
30 routing "github.com/libp2p/go-libp2p-core/routing"
31 + prometheus "github.com/prometheus/client_golang/prometheus"
32 )
33
34 const (
@@ -62,6 +63,8 @@ type redirectTemplateData struct {
63 type gatewayHandler struct {
64 config GatewayConfig
65 api coreiface.CoreAPI
66 +
67 + unixfsGetMetric *prometheus.SummaryVec
68 }
69
70 // StatusResponseWriter enables us to override HTTP Status Code passed to
@@ -84,9 +87,27 @@ func (sw *statusResponseWriter) WriteHeader(code int) {
87 }
88
89 func newGatewayHandler(c GatewayConfig, api coreiface.CoreAPI) *gatewayHandler {
90 + unixfsGetMetric := prometheus.NewSummaryVec(
91 + prometheus.SummaryOpts{
92 + Namespace: "ipfs",
93 + Subsystem: "http",
94 + Name: "unixfs_get_latency_seconds",
95 + Help: "The time till the first block is received when 'getting' a file from the gateway.",
96 + },
97 + []string{"gateway"},
98 + )
99 + if err := prometheus.Register(unixfsGetMetric); err != nil {
100 + if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
101 + unixfsGetMetric = are.ExistingCollector.(*prometheus.SummaryVec)
102 + } else {
103 + log.Errorf("failed to register unixfsGetMetric: %v", err)
104 + }
105 + }
106 +
107 i := &gatewayHandler{
88 - config: c,
89 - api: api,
108 + config: c,
109 + api: api,
110 + unixfsGetMetric: unixfsGetMetric,
111 }
112 return i
113 }
@@ -271,7 +292,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
292 return
293 }
294
274 - unixfsGetMetric.WithLabelValues(parsedPath.Namespace()).Observe(time.Since(begin).Seconds())
295 + i.unixfsGetMetric.WithLabelValues(parsedPath.Namespace()).Observe(time.Since(begin).Seconds())
296
297 defer dr.Close()
298
core/corehttp/metrics.go
+6 -10
@@ -14,7 +14,7 @@ import (
14 promhttp "github.com/prometheus/client_golang/prometheus/promhttp"
15 )
16
17 -// This adds the scraping endpoint which Prometheus uses to fetch metrics.
17 +// MetricsScrapingOption adds the scraping endpoint which Prometheus uses to fetch metrics.
18 func MetricsScrapingOption(path string) ServeOption {
19 return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
20 mux.Handle(path, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
@@ -51,7 +51,7 @@ func MetricsOpenCensusCollectionOption() ServeOption {
51 }
52 }
53
54 -// This adds collection of net/http-related metrics
54 +// MetricsCollectionOption adds collection of net/http-related metrics.
55 func MetricsCollectionOption(handlerName string) ServeOption {
56 return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
57 // Adapted from github.com/prometheus/client_golang/prometheus/http.go
@@ -130,14 +130,10 @@ func MetricsCollectionOption(handlerName string) ServeOption {
130 var (
131 peersTotalMetric = prometheus.NewDesc(
132 prometheus.BuildFQName("ipfs", "p2p", "peers_total"),
133 - "Number of connected peers", []string{"transport"}, nil)
134 -
135 - unixfsGetMetric = prometheus.NewSummaryVec(prometheus.SummaryOpts{
136 - Namespace: "ipfs",
137 - Subsystem: "http",
138 - Name: "unixfs_get_latency_seconds",
139 - Help: "The time till the first block is received when 'getting' a file from the gateway.",
140 - }, []string{"namespace"})
133 + "Number of connected peers",
134 + []string{"transport"},
135 + nil,
136 + )
137 )
138
139 type IpfsNodeCollector struct {