Skip to content

Commit 0e73e23

Browse files
authored
Merge pull request #8332 from ipfs/feat/first-block-metric
feat: register first block metric by default
2 parents 64df3ea + 0282393 commit 0e73e23

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

core/corehttp/gateway_handler.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
coreiface "github.com/ipfs/interface-go-ipfs-core"
2929
ipath "github.com/ipfs/interface-go-ipfs-core/path"
3030
routing "github.com/libp2p/go-libp2p-core/routing"
31+
prometheus "github.com/prometheus/client_golang/prometheus"
3132
)
3233

3334
const (
@@ -62,6 +63,8 @@ type redirectTemplateData struct {
6263
type gatewayHandler struct {
6364
config GatewayConfig
6465
api coreiface.CoreAPI
66+
67+
unixfsGetMetric *prometheus.SummaryVec
6568
}
6669

6770
// StatusResponseWriter enables us to override HTTP Status Code passed to
@@ -84,9 +87,27 @@ func (sw *statusResponseWriter) WriteHeader(code int) {
8487
}
8588

8689
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+
87107
i := &gatewayHandler{
88-
config: c,
89-
api: api,
108+
config: c,
109+
api: api,
110+
unixfsGetMetric: unixfsGetMetric,
90111
}
91112
return i
92113
}
@@ -271,7 +292,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
271292
return
272293
}
273294

274-
unixfsGetMetric.WithLabelValues(parsedPath.Namespace()).Observe(time.Since(begin).Seconds())
295+
i.unixfsGetMetric.WithLabelValues(parsedPath.Namespace()).Observe(time.Since(begin).Seconds())
275296

276297
defer dr.Close()
277298

core/corehttp/metrics.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
promhttp "github.com/prometheus/client_golang/prometheus/promhttp"
1515
)
1616

17-
// This adds the scraping endpoint which Prometheus uses to fetch metrics.
17+
// MetricsScrapingOption adds the scraping endpoint which Prometheus uses to fetch metrics.
1818
func MetricsScrapingOption(path string) ServeOption {
1919
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
2020
mux.Handle(path, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
@@ -51,7 +51,7 @@ func MetricsOpenCensusCollectionOption() ServeOption {
5151
}
5252
}
5353

54-
// This adds collection of net/http-related metrics
54+
// MetricsCollectionOption adds collection of net/http-related metrics.
5555
func MetricsCollectionOption(handlerName string) ServeOption {
5656
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
5757
// Adapted from github.com/prometheus/client_golang/prometheus/http.go
@@ -130,14 +130,10 @@ func MetricsCollectionOption(handlerName string) ServeOption {
130130
var (
131131
peersTotalMetric = prometheus.NewDesc(
132132
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+
)
141137
)
142138

143139
type IpfsNodeCollector struct {

0 commit comments

Comments
 (0)