waku-org/go-waku

View on GitHub
waku/v2/protocol/peer_exchange/metrics.go

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
package peer_exchange

import (
    "github.com/libp2p/go-libp2p/p2p/metricshelper"
    "github.com/prometheus/client_golang/prometheus"
)

var peerExchangeErrors = prometheus.NewCounterVec(
    prometheus.CounterOpts{
        Name: "peer_exchange_errors",
        Help: "The distribution of the lightpush protocol errors",
    },
    []string{"error_type"},
)

var collectors = []prometheus.Collector{
    peerExchangeErrors,
}

// Metrics exposes the functions required to update prometheus metrics for peer_exchange protocol
type Metrics interface {
    RecordError(err metricsErrCategory)
}

type metricsImpl struct {
    reg prometheus.Registerer
}

func newMetrics(reg prometheus.Registerer) Metrics {
    metricshelper.RegisterCollectors(reg, collectors...)
    return &metricsImpl{
        reg: reg,
    }
}

type metricsErrCategory string

var (
    decodeRPCFailure metricsErrCategory = "decode_rpc_failure"
    pxFailure        metricsErrCategory = "px_failure"
    dialFailure      metricsErrCategory = "dial_failure"
    rateLimitFailure metricsErrCategory = "ratelimit_failure"
)

// RecordError increases the counter for different error types
func (m *metricsImpl) RecordError(err metricsErrCategory) {
    peerExchangeErrors.WithLabelValues(string(err)).Inc()
}