Skip to content

Commit

Permalink
infra: register first block metric for gateway only
Browse files Browse the repository at this point in the history
  • Loading branch information
lanzafame authored and aschmahmann committed Feb 25, 2021
1 parent ce693d7 commit 2595a3d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
27 changes: 24 additions & 3 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
coreiface "github.com/ipfs/interface-go-ipfs-core"
ipath "github.com/ipfs/interface-go-ipfs-core/path"
routing "github.com/libp2p/go-libp2p-core/routing"
prometheus "github.com/prometheus/client_golang/prometheus"
)

const (
Expand All @@ -41,6 +42,8 @@ var onlyAscii = regexp.MustCompile("[[:^ascii:]]")
type gatewayHandler struct {
config GatewayConfig
api coreiface.CoreAPI

unixfsGetMetric *prometheus.SummaryVec
}

// StatusResponseWriter enables us to override HTTP Status Code passed to
Expand All @@ -63,9 +66,27 @@ func (sw *statusResponseWriter) WriteHeader(code int) {
}

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{
config: c,
api: api,
config: c,
api: api,
unixfsGetMetric: unixfsGetMetric,
}
return i
}
Expand Down Expand Up @@ -243,7 +264,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
return
}

unixfsGetMetric.WithLabelValues(parsedPath.Namespace()).Observe(time.Since(begin).Seconds())
i.unixfsGetMetric.WithLabelValues(parsedPath.Namespace()).Observe(time.Since(begin).Seconds())

defer dr.Close()

Expand Down
16 changes: 6 additions & 10 deletions core/corehttp/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
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 {
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
mux.Handle(path, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
Expand Down Expand Up @@ -56,7 +56,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 {
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
promRegistry := prometheus.NewRegistry()
Expand Down Expand Up @@ -136,14 +136,10 @@ func MetricsCollectionOption(handlerName string) ServeOption {
var (
peersTotalMetric = prometheus.NewDesc(
prometheus.BuildFQName("ipfs", "p2p", "peers_total"),
"Number of connected peers", []string{"transport"}, nil)

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{"namespace"})
"Number of connected peers",
[]string{"transport"},
nil,
)
)

type IpfsNodeCollector struct {
Expand Down

0 comments on commit 2595a3d

Please sign in to comment.