mirror of
https://github.com/ipfs/kubo.git
synced 2025-08-06 19:44:01 +08:00
feat: register first block metric by default
This commit is contained in:

committed by
Gus Eggert

parent
64df3ea004
commit
02823935aa
@ -28,6 +28,7 @@ import (
|
|||||||
coreiface "github.com/ipfs/interface-go-ipfs-core"
|
coreiface "github.com/ipfs/interface-go-ipfs-core"
|
||||||
ipath "github.com/ipfs/interface-go-ipfs-core/path"
|
ipath "github.com/ipfs/interface-go-ipfs-core/path"
|
||||||
routing "github.com/libp2p/go-libp2p-core/routing"
|
routing "github.com/libp2p/go-libp2p-core/routing"
|
||||||
|
prometheus "github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -62,6 +63,8 @@ type redirectTemplateData struct {
|
|||||||
type gatewayHandler struct {
|
type gatewayHandler struct {
|
||||||
config GatewayConfig
|
config GatewayConfig
|
||||||
api coreiface.CoreAPI
|
api coreiface.CoreAPI
|
||||||
|
|
||||||
|
unixfsGetMetric *prometheus.SummaryVec
|
||||||
}
|
}
|
||||||
|
|
||||||
// StatusResponseWriter enables us to override HTTP Status Code passed to
|
// StatusResponseWriter enables us to override HTTP Status Code passed to
|
||||||
@ -84,9 +87,27 @@ func (sw *statusResponseWriter) WriteHeader(code int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newGatewayHandler(c GatewayConfig, api coreiface.CoreAPI) *gatewayHandler {
|
func newGatewayHandler(c GatewayConfig, api coreiface.CoreAPI) *gatewayHandler {
|
||||||
|
unixfsGetMetric := prometheus.NewSummaryVec(
|
||||||
|
prometheus.SummaryOpts{
|
||||||
|
Namespace: "ipfs",
|
||||||
|
Subsystem: "http",
|
||||||
|
Name: "unixfs_get_latency_seconds",
|
||||||
|
Help: "The time till the first block is received when 'getting' a file from the gateway.",
|
||||||
|
},
|
||||||
|
[]string{"gateway"},
|
||||||
|
)
|
||||||
|
if err := prometheus.Register(unixfsGetMetric); err != nil {
|
||||||
|
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
|
||||||
|
unixfsGetMetric = are.ExistingCollector.(*prometheus.SummaryVec)
|
||||||
|
} else {
|
||||||
|
log.Errorf("failed to register unixfsGetMetric: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
i := &gatewayHandler{
|
i := &gatewayHandler{
|
||||||
config: c,
|
config: c,
|
||||||
api: api,
|
api: api,
|
||||||
|
unixfsGetMetric: unixfsGetMetric,
|
||||||
}
|
}
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
@ -271,7 +292,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
unixfsGetMetric.WithLabelValues(parsedPath.Namespace()).Observe(time.Since(begin).Seconds())
|
i.unixfsGetMetric.WithLabelValues(parsedPath.Namespace()).Observe(time.Since(begin).Seconds())
|
||||||
|
|
||||||
defer dr.Close()
|
defer dr.Close()
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ import (
|
|||||||
promhttp "github.com/prometheus/client_golang/prometheus/promhttp"
|
promhttp "github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This adds the scraping endpoint which Prometheus uses to fetch metrics.
|
// MetricsScrapingOption adds the scraping endpoint which Prometheus uses to fetch metrics.
|
||||||
func MetricsScrapingOption(path string) ServeOption {
|
func MetricsScrapingOption(path string) ServeOption {
|
||||||
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
|
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
|
||||||
mux.Handle(path, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
|
mux.Handle(path, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
|
||||||
@ -51,7 +51,7 @@ func MetricsOpenCensusCollectionOption() ServeOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This adds collection of net/http-related metrics
|
// MetricsCollectionOption adds collection of net/http-related metrics.
|
||||||
func MetricsCollectionOption(handlerName string) ServeOption {
|
func MetricsCollectionOption(handlerName string) ServeOption {
|
||||||
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
|
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
|
||||||
// Adapted from github.com/prometheus/client_golang/prometheus/http.go
|
// Adapted from github.com/prometheus/client_golang/prometheus/http.go
|
||||||
@ -130,14 +130,10 @@ func MetricsCollectionOption(handlerName string) ServeOption {
|
|||||||
var (
|
var (
|
||||||
peersTotalMetric = prometheus.NewDesc(
|
peersTotalMetric = prometheus.NewDesc(
|
||||||
prometheus.BuildFQName("ipfs", "p2p", "peers_total"),
|
prometheus.BuildFQName("ipfs", "p2p", "peers_total"),
|
||||||
"Number of connected peers", []string{"transport"}, nil)
|
"Number of connected peers",
|
||||||
|
[]string{"transport"},
|
||||||
unixfsGetMetric = prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
nil,
|
||||||
Namespace: "ipfs",
|
)
|
||||||
Subsystem: "http",
|
|
||||||
Name: "unixfs_get_latency_seconds",
|
|
||||||
Help: "The time till the first block is received when 'getting' a file from the gateway.",
|
|
||||||
}, []string{"namespace"})
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type IpfsNodeCollector struct {
|
type IpfsNodeCollector struct {
|
||||||
|
Reference in New Issue
Block a user