diff --git a/.gitignore b/.gitignore index b7b0629ab25..c30463f8e68 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /.idea /.vscode /tmp +vendor # Compiled Object files, Static and Dynamic libs (Shared Objects) *.o diff --git a/Makefile b/Makefile index 047f5d4f8be..d12db14204a 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ VERSION ?= "$(shell git describe --tags --abbrev=0 | cut -c2-)" COMMIT_HASH ?= "$(shell git describe --long --dirty --always --match "" || true)" CLEAN_COMMIT ?= "$(shell git describe --long --always --match "" || true)" COMMIT_TIME ?= "$(shell git show -s --format=%ct $(CLEAN_COMMIT) || true)" +BUILD_TAGS ?= LDFLAGS ?= -s -w \ -X github.com/ethersphere/bee/v2.version="$(VERSION)" \ -X github.com/ethersphere/bee/v2.commitHash="$(COMMIT_HASH)" \ @@ -31,11 +32,14 @@ LDFLAGS ?= -s -w \ .PHONY: all all: build lint test-race binary -.PHONY: binary +.PHONY: binary binary-nometrics binary: export CGO_ENABLED=0 binary: dist FORCE $(GO) version - $(GO) build -trimpath -ldflags "$(LDFLAGS)" -o dist/bee ./cmd/bee + $(GO) build -trimpath -ldflags "$(LDFLAGS)" -tags "$(BUILD_TAGS)" -o dist/bee ./cmd/bee + +binary-nometrics: + $(MAKE) binary BUILD_TAGS=nometrics dist: mkdir $@ diff --git a/go.mod b/go.mod index eccad953b87..7b9d37fae0f 100644 --- a/go.mod +++ b/go.mod @@ -140,7 +140,7 @@ require ( github.com/pelletier/go-toml v1.8.0 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_model v0.6.1 // indirect + github.com/prometheus/client_model v0.6.1 github.com/prometheus/common v0.62.0 github.com/prometheus/procfs v0.15.1 // indirect github.com/prometheus/statsd_exporter v0.22.7 // indirect diff --git a/pkg/accounting/metrics.go b/pkg/accounting/metrics.go index e38bdbbd989..18cba809866 100644 --- a/pkg/accounting/metrics.go +++ b/pkg/accounting/metrics.go @@ -6,169 +6,168 @@ package accounting import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { // all metrics fields must be exported // to be able to return them by Metrics() // using reflection - TotalDebitedAmount prometheus.Counter - TotalCreditedAmount prometheus.Counter - DebitEventsCount prometheus.Counter - CreditEventsCount prometheus.Counter - AccountingDisconnectsEnforceRefreshCount prometheus.Counter - AccountingRefreshAttemptCount prometheus.Counter - AccountingNonFatalRefreshFailCount prometheus.Counter - AccountingDisconnectsOverdrawCount prometheus.Counter - AccountingDisconnectsGhostOverdrawCount prometheus.Counter - AccountingDisconnectsReconnectCount prometheus.Counter - AccountingBlocksCount prometheus.Counter - AccountingReserveCount prometheus.Counter - TotalOriginatedCreditedAmount prometheus.Counter - OriginatedCreditEventsCount prometheus.Counter - SettleErrorCount prometheus.Counter - PaymentAttemptCount prometheus.Counter - PaymentErrorCount prometheus.Counter - ErrTimeOutOfSyncAlleged prometheus.Counter - ErrTimeOutOfSyncRecent prometheus.Counter - ErrTimeOutOfSyncInterval prometheus.Counter - ErrRefreshmentBelowExpected prometheus.Counter - ErrRefreshmentAboveExpected prometheus.Counter + TotalDebitedAmount m.Counter + TotalCreditedAmount m.Counter + DebitEventsCount m.Counter + CreditEventsCount m.Counter + AccountingDisconnectsEnforceRefreshCount m.Counter + AccountingRefreshAttemptCount m.Counter + AccountingNonFatalRefreshFailCount m.Counter + AccountingDisconnectsOverdrawCount m.Counter + AccountingDisconnectsGhostOverdrawCount m.Counter + AccountingDisconnectsReconnectCount m.Counter + AccountingBlocksCount m.Counter + AccountingReserveCount m.Counter + TotalOriginatedCreditedAmount m.Counter + OriginatedCreditEventsCount m.Counter + SettleErrorCount m.Counter + PaymentAttemptCount m.Counter + PaymentErrorCount m.Counter + ErrTimeOutOfSyncAlleged m.Counter + ErrTimeOutOfSyncRecent m.Counter + ErrTimeOutOfSyncInterval m.Counter + ErrRefreshmentBelowExpected m.Counter + ErrRefreshmentAboveExpected m.Counter } func newMetrics() metrics { subsystem := "accounting" return metrics{ - TotalDebitedAmount: prometheus.NewCounter(prometheus.CounterOpts{ + TotalDebitedAmount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_debited_amount", Help: "Amount of BZZ debited to peers (potential income of the node)", }), - TotalCreditedAmount: prometheus.NewCounter(prometheus.CounterOpts{ + TotalCreditedAmount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_credited_amount", Help: "Amount of BZZ credited to peers (potential cost of the node)", }), - DebitEventsCount: prometheus.NewCounter(prometheus.CounterOpts{ + DebitEventsCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "debit_events_count", Help: "Number of occurrences of BZZ debit events towards peers", }), - CreditEventsCount: prometheus.NewCounter(prometheus.CounterOpts{ + CreditEventsCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "credit_events_count", Help: "Number of occurrences of BZZ credit events towards peers", }), - AccountingDisconnectsEnforceRefreshCount: prometheus.NewCounter(prometheus.CounterOpts{ + AccountingDisconnectsEnforceRefreshCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "disconnects_enforce_refresh_count", Help: "Number of occurrences of peers disconnected based on failed refreshment attempts", }), - AccountingRefreshAttemptCount: prometheus.NewCounter(prometheus.CounterOpts{ + AccountingRefreshAttemptCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "refresh_attempt_count", Help: "Number of attempts of refresh op", }), - AccountingNonFatalRefreshFailCount: prometheus.NewCounter(prometheus.CounterOpts{ + AccountingNonFatalRefreshFailCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "non_fatal_refresh_fail_count", Help: "Number of occurrences of refreshments failing for peers because of peer timestamp ahead of ours", }), - AccountingDisconnectsOverdrawCount: prometheus.NewCounter(prometheus.CounterOpts{ + AccountingDisconnectsOverdrawCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "disconnects_overdraw_count", Help: "Number of occurrences of peers disconnected based on payment thresholds", }), - AccountingDisconnectsGhostOverdrawCount: prometheus.NewCounter(prometheus.CounterOpts{ + AccountingDisconnectsGhostOverdrawCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "disconnects_ghost_overdraw_count", Help: "Number of occurrences of peers disconnected based on undebitable requests thresholds", }), - AccountingDisconnectsReconnectCount: prometheus.NewCounter(prometheus.CounterOpts{ + AccountingDisconnectsReconnectCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "disconnects_reconnect_count", Help: "Number of occurrences of peers disconnected based on early attempt to reconnect", }), - AccountingBlocksCount: prometheus.NewCounter(prometheus.CounterOpts{ + AccountingBlocksCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "accounting_blocks_count", Help: "Number of occurrences of temporarily skipping a peer to avoid crossing their disconnect thresholds", }), - AccountingReserveCount: prometheus.NewCounter(prometheus.CounterOpts{ + AccountingReserveCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "accounting_reserve_count", Help: "Number of reserve calls", }), - TotalOriginatedCreditedAmount: prometheus.NewCounter(prometheus.CounterOpts{ + TotalOriginatedCreditedAmount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_originated_credited_amount", Help: "Amount of BZZ credited to peers (potential cost of the node) for originated traffic", }), - OriginatedCreditEventsCount: prometheus.NewCounter(prometheus.CounterOpts{ + OriginatedCreditEventsCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "originated_credit_events_count", Help: "Number of occurrences of BZZ credit events as originator towards peers", }), - SettleErrorCount: prometheus.NewCounter(prometheus.CounterOpts{ + SettleErrorCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "settle_error_count", Help: "Number of errors occurring in settle method", }), - PaymentErrorCount: prometheus.NewCounter(prometheus.CounterOpts{ + PaymentErrorCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "payment_error_count", Help: "Number of errors occurring during payment op", }), - PaymentAttemptCount: prometheus.NewCounter(prometheus.CounterOpts{ + PaymentAttemptCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "payment_attempt_count", Help: "Number of attempts of payment op", }), - ErrRefreshmentBelowExpected: prometheus.NewCounter(prometheus.CounterOpts{ + ErrRefreshmentBelowExpected: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "refreshment_below_expected", Help: "Number of times the peer received a refreshment that is below expected", }), - ErrRefreshmentAboveExpected: prometheus.NewCounter(prometheus.CounterOpts{ + ErrRefreshmentAboveExpected: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "refreshment_above_expected", Help: "Number of times the peer received a refreshment that is above expected", }), - ErrTimeOutOfSyncAlleged: prometheus.NewCounter(prometheus.CounterOpts{ + ErrTimeOutOfSyncAlleged: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "time_out_of_sync_alleged", Help: "Number of times the timestamps from peer were decreasing", }), - ErrTimeOutOfSyncRecent: prometheus.NewCounter(prometheus.CounterOpts{ + ErrTimeOutOfSyncRecent: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "time_out_of_sync_recent", Help: "Number of times the timestamps from peer differed from our measurement by more than 2 seconds", }), - ErrTimeOutOfSyncInterval: prometheus.NewCounter(prometheus.CounterOpts{ + ErrTimeOutOfSyncInterval: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "time_out_of_sync_interval", @@ -178,6 +177,6 @@ func newMetrics() metrics { } // Metrics returns the prometheus Collector for the accounting service. -func (a *Accounting) Metrics() []prometheus.Collector { +func (a *Accounting) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(a.metrics) } diff --git a/pkg/api/api.go b/pkg/api/api.go index 5f747237bc4..198be6a072e 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -36,6 +36,7 @@ import ( "github.com/ethersphere/bee/v2/pkg/gsoc" "github.com/ethersphere/bee/v2/pkg/jsonhttp" "github.com/ethersphere/bee/v2/pkg/log" + m "github.com/ethersphere/bee/v2/pkg/metrics" "github.com/ethersphere/bee/v2/pkg/p2p" "github.com/ethersphere/bee/v2/pkg/pingpong" "github.com/ethersphere/bee/v2/pkg/postage" @@ -63,7 +64,6 @@ import ( "github.com/go-playground/validator/v10" "github.com/gorilla/mux" "github.com/hashicorp/go-multierror" - "github.com/prometheus/client_golang/prometheus" "golang.org/x/sync/semaphore" ) @@ -164,7 +164,7 @@ type Service struct { accesscontrol accesscontrol.Controller postageContract postagecontract.Interface probe *Probe - metricsRegistry *prometheus.Registry + metricsRegistry m.MetricsRegistererGatherer stakingContract staking.Contract Options diff --git a/pkg/api/metrics.go b/pkg/api/metrics.go index 4f27142dfab..0a1cfcc8164 100644 --- a/pkg/api/metrics.go +++ b/pkg/api/metrics.go @@ -11,8 +11,6 @@ import ( "github.com/ethersphere/bee/v2" m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/collectors" ) const bytesInKB = 1000 @@ -23,35 +21,35 @@ type metrics struct { // all metrics fields must be exported // to be able to return them by Metrics() // using reflection - RequestCount prometheus.Counter - ResponseDuration prometheus.Histogram - PingRequestCount prometheus.Counter - ResponseCodeCounts *prometheus.CounterVec + RequestCount m.Counter + ResponseDuration m.Histogram + PingRequestCount m.Counter + ResponseCodeCounts m.CounterMetricVector - ContentApiDuration *prometheus.HistogramVec - UploadSpeed *prometheus.HistogramVec - DownloadSpeed *prometheus.HistogramVec + ContentApiDuration m.HistogramMetricVector + UploadSpeed m.HistogramMetricVector + DownloadSpeed m.HistogramMetricVector } func newMetrics() metrics { subsystem := "api" return metrics{ - RequestCount: prometheus.NewCounter(prometheus.CounterOpts{ + RequestCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "request_count", Help: "Number of API requests.", }), - ResponseDuration: prometheus.NewHistogram(prometheus.HistogramOpts{ + ResponseDuration: m.NewHistogram(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "response_duration_seconds", Help: "Histogram of API response durations.", Buckets: []float64{0.01, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}, }), - ResponseCodeCounts: prometheus.NewCounterVec( - prometheus.CounterOpts{ + ResponseCodeCounts: m.NewCounterVec( + m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "response_code_count", @@ -59,21 +57,21 @@ func newMetrics() metrics { }, []string{"code", "method"}, ), - ContentApiDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{ + ContentApiDuration: m.NewHistogramVec(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "content_api_duration", Help: "Histogram of file upload API response durations.", Buckets: []float64{0.5, 1, 2.5, 5, 10, 30, 60}, }, []string{"filesize", "method"}), - UploadSpeed: prometheus.NewHistogramVec(prometheus.HistogramOpts{ + UploadSpeed: m.NewHistogramVec(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "upload_speed", Help: "Histogram of upload speed in B/s.", Buckets: []float64{0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 5}, }, []string{"endpoint", "mode"}), - DownloadSpeed: prometheus.NewHistogramVec(prometheus.HistogramOpts{ + DownloadSpeed: m.NewHistogramVec(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "download_speed", @@ -94,13 +92,13 @@ func toFileSizeBucket(bytes int64) int64 { return fileSizeBucketsKBytes[len(fileSizeBucketsKBytes)-1] * bytesInKB } -func (s *Service) Metrics() []prometheus.Collector { +func (s *Service) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } // StatusMetrics exposes metrics that are exposed on the status protocol. -func (s *Service) StatusMetrics() []prometheus.Collector { - return []prometheus.Collector{ +func (s *Service) StatusMetrics() []m.Collector { + return []m.Collector{ s.metrics.UploadSpeed, s.metrics.DownloadSpeed, } @@ -166,20 +164,20 @@ func (rw *responseWriter) WriteHeader(code int) { rw.wroteHeader = true } -func newDebugMetrics() (r *prometheus.Registry) { - r = prometheus.NewRegistry() +func newDebugMetrics() (r m.MetricsRegistererGatherer) { + r = m.NewRegistry() // register standard metrics r.MustRegister( - collectors.NewProcessCollector(collectors.ProcessCollectorOpts{ + m.NewProcessCollector(m.ProcessCollectorOpts{ Namespace: m.Namespace, }), - collectors.NewGoCollector(), - prometheus.NewGauge(prometheus.GaugeOpts{ + m.NewGoCollector(), + m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Name: "info", Help: "Bee information.", - ConstLabels: prometheus.Labels{ + ConstLabels: m.Labels{ "version": bee.Version, }, }), @@ -188,10 +186,10 @@ func newDebugMetrics() (r *prometheus.Registry) { return r } -func (s *Service) MetricsRegistry() *prometheus.Registry { +func (s *Service) MetricsRegistry() m.MetricsRegistererGatherer { return s.metricsRegistry } -func (s *Service) MustRegisterMetrics(cs ...prometheus.Collector) { +func (s *Service) MustRegisterMetrics(cs ...m.Collector) { s.metricsRegistry.MustRegister(cs...) } diff --git a/pkg/api/router.go b/pkg/api/router.go index 13a782bcd77..6b286fd3467 100644 --- a/pkg/api/router.go +++ b/pkg/api/router.go @@ -13,11 +13,11 @@ import ( "github.com/ethersphere/bee/v2/pkg/jsonhttp" "github.com/ethersphere/bee/v2/pkg/log/httpaccess" + m "github.com/ethersphere/bee/v2/pkg/metrics" "github.com/ethersphere/bee/v2/pkg/swarm" "github.com/felixge/fgprof" "github.com/gorilla/handlers" "github.com/gorilla/mux" - "github.com/prometheus/client_golang/prometheus/promhttp" "resenje.org/web" ) @@ -126,9 +126,9 @@ func (s *Service) mountTechnicalDebug() { s.router.Path("/metrics").Handler(web.ChainHandlers( httpaccess.NewHTTPAccessSuppressLogHandler(), - web.FinalHandler(promhttp.InstrumentMetricHandler( + web.FinalHandler(m.InstrumentMetricHandler( s.metricsRegistry, - promhttp.HandlerFor(s.metricsRegistry, promhttp.HandlerOpts{}), + m.HandlerFor(s.metricsRegistry, m.HandlerOpts{}), )), )) diff --git a/pkg/hive/metrics.go b/pkg/hive/metrics.go index 62e9f5e482b..7d3da10cf78 100644 --- a/pkg/hive/metrics.go +++ b/pkg/hive/metrics.go @@ -6,98 +6,97 @@ package hive import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { - BroadcastPeers prometheus.Counter - BroadcastPeersPeers prometheus.Counter - BroadcastPeersSends prometheus.Counter + BroadcastPeers m.Counter + BroadcastPeersPeers m.Counter + BroadcastPeersSends m.Counter - PeersHandler prometheus.Counter - PeersHandlerPeers prometheus.Counter - UnreachablePeers prometheus.Counter + PeersHandler m.Counter + PeersHandlerPeers m.Counter + UnreachablePeers m.Counter - PingTime prometheus.Histogram - PingFailureTime prometheus.Histogram + PingTime m.Histogram + PingFailureTime m.Histogram - PeerConnectAttempts prometheus.Counter - PeerUnderlayErr prometheus.Counter - StorePeerErr prometheus.Counter - ReachablePeers prometheus.Counter + PeerConnectAttempts m.Counter + PeerUnderlayErr m.Counter + StorePeerErr m.Counter + ReachablePeers m.Counter } func newMetrics() metrics { subsystem := "hive" return metrics{ - BroadcastPeers: prometheus.NewCounter(prometheus.CounterOpts{ + BroadcastPeers: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "broadcast_peers_count", Help: "Number of calls to broadcast peers.", }), - BroadcastPeersPeers: prometheus.NewCounter(prometheus.CounterOpts{ + BroadcastPeersPeers: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "broadcast_peers_peer_count", Help: "Number of peers to be sent.", }), - BroadcastPeersSends: prometheus.NewCounter(prometheus.CounterOpts{ + BroadcastPeersSends: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "broadcast_peers_message_count", Help: "Number of individual peer gossip messages sent.", }), - PeersHandler: prometheus.NewCounter(prometheus.CounterOpts{ + PeersHandler: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "peers_handler_count", Help: "Number of peer messages received.", }), - PeersHandlerPeers: prometheus.NewCounter(prometheus.CounterOpts{ + PeersHandlerPeers: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "peers_handler_peers_count", Help: "Number of peers received in peer messages.", }), - UnreachablePeers: prometheus.NewCounter(prometheus.CounterOpts{ + UnreachablePeers: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "unreachable_peers_count", Help: "Number of peers that are unreachable.", }), - PingTime: prometheus.NewHistogram(prometheus.HistogramOpts{ + PingTime: m.NewHistogram(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "ping_time", Help: "The time spent for pings.", }), - PingFailureTime: prometheus.NewHistogram(prometheus.HistogramOpts{ + PingFailureTime: m.NewHistogram(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "fail_ping_time", Help: "The time spent for unsuccessful pings.", }), - PeerConnectAttempts: prometheus.NewCounter(prometheus.CounterOpts{ + PeerConnectAttempts: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "peer_attempt_count", Help: "Number of attempts made to check peer reachability.", }), - PeerUnderlayErr: prometheus.NewCounter(prometheus.CounterOpts{ + PeerUnderlayErr: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "peer_underlay_err_count", Help: "Number of errors extracting peer underlay.", }), - StorePeerErr: prometheus.NewCounter(prometheus.CounterOpts{ + StorePeerErr: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "store_peer_err_count", Help: "Number of peers that could not be stored.", }), - ReachablePeers: prometheus.NewCounter(prometheus.CounterOpts{ + ReachablePeers: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "reachable_peers_count", @@ -106,6 +105,6 @@ func newMetrics() metrics { } } -func (s *Service) Metrics() []prometheus.Collector { +func (s *Service) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/hive/pb/hive.pb.go b/pkg/hive/pb/hive.pb.go index a3f108a0bcb..856d7c1991f 100644 --- a/pkg/hive/pb/hive.pb.go +++ b/pkg/hive/pb/hive.pb.go @@ -5,10 +5,11 @@ package pb import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/log/logger.go b/pkg/log/logger.go index bb81d327172..f9fd360ad7d 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -15,7 +15,6 @@ import ( m "github.com/ethersphere/bee/v2/pkg/metrics" "github.com/hashicorp/go-multierror" - "github.com/prometheus/client_golang/prometheus" ) var _ Logger = (*logger)(nil) @@ -166,7 +165,7 @@ type logger struct { } // Metrics implements metrics.Collector interface. -func (l *logger) Metrics() []prometheus.Collector { +func (l *logger) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(l.metrics) } diff --git a/pkg/log/metrics.go b/pkg/log/metrics.go index 2691868921a..27a6cf94c3b 100644 --- a/pkg/log/metrics.go +++ b/pkg/log/metrics.go @@ -6,16 +6,15 @@ package log import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) // metrics groups various metrics counters for statistical reasons. type metrics struct { - ErrorCount prometheus.Counter - WarnCount prometheus.Counter - InfoCount prometheus.Counter - DebugCount prometheus.Counter - TraceCount prometheus.Counter + ErrorCount m.Counter + WarnCount m.Counter + InfoCount m.Counter + DebugCount m.Counter + TraceCount m.Counter } // Fire implements Hook interface. @@ -40,31 +39,31 @@ func newLogMetrics() *metrics { const subsystem = "log" return &metrics{ - ErrorCount: prometheus.NewCounter(prometheus.CounterOpts{ + ErrorCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "error_count", Help: "Number ERROR log messages.", }), - WarnCount: prometheus.NewCounter(prometheus.CounterOpts{ + WarnCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "warn_count", Help: "Number WARN log messages.", }), - InfoCount: prometheus.NewCounter(prometheus.CounterOpts{ + InfoCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "info_count", Help: "Number INFO log messages.", }), - DebugCount: prometheus.NewCounter(prometheus.CounterOpts{ + DebugCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "debug_count", Help: "Number DEBUG log messages.", }), - TraceCount: prometheus.NewCounter(prometheus.CounterOpts{ + TraceCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "trace_count", diff --git a/pkg/manifest/mantaray/persist.go b/pkg/manifest/mantaray/persist.go index 259785d803c..b59e58fed70 100644 --- a/pkg/manifest/mantaray/persist.go +++ b/pkg/manifest/mantaray/persist.go @@ -7,6 +7,7 @@ package mantaray import ( "context" "errors" + "golang.org/x/sync/errgroup" ) diff --git a/pkg/metrics/collectors.go b/pkg/metrics/collectors.go new file mode 100644 index 00000000000..0f713c27bc1 --- /dev/null +++ b/pkg/metrics/collectors.go @@ -0,0 +1,26 @@ +// Copyright 2020 The Swarm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package metrics + +import ( + "reflect" +) + +func PrometheusCollectorsFromFields(i any) (cs []Collector) { + v := reflect.Indirect(reflect.ValueOf(i)) + for i := 0; i < v.NumField(); i++ { + if !v.Field(i).CanInterface() { + continue + } + + u, ok := v.Field(i).Interface().(Collector) + if !ok { + continue + } + cs = append(cs, u) + + } + return cs +} diff --git a/pkg/metrics/exporter/exporter.go b/pkg/metrics/exporter/exporter.go new file mode 100644 index 00000000000..c94f218e79a --- /dev/null +++ b/pkg/metrics/exporter/exporter.go @@ -0,0 +1,23 @@ +// Copyright 2020 The Swarm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build !nometrics +// +build !nometrics + +package exporter + +import ( + exporter "contrib.go.opencensus.io/exporter/prometheus" + "github.com/prometheus/client_golang/prometheus" +) + +func NewExporter(o ExporterOptions) error { + r, _ := o.Registry.(*prometheus.Registry) + opts := exporter.Options{ + Namespace: o.Namespace, + Registry: r, + } + _, err := exporter.NewExporter(opts) + return err +} diff --git a/pkg/metrics/exporter/noop.go b/pkg/metrics/exporter/noop.go new file mode 100644 index 00000000000..3f80d2ee859 --- /dev/null +++ b/pkg/metrics/exporter/noop.go @@ -0,0 +1,12 @@ +// Copyright 2020 The Swarm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build nometrics +// +build nometrics + +package exporter + +func NewExporter(o ExporterOptions) error { + return nil +} diff --git a/pkg/metrics/exporter/types.go b/pkg/metrics/exporter/types.go new file mode 100644 index 00000000000..965c7e0753d --- /dev/null +++ b/pkg/metrics/exporter/types.go @@ -0,0 +1,20 @@ +// Copyright 2020 The Swarm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package exporter + +import ( + "github.com/prometheus/client_golang/prometheus" +) + +type MetricsSourcer interface { + MustRegister(...prometheus.Collector) + Register(prometheus.Collector) error + Unregister(prometheus.Collector) bool +} + +type ExporterOptions struct { + Namespace string + Registry MetricsSourcer +} diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 64ceef0c51e..7567085fa0f 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -2,31 +2,76 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +//go:build !nometrics +// +build !nometrics + package metrics import ( - "reflect" + "io" + "net/http" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/collectors" + "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/prometheus/common/expfmt" ) -// Namespace is prefixed before every metric. If it is changed, it must be done -// before any metrics collector is registered. -var Namespace = "bee" - -type Collector interface { - Metrics() []prometheus.Collector -} - -func PrometheusCollectorsFromFields(i any) (cs []prometheus.Collector) { - v := reflect.Indirect(reflect.ValueOf(i)) - for i := 0; i < v.NumField(); i++ { - if !v.Field(i).CanInterface() { - continue - } - if u, ok := v.Field(i).Interface().(prometheus.Collector); ok { - cs = append(cs, u) - } - } - return cs +func NewCounter(opts CounterOpts) Counter { + return prometheus.NewCounter(opts) +} + +func NewEncoder(w io.Writer, format expfmt.Format, options ...expfmt.EncoderOption) expfmt.Encoder { + return expfmt.NewEncoder(w, format, options...) +} + +func NewFormat(t expfmt.FormatType) expfmt.Format { + return expfmt.NewFormat(t) +} + +func NewGoCollector() Collector { + return collectors.NewGoCollector() +} + +func NewGaugeVec(opts GaugeOpts, names []string) GaugeMetricVector { + return prometheus.NewGaugeVec(opts, names) +} +func NewGauge(opts GaugeOpts) Gauge { + return prometheus.NewGauge(opts) +} + +func NewHistogram(opts HistogramOpts) Histogram { + return prometheus.NewHistogram(opts) +} + +func NewHistogramVec(opts HistogramOpts, names []string) HistogramMetricVector { + return prometheus.NewHistogramVec(opts, names) +} + +func NewCounterVec(opts CounterOpts, names []string) CounterMetricVector { + return prometheus.NewCounterVec(opts, names) +} + +func NewRegistry() MetricsRegistererGatherer { + return prometheus.NewRegistry() +} + +func NewProcessCollector(opts ProcessCollectorOpts) Collector { + return collectors.NewProcessCollector(opts) +} + +func NewSummary(opts SummaryOpts) Summary { + return prometheus.NewSummary(opts) +} + +func InstrumentMetricHandler(reg MetricsRegistererGatherer, handler http.Handler) http.Handler { + return promhttp.InstrumentMetricHandler(reg, handler) +} + +func HandlerFor(reg MetricsRegistererGatherer, opts HandlerOpts) http.Handler { + return promhttp.HandlerFor(reg, opts) +} + +func MustRegister(cs ...Collector) { + prometheus.MustRegister(cs...) } diff --git a/pkg/metrics/metrics_test.go b/pkg/metrics/metrics_test.go index 65029836e69..a71be9305b1 100644 --- a/pkg/metrics/metrics_test.go +++ b/pkg/metrics/metrics_test.go @@ -2,32 +2,34 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +//go:build !nometrics +// +build !nometrics + package metrics_test import ( "strings" "testing" - "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" + m "github.com/ethersphere/bee/v2/pkg/metrics" ) func TestPrometheusCollectorsFromFields(t *testing.T) { t.Parallel() s := newService() - collectors := metrics.PrometheusCollectorsFromFields(s) + collectors := m.PrometheusCollectorsFromFields(s) if l := len(collectors); l != 2 { t.Fatalf("got %v collectors %+v, want 2", l, collectors) } - m1 := collectors[0].(prometheus.Metric).Desc().String() + m1 := collectors[0].(m.Metric).Desc().String() if !strings.Contains(m1, "api_request_count") { t.Errorf("unexpected metric %s", m1) } - m2 := collectors[1].(prometheus.Metric).Desc().String() + m2 := collectors[1].(m.Metric).Desc().String() if !strings.Contains(m2, "api_response_duration_seconds") { t.Errorf("unexpected metric %s", m2) } @@ -35,31 +37,31 @@ func TestPrometheusCollectorsFromFields(t *testing.T) { type service struct { // valid metrics - RequestCount prometheus.Counter - ResponseDuration prometheus.Histogram + RequestCount m.Counter + ResponseDuration m.Histogram // invalid metrics - unexportedCount prometheus.Counter - UninitializedCount prometheus.Counter + unexportedCount m.Counter + UninitializedCount m.Counter } func newService() *service { subsystem := "api" return &service{ - RequestCount: prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: metrics.Namespace, + RequestCount: m.NewCounter(m.CounterOpts{ + Namespace: m.Namespace, Subsystem: subsystem, Name: "request_count", Help: "Number of API requests.", }), - ResponseDuration: prometheus.NewHistogram(prometheus.HistogramOpts{ - Namespace: metrics.Namespace, + ResponseDuration: m.NewHistogram(m.HistogramOpts{ + Namespace: m.Namespace, Subsystem: subsystem, Name: "response_duration_seconds", Help: "Histogram of API response durations.", Buckets: []float64{0.01, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}, }), - unexportedCount: prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: metrics.Namespace, + unexportedCount: m.NewCounter(m.CounterOpts{ + Namespace: m.Namespace, Subsystem: subsystem, Name: "unexported_count", Help: "This metrics should not be discoverable by metrics.PrometheusCollectorsFromFields.", diff --git a/pkg/metrics/noop.go b/pkg/metrics/noop.go new file mode 100644 index 00000000000..773177aaeda --- /dev/null +++ b/pkg/metrics/noop.go @@ -0,0 +1,171 @@ +//go:build nometrics +// +build nometrics + +// Copyright 2020 The Swarm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package metrics + +import ( + "io" + "net/http" +) + +type counterNoop struct{} + +func (c counterNoop) Desc() *Desc { return &Desc{} } +func (c counterNoop) Write(_ *MetricDTO) error { return nil } +func (c counterNoop) Describe(_ chan<- *Desc) { return } +func (c counterNoop) Collect(_ chan<- Metric) { return } +func (c counterNoop) Inc() { return } +func (c counterNoop) Add(_ float64) { return } + +func NewCounter(_ CounterOpts) Counter { + return &counterNoop{} +} + +var _ CounterMetricVector = (*counterVecNoop)(nil) + +type counterVecNoop struct{} + +func (c counterVecNoop) WithLabelValues(lvs ...string) Counter { + return NewCounter(CounterOpts{}) +} + +func NewCounterVec(opts CounterOpts, names []string) CounterMetricVector { + return &counterVecNoop{} +} + +type gaugeNoop struct{} + +func (g gaugeNoop) Desc() *Desc { return &Desc{} } +func (g gaugeNoop) Write(_ *MetricDTO) error { return nil } +func (g gaugeNoop) Describe(_ chan<- *Desc) { return } +func (g gaugeNoop) Collect(_ chan<- Metric) { return } +func (g gaugeNoop) Set(_ float64) { return } +func (g gaugeNoop) Inc() { return } +func (g gaugeNoop) Dec() { return } +func (g gaugeNoop) Add(_ float64) { return } +func (g gaugeNoop) Sub(_ float64) { return } +func (g gaugeNoop) SetToCurrentTime() { return } + +func NewGauge(_ GaugeOpts) Gauge { + return &gaugeNoop{} +} + +var _ GaugeMetricVector = (*gaugeVecNoop)(nil) + +type gaugeVecNoop struct{} + +func (g gaugeVecNoop) WithLabelValues(lvs ...string) Gauge { + return NewGauge(GaugeOpts{}) +} + +func NewGaugeVec(opts GaugeOpts, names []string) GaugeMetricVector { + return &gaugeVecNoop{} +} + +var _ Histogram = (*histogramNoop)(nil) + +type histogramNoop struct{} + +func (h histogramNoop) Desc() *Desc { return &Desc{} } +func (h histogramNoop) Write(_ *MetricDTO) error { return nil } +func (h histogramNoop) Describe(_ chan<- *Desc) { return } +func (h histogramNoop) Collect(_ chan<- Metric) { return } +func (h histogramNoop) Observe(_ float64) { return } + +func NewHistogram(_ HistogramOpts) Histogram { + return &histogramNoop{} +} + +var _ HistogramMetricVector = (*histogramVecNoop)(nil) + +type histogramVecNoop struct{} + +func (h histogramVecNoop) Describe(descs chan<- *Desc) { return } +func (h histogramVecNoop) Collect(metrics chan<- Metric) { return } +func (h histogramVecNoop) WithLabelValues(lvs ...string) Observer { + return NewHistogram(HistogramOpts{}) +} + +func NewHistogramVec(opts HistogramOpts, names []string) HistogramMetricVector { + return &histogramVecNoop{} +} + +var _ Summary = (*summaryNoop)(nil) + +type summaryNoop struct{} + +func (s summaryNoop) Desc() *Desc { return &Desc{} } +func (s summaryNoop) Write(_ *MetricDTO) error { return nil } +func (s summaryNoop) Describe(_ chan<- *Desc) { return } +func (s summaryNoop) Collect(_ chan<- Metric) { return } +func (s summaryNoop) Observe(_ float64) { return } + +func NewSummary(_ SummaryOpts) Summary { + return &summaryNoop{} +} + +var _ Collector = (*collectorNoop)(nil) + +type collectorNoop struct{} + +func (c collectorNoop) Describe(_ chan<- *Desc) { return } +func (c collectorNoop) Collect(_ chan<- Metric) { return } + +func NewGoCollector() Collector { + return &collectorNoop{} +} + +func NewProcessCollector(opts ProcessCollectorOpts) Collector { + return &collectorNoop{} +} + +var _ MetricsRegistererGatherer = (*registryNoop)(nil) + +type registryNoop struct{} + +func (r registryNoop) Register(collector Collector) error { + return nil +} + +func (r registryNoop) Unregister(collector Collector) bool { + return true +} + +func (r registryNoop) MustRegister(collector ...Collector) { return } +func (r registryNoop) Gather() ([]*MetricFamily, error) { + return []*MetricFamily{}, nil +} + +func NewRegistry() MetricsRegistererGatherer { + return ®istryNoop{} +} + +func MustRegister(_ ...Collector) { + // pass +} + +func InstrumentMetricHandler(_ MetricsRegistererGatherer, h http.Handler) http.Handler { + return h +} + +func HandlerFor(_ MetricsRegistererGatherer, _ HandlerOpts) http.Handler { + return http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { return }) +} + +var _ Encoder = (*encoderNoop)(nil) + +type encoderNoop struct{} + +func (e encoderNoop) Encode(family *MetricFamily) error { return nil } + +func NewEncoder(w io.Writer, format Format, options ...EncoderOption) Encoder { + return &encoderNoop{} +} + +func NewFormat(t FormatType) Format { + return `` +} diff --git a/pkg/metrics/types.go b/pkg/metrics/types.go new file mode 100644 index 00000000000..4722d5d0acf --- /dev/null +++ b/pkg/metrics/types.go @@ -0,0 +1,93 @@ +// Copyright 2020 The Swarm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package metrics + +import ( + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/collectors" + "github.com/prometheus/client_golang/prometheus/promhttp" + + dto "github.com/prometheus/client_model/go" + "github.com/prometheus/common/expfmt" +) + +const ( + // Namespace is prefixed before every metric. If it is changed, it must be done + // before any metrics collector is registered. + Namespace = "bee" + + TypeTextPlain = expfmt.TypeTextPlain +) + +// Prometheus Vector type interfaces +type ( + MetricsCollector interface { + Metrics() []prometheus.Collector + } + + GaugeMetricVector interface { + WithLabelValues(lvs ...string) Gauge + } + + HistogramMetricVector interface { + WithLabelValues(lvs ...string) Observer + Describe(chan<- *Desc) + Collect(chan<- Metric) + } + + CounterMetricVector interface { + WithLabelValues(lvs ...string) Counter + } + + MetricsRegistererGatherer interface { + Gather() ([]*MetricFamily, error) + MetricsRegisterer + } + + MetricsRegisterer interface { + MustRegister(...Collector) + Register(Collector) error + Unregister(Collector) bool + } +) + +// Prometheus types aliases +type ( + Collector = prometheus.Collector + Registry = prometheus.Registry + Observer = prometheus.Observer + Labels = prometheus.Labels + Metric = prometheus.Metric + Desc = prometheus.Desc + + Counter = prometheus.Counter + CounterOpts = prometheus.CounterOpts + CounterVec = CounterMetricVector + + Gauge = prometheus.Gauge + GaugeVec = GaugeMetricVector + GaugeVecOpts = prometheus.GaugeVecOpts + GaugeOpts = prometheus.GaugeOpts + + Histogram = prometheus.Histogram + HistogramOpts = prometheus.HistogramOpts + HistogramVec = HistogramMetricVector + HistogramVecOpts = prometheus.HistogramVecOpts + + Summary = prometheus.Summary + SummaryOpts = prometheus.SummaryOpts + SummaryVec = prometheus.SummaryVec + + ProcessCollectorOpts = collectors.ProcessCollectorOpts + HandlerOpts = promhttp.HandlerOpts + + MetricDTO = dto.Metric + MetricFamily = dto.MetricFamily + + Encoder = expfmt.Encoder + Format = expfmt.Format + FormatType = expfmt.FormatType + EncoderOption = expfmt.EncoderOption +) diff --git a/pkg/node/chain.go b/pkg/node/chain.go index c1f00dd61bc..77ac19d1f17 100644 --- a/pkg/node/chain.go +++ b/pkg/node/chain.go @@ -21,6 +21,7 @@ import ( "github.com/ethersphere/bee/v2/pkg/config" "github.com/ethersphere/bee/v2/pkg/crypto" "github.com/ethersphere/bee/v2/pkg/log" + m "github.com/ethersphere/bee/v2/pkg/metrics" "github.com/ethersphere/bee/v2/pkg/p2p/libp2p" "github.com/ethersphere/bee/v2/pkg/postage/postagecontract" "github.com/ethersphere/bee/v2/pkg/settlement" @@ -32,7 +33,6 @@ import ( "github.com/ethersphere/bee/v2/pkg/storage" "github.com/ethersphere/bee/v2/pkg/transaction" "github.com/ethersphere/bee/v2/pkg/transaction/wrapped" - "github.com/prometheus/client_golang/prometheus" ) const ( @@ -346,7 +346,7 @@ type noOpChainBackend struct { chainID int64 } -func (m noOpChainBackend) Metrics() []prometheus.Collector { +func (m noOpChainBackend) Metrics() []m.Collector { return nil } diff --git a/pkg/node/metrics.go b/pkg/node/metrics.go index a161c39ffe7..b5211c895a3 100644 --- a/pkg/node/metrics.go +++ b/pkg/node/metrics.go @@ -5,24 +5,23 @@ package node import ( - "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" + m "github.com/ethersphere/bee/v2/pkg/metrics" ) type nodeMetrics struct { // WarmupDuration measures time in seconds for the node warmup to complete - WarmupDuration prometheus.Histogram + WarmupDuration m.Histogram // FullSyncDuration measures time in seconds for the full sync to complete - FullSyncDuration prometheus.Histogram + FullSyncDuration m.Histogram } func newMetrics() nodeMetrics { subsystem := "init" return nodeMetrics{ - WarmupDuration: prometheus.NewHistogram( - prometheus.HistogramOpts{ - Namespace: metrics.Namespace, + WarmupDuration: m.NewHistogram( + m.HistogramOpts{ + Namespace: m.Namespace, Subsystem: subsystem, Name: "warmup_duration_seconds", Help: "Duration in seconds for node warmup to complete", @@ -30,9 +29,9 @@ func newMetrics() nodeMetrics { Buckets: []float64{10, 20, 25, 30, 35, 40, 45, 50, 60, 70, 90, 120, 180, 240, 300, 350, 380, 400, 420, 440, 460, 480, 550, 600}, }, ), - FullSyncDuration: prometheus.NewHistogram( - prometheus.HistogramOpts{ - Namespace: metrics.Namespace, + FullSyncDuration: m.NewHistogram( + m.HistogramOpts{ + Namespace: m.Namespace, Subsystem: subsystem, Name: "full_sync_duration_minutes", Help: "Duration in minutes for node full sync to complete", @@ -45,6 +44,6 @@ func newMetrics() nodeMetrics { } } -func getMetrics(nodeMetrics nodeMetrics) []prometheus.Collector { - return metrics.PrometheusCollectorsFromFields(nodeMetrics) +func getMetrics(nodeMetrics nodeMetrics) []m.Collector { + return m.PrometheusCollectorsFromFields(nodeMetrics) } diff --git a/pkg/node/node.go b/pkg/node/node.go index 163b599e651..575ee44cbeb 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -35,7 +35,7 @@ import ( "github.com/ethersphere/bee/v2/pkg/gsoc" "github.com/ethersphere/bee/v2/pkg/hive" "github.com/ethersphere/bee/v2/pkg/log" - "github.com/ethersphere/bee/v2/pkg/metrics" + m "github.com/ethersphere/bee/v2/pkg/metrics" "github.com/ethersphere/bee/v2/pkg/p2p" "github.com/ethersphere/bee/v2/pkg/p2p/libp2p" "github.com/ethersphere/bee/v2/pkg/pingpong" @@ -78,7 +78,6 @@ import ( "github.com/ethersphere/bee/v2/pkg/util/syncutil" "github.com/hashicorp/go-multierror" ma "github.com/multiformats/go-multiaddr" - "github.com/prometheus/client_golang/prometheus" "golang.org/x/crypto/sha3" "golang.org/x/sync/errgroup" ) @@ -648,7 +647,7 @@ func NewBee( } } - var registry *prometheus.Registry + var registry m.MetricsRegistererGatherer if apiService != nil { registry = apiService.MetricsRegistry() @@ -979,7 +978,7 @@ func NewBee( validStamp := postage.ValidStamp(batchStore) // metrics exposed on the status protocol - statusMetricsRegistry := prometheus.NewRegistry() + statusMetricsRegistry := m.NewRegistry() if localStore != nil { statusMetricsRegistry.MustRegister(localStore.StatusMetrics()...) } @@ -1282,20 +1281,20 @@ func NewBee( apiService.MustRegisterMetrics(lightNodes.Metrics()...) apiService.MustRegisterMetrics(hive.Metrics()...) - if bs, ok := batchStore.(metrics.Collector); ok { + if bs, ok := batchStore.(m.MetricsCollector); ok { apiService.MustRegisterMetrics(bs.Metrics()...) } - if ls, ok := eventListener.(metrics.Collector); ok { + if ls, ok := eventListener.(m.MetricsCollector); ok { apiService.MustRegisterMetrics(ls.Metrics()...) } - if pssServiceMetrics, ok := pssService.(metrics.Collector); ok { + if pssServiceMetrics, ok := pssService.(m.MetricsCollector); ok { apiService.MustRegisterMetrics(pssServiceMetrics.Metrics()...) } - if swapBackendMetrics, ok := chainBackend.(metrics.Collector); ok { + if swapBackendMetrics, ok := chainBackend.(m.MetricsCollector); ok { apiService.MustRegisterMetrics(swapBackendMetrics.Metrics()...) } - if l, ok := logger.(metrics.Collector); ok { + if l, ok := logger.(m.MetricsCollector); ok { apiService.MustRegisterMetrics(l.Metrics()...) } apiService.MustRegisterMetrics(pseudosettleService.Metrics()...) diff --git a/pkg/node/statestore.go b/pkg/node/statestore.go index 626848cf88e..b5009e08b6a 100644 --- a/pkg/node/statestore.go +++ b/pkg/node/statestore.go @@ -21,7 +21,7 @@ import ( // InitStateStore will initialize the stateStore with the given path to the // data directory. When given an empty directory path, the function will instead // initialize an in-memory state store that will not be persisted. -func InitStateStore(logger log.Logger, dataDir string, cacheCapacity uint64) (storage.StateStorerManager, metrics.Collector, error) { +func InitStateStore(logger log.Logger, dataDir string, cacheCapacity uint64) (storage.StateStorerManager, metrics.MetricsCollector, error) { if dataDir == "" { logger.Warning("using in-mem state store, no node state will be persisted") } else { diff --git a/pkg/p2p/libp2p/internal/handshake/metrics.go b/pkg/p2p/libp2p/internal/handshake/metrics.go index c8177d3f93e..7b83d7ef7da 100644 --- a/pkg/p2p/libp2p/internal/handshake/metrics.go +++ b/pkg/p2p/libp2p/internal/handshake/metrics.go @@ -6,17 +6,16 @@ package handshake import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) // metrics groups handshake related prometheus counters. type metrics struct { - SynRx prometheus.Counter - SynRxFailed prometheus.Counter - SynAckTx prometheus.Counter - SynAckTxFailed prometheus.Counter - AckRx prometheus.Counter - AckRxFailed prometheus.Counter + SynRx m.Counter + SynRxFailed m.Counter + SynAckTx m.Counter + SynAckTxFailed m.Counter + AckRx m.Counter + AckRxFailed m.Counter } // newMetrics is a convenient constructor for creating new metrics. @@ -24,37 +23,37 @@ func newMetrics() metrics { const subsystem = "handshake" return metrics{ - SynRx: prometheus.NewCounter(prometheus.CounterOpts{ + SynRx: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "syn_rx", Help: "The number of syn messages that were successfully read.", }), - SynRxFailed: prometheus.NewCounter(prometheus.CounterOpts{ + SynRxFailed: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "syn_rx_failed", Help: "The number of syn messages that were unsuccessfully read.", }), - SynAckTx: prometheus.NewCounter(prometheus.CounterOpts{ + SynAckTx: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "syn_ack_tx", Help: "The number of syn-ack messages that were successfully written.", }), - SynAckTxFailed: prometheus.NewCounter(prometheus.CounterOpts{ + SynAckTxFailed: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "syn_ack_tx_failed", Help: "The number of syn-ack messages that were unsuccessfully written.", }), - AckRx: prometheus.NewCounter(prometheus.CounterOpts{ + AckRx: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "ack_rx", Help: "The number of ack messages that were successfully read.", }), - AckRxFailed: prometheus.NewCounter(prometheus.CounterOpts{ + AckRxFailed: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "ack_rx_failed", @@ -64,6 +63,6 @@ func newMetrics() metrics { } // Metrics returns set of prometheus collectors. -func (s *Service) Metrics() []prometheus.Collector { +func (s *Service) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/p2p/libp2p/internal/handshake/pb/handshake.pb.go b/pkg/p2p/libp2p/internal/handshake/pb/handshake.pb.go index 0ae7f00ebf6..799f478429d 100644 --- a/pkg/p2p/libp2p/internal/handshake/pb/handshake.pb.go +++ b/pkg/p2p/libp2p/internal/handshake/pb/handshake.pb.go @@ -5,10 +5,11 @@ package pb import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/p2p/libp2p/internal/headers/pb/headers.pb.go b/pkg/p2p/libp2p/internal/headers/pb/headers.pb.go index c5652519eed..2efd44854a2 100644 --- a/pkg/p2p/libp2p/internal/headers/pb/headers.pb.go +++ b/pkg/p2p/libp2p/internal/headers/pb/headers.pb.go @@ -5,10 +5,11 @@ package pb import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/p2p/libp2p/internal/reacher/metrics.go b/pkg/p2p/libp2p/internal/reacher/metrics.go index a6019ba4a4e..f8008096a17 100644 --- a/pkg/p2p/libp2p/internal/reacher/metrics.go +++ b/pkg/p2p/libp2p/internal/reacher/metrics.go @@ -6,25 +6,24 @@ package reacher import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { - Pings *prometheus.CounterVec - PingTime *prometheus.HistogramVec + Pings m.CounterMetricVector + PingTime m.HistogramMetricVector } func newMetrics() metrics { subsystem := "reacher" return metrics{ - Pings: prometheus.NewCounterVec(prometheus.CounterOpts{ + Pings: m.NewCounterVec(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "pings", Help: "Ping counter.", }, []string{"status"}), - PingTime: prometheus.NewHistogramVec(prometheus.HistogramOpts{ + PingTime: m.NewHistogramVec(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "ping_timer", @@ -33,6 +32,6 @@ func newMetrics() metrics { } } -func (s *reacher) Metrics() []prometheus.Collector { +func (s *reacher) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/p2p/libp2p/libp2p.go b/pkg/p2p/libp2p/libp2p.go index e1091e0b637..c4e193a8bfc 100644 --- a/pkg/p2p/libp2p/libp2p.go +++ b/pkg/p2p/libp2p/libp2p.go @@ -22,6 +22,7 @@ import ( "github.com/ethersphere/bee/v2/pkg/bzz" beecrypto "github.com/ethersphere/bee/v2/pkg/crypto" "github.com/ethersphere/bee/v2/pkg/log" + exporter "github.com/ethersphere/bee/v2/pkg/metrics/exporter" "github.com/ethersphere/bee/v2/pkg/p2p" "github.com/ethersphere/bee/v2/pkg/p2p/libp2p/internal/blocklist" "github.com/ethersphere/bee/v2/pkg/p2p/libp2p/internal/breaker" @@ -48,14 +49,11 @@ import ( libp2pping "github.com/libp2p/go-libp2p/p2p/protocol/ping" "github.com/libp2p/go-libp2p/p2p/transport/tcp" ws "github.com/libp2p/go-libp2p/p2p/transport/websocket" - ma "github.com/multiformats/go-multiaddr" "github.com/multiformats/go-multistream" "go.uber.org/atomic" - ocprom "contrib.go.opencensus.io/exporter/prometheus" - m2 "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" + m "github.com/ethersphere/bee/v2/pkg/metrics" ) // loggerName is the tree path name of the logger for this package. @@ -129,7 +127,7 @@ type Options struct { ValidateOverlay bool hostFactory func(...libp2p.Option) (host.Host, error) HeadersRWTimeout time.Duration - Registry *prometheus.Registry + Registry m.MetricsRegistererGatherer } func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay swarm.Address, addr string, ab addressbook.Putter, storer storage.StateStorer, lightNodes *lightnode.Container, logger log.Logger, tracer *tracing.Tracer, o Options) (*Service, error) { @@ -176,9 +174,8 @@ func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay if o.Registry != nil { rcmgr.MustRegisterWith(o.Registry) } - - _, err = ocprom.NewExporter(ocprom.Options{ - Namespace: m2.Namespace, + err = exporter.NewExporter(exporter.ExporterOptions{ + Namespace: m.Namespace, Registry: o.Registry, }) if err != nil { diff --git a/pkg/p2p/libp2p/metrics.go b/pkg/p2p/libp2p/metrics.go index 378ec26f6b0..210f6cef4e8 100644 --- a/pkg/p2p/libp2p/metrics.go +++ b/pkg/p2p/libp2p/metrics.go @@ -6,112 +6,111 @@ package libp2p import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { // all metrics fields must be exported // to be able to return them by Metrics() // using reflection - CreatedConnectionCount prometheus.Counter - HandledConnectionCount prometheus.Counter - CreatedStreamCount prometheus.Counter - ClosedStreamCount prometheus.Counter - StreamResetCount prometheus.Counter - HandledStreamCount prometheus.Counter - BlocklistedPeerCount prometheus.Counter - BlocklistedPeerErrCount prometheus.Counter - DisconnectCount prometheus.Counter - ConnectBreakerCount prometheus.Counter - UnexpectedProtocolReqCount prometheus.Counter - KickedOutPeersCount prometheus.Counter - StreamHandlerErrResetCount prometheus.Counter - HeadersExchangeDuration prometheus.Histogram + CreatedConnectionCount m.Counter + HandledConnectionCount m.Counter + CreatedStreamCount m.Counter + ClosedStreamCount m.Counter + StreamResetCount m.Counter + HandledStreamCount m.Counter + BlocklistedPeerCount m.Counter + BlocklistedPeerErrCount m.Counter + DisconnectCount m.Counter + ConnectBreakerCount m.Counter + UnexpectedProtocolReqCount m.Counter + KickedOutPeersCount m.Counter + StreamHandlerErrResetCount m.Counter + HeadersExchangeDuration m.Histogram } func newMetrics() metrics { subsystem := "libp2p" return metrics{ - CreatedConnectionCount: prometheus.NewCounter(prometheus.CounterOpts{ + CreatedConnectionCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "created_connection_count", Help: "Number of initiated outgoing libp2p connections.", }), - HandledConnectionCount: prometheus.NewCounter(prometheus.CounterOpts{ + HandledConnectionCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "handled_connection_count", Help: "Number of handled incoming libp2p connections.", }), - CreatedStreamCount: prometheus.NewCounter(prometheus.CounterOpts{ + CreatedStreamCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "created_stream_count", Help: "Number of initiated outgoing libp2p streams.", }), - ClosedStreamCount: prometheus.NewCounter(prometheus.CounterOpts{ + ClosedStreamCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "closed_stream_count", Help: "Number of closed outgoing libp2p streams.", }), - StreamResetCount: prometheus.NewCounter(prometheus.CounterOpts{ + StreamResetCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "stream_reset_count", Help: "Number of outgoing libp2p streams resets.", }), - HandledStreamCount: prometheus.NewCounter(prometheus.CounterOpts{ + HandledStreamCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "handled_stream_count", Help: "Number of handled incoming libp2p streams.", }), - BlocklistedPeerCount: prometheus.NewCounter(prometheus.CounterOpts{ + BlocklistedPeerCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "blocklisted_peer_count", Help: "Number of peers we've blocklisted.", }), - BlocklistedPeerErrCount: prometheus.NewCounter(prometheus.CounterOpts{ + BlocklistedPeerErrCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "blocklisted_peer_err_count", Help: "Number of peers we've been unable to blocklist.", }), - DisconnectCount: prometheus.NewCounter(prometheus.CounterOpts{ + DisconnectCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "disconnect_count", Help: "Number of peers we've disconnected from (initiated locally).", }), - ConnectBreakerCount: prometheus.NewCounter(prometheus.CounterOpts{ + ConnectBreakerCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "connect_breaker_count", Help: "Number of times we got a closed breaker while connecting to another peer.", }), - UnexpectedProtocolReqCount: prometheus.NewCounter(prometheus.CounterOpts{ + UnexpectedProtocolReqCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "unexpected_protocol_request_count", Help: "Number of requests the peer is not expecting.", }), - KickedOutPeersCount: prometheus.NewCounter(prometheus.CounterOpts{ + KickedOutPeersCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "kickedout_peers_count", Help: "Number of total kicked-out peers.", }), - StreamHandlerErrResetCount: prometheus.NewCounter(prometheus.CounterOpts{ + StreamHandlerErrResetCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "stream_handler_error_reset_count", Help: "Number of total stream handler error resets.", }), - HeadersExchangeDuration: prometheus.NewHistogram(prometheus.HistogramOpts{ + HeadersExchangeDuration: m.NewHistogram(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "headers_exchange_duration", @@ -120,13 +119,13 @@ func newMetrics() metrics { } } -func (s *Service) Metrics() []prometheus.Collector { +func (s *Service) Metrics() []m.Collector { return append(m.PrometheusCollectorsFromFields(s.metrics), s.handshakeService.Metrics()...) } // StatusMetrics exposes metrics that are exposed on the status protocol. -func (s *Service) StatusMetrics() []prometheus.Collector { - return []prometheus.Collector{ +func (s *Service) StatusMetrics() []m.Collector { + return []m.Collector{ s.metrics.HeadersExchangeDuration, } } diff --git a/pkg/p2p/protobuf/internal/pb/test.pb.go b/pkg/p2p/protobuf/internal/pb/test.pb.go index 2ca5f3d8ac8..660fcb31cc1 100644 --- a/pkg/p2p/protobuf/internal/pb/test.pb.go +++ b/pkg/p2p/protobuf/internal/pb/test.pb.go @@ -5,10 +5,11 @@ package pb import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/pingpong/metrics.go b/pkg/pingpong/metrics.go index 155b3abdb8e..7cd38937480 100644 --- a/pkg/pingpong/metrics.go +++ b/pkg/pingpong/metrics.go @@ -6,42 +6,41 @@ package pingpong import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { // all metrics fields must be exported // to be able to return them by Metrics() // using reflection - PingSentCount prometheus.Counter - PongSentCount prometheus.Counter - PingReceivedCount prometheus.Counter - PongReceivedCount prometheus.Counter + PingSentCount m.Counter + PongSentCount m.Counter + PingReceivedCount m.Counter + PongReceivedCount m.Counter } func newMetrics() metrics { subsystem := "pingpong" return metrics{ - PingSentCount: prometheus.NewCounter(prometheus.CounterOpts{ + PingSentCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "ping_sent_count", Help: "Number ping requests sent.", }), - PongSentCount: prometheus.NewCounter(prometheus.CounterOpts{ + PongSentCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "pong_sent_count", Help: "Number of pong responses sent.", }), - PingReceivedCount: prometheus.NewCounter(prometheus.CounterOpts{ + PingReceivedCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "ping_received_count", Help: "Number ping requests received.", }), - PongReceivedCount: prometheus.NewCounter(prometheus.CounterOpts{ + PongReceivedCount: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "pong_received_count", @@ -50,6 +49,6 @@ func newMetrics() metrics { } } -func (s *Service) Metrics() []prometheus.Collector { +func (s *Service) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/pingpong/pb/pingpong.pb.go b/pkg/pingpong/pb/pingpong.pb.go index 536cbf6a254..0b6656053ab 100644 --- a/pkg/pingpong/pb/pingpong.pb.go +++ b/pkg/pingpong/pb/pingpong.pb.go @@ -5,10 +5,11 @@ package pb import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/postage/batchstore/metrics.go b/pkg/postage/batchstore/metrics.go index de78aa5ef54..fd7ca9e8c6d 100644 --- a/pkg/postage/batchstore/metrics.go +++ b/pkg/postage/batchstore/metrics.go @@ -6,32 +6,31 @@ package batchstore import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { - Commitment prometheus.Gauge - Radius prometheus.Gauge - UnreserveDuration *prometheus.HistogramVec + Commitment m.Gauge + Radius m.Gauge + UnreserveDuration m.HistogramMetricVector } func newMetrics() metrics { subsystem := "batchstore" return metrics{ - Commitment: prometheus.NewGauge(prometheus.GaugeOpts{ + Commitment: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "commitment", Help: "Sum of all batches' commitment.", }), - Radius: prometheus.NewGauge(prometheus.GaugeOpts{ + Radius: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "radius", Help: "Radius of responsibility observed by the batchstore.", }), - UnreserveDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{ + UnreserveDuration: m.NewHistogramVec(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "unreserve_duration", @@ -40,6 +39,6 @@ func newMetrics() metrics { } } -func (s *store) Metrics() []prometheus.Collector { +func (s *store) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/postage/listener/listener.go b/pkg/postage/listener/listener.go index d9d52b2c5a1..56ef4303a35 100644 --- a/pkg/postage/listener/listener.go +++ b/pkg/postage/listener/listener.go @@ -17,11 +17,11 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethersphere/bee/v2/pkg/log" + m "github.com/ethersphere/bee/v2/pkg/metrics" "github.com/ethersphere/bee/v2/pkg/postage" "github.com/ethersphere/bee/v2/pkg/postage/batchservice" "github.com/ethersphere/bee/v2/pkg/transaction" "github.com/ethersphere/bee/v2/pkg/util/syncutil" - "github.com/prometheus/client_golang/prometheus" ) // loggerName is the tree path name of the logger for this package. @@ -413,7 +413,7 @@ type priceUpdateEvent struct { Price *big.Int } -func totalTimeMetric(metric prometheus.Counter, start time.Time) { +func totalTimeMetric(metric m.Counter, start time.Time) { totalTime := time.Since(start) metric.Add(float64(totalTime)) } diff --git a/pkg/postage/listener/metrics.go b/pkg/postage/listener/metrics.go index 8fb7b639884..3045edec1a4 100644 --- a/pkg/postage/listener/metrics.go +++ b/pkg/postage/listener/metrics.go @@ -6,28 +6,27 @@ package listener import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { // aggregate events handled - EventsProcessed prometheus.Counter - EventErrors prometheus.Counter - PagesProcessed prometheus.Counter + EventsProcessed m.Counter + EventErrors m.Counter + PagesProcessed m.Counter // individual event counters - CreatedCounter prometheus.Counter - TopupCounter prometheus.Counter - DepthCounter prometheus.Counter - PriceCounter prometheus.Counter + CreatedCounter m.Counter + TopupCounter m.Counter + DepthCounter m.Counter + PriceCounter m.Counter // total calls to chain backend - BackendCalls prometheus.Counter - BackendErrors prometheus.Counter + BackendCalls m.Counter + BackendErrors m.Counter // processing durations - PageProcessDuration prometheus.Counter - EventProcessDuration prometheus.Counter + PageProcessDuration m.Counter + EventProcessDuration m.Counter } func newMetrics() metrics { @@ -35,19 +34,19 @@ func newMetrics() metrics { return metrics{ // aggregate events handled - EventsProcessed: prometheus.NewCounter(prometheus.CounterOpts{ + EventsProcessed: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "events_processed", Help: "total events processed", }), - EventErrors: prometheus.NewCounter(prometheus.CounterOpts{ + EventErrors: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "event_errors", Help: "total event errors while processing", }), - PagesProcessed: prometheus.NewCounter(prometheus.CounterOpts{ + PagesProcessed: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "pages_processed", @@ -55,28 +54,28 @@ func newMetrics() metrics { }), // individual event counters - CreatedCounter: prometheus.NewCounter(prometheus.CounterOpts{ + CreatedCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "created_events", Help: "total batch created events processed", }), - TopupCounter: prometheus.NewCounter(prometheus.CounterOpts{ + TopupCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "topup_events", Help: "total batch topup events handled", }), - DepthCounter: prometheus.NewCounter(prometheus.CounterOpts{ + DepthCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "depth_events", Help: "total batch depth change events handled", }), - PriceCounter: prometheus.NewCounter(prometheus.CounterOpts{ + PriceCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "price_events", @@ -84,13 +83,13 @@ func newMetrics() metrics { }), // total call - BackendCalls: prometheus.NewCounter(prometheus.CounterOpts{ + BackendCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "backend_calls", Help: "total chain backend calls", }), - BackendErrors: prometheus.NewCounter(prometheus.CounterOpts{ + BackendErrors: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "backend_errors", @@ -98,14 +97,14 @@ func newMetrics() metrics { }), // processing durations - PageProcessDuration: prometheus.NewCounter(prometheus.CounterOpts{ + PageProcessDuration: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "page_duration", Help: "how long it took to process a page", }), - EventProcessDuration: prometheus.NewCounter(prometheus.CounterOpts{ + EventProcessDuration: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "event_duration", @@ -114,6 +113,6 @@ func newMetrics() metrics { } } -func (l *listener) Metrics() []prometheus.Collector { +func (l *listener) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(l.metrics) } diff --git a/pkg/pricer/headerutils/utilities.go b/pkg/pricer/headerutils/utilities.go index a85e56990b4..2576df0c1c8 100644 --- a/pkg/pricer/headerutils/utilities.go +++ b/pkg/pricer/headerutils/utilities.go @@ -7,6 +7,7 @@ package headerutils import ( "encoding/binary" "errors" + "github.com/ethersphere/bee/v2/pkg/p2p" "github.com/ethersphere/bee/v2/pkg/swarm" ) diff --git a/pkg/pricing/pb/pricing.pb.go b/pkg/pricing/pb/pricing.pb.go index 949eba00bb2..02103ee355c 100644 --- a/pkg/pricing/pb/pricing.pb.go +++ b/pkg/pricing/pb/pricing.pb.go @@ -5,10 +5,11 @@ package pb import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/pss/metrics.go b/pkg/pss/metrics.go index 108f516215f..20803cf3b50 100644 --- a/pkg/pss/metrics.go +++ b/pkg/pss/metrics.go @@ -6,25 +6,24 @@ package pss import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { - TotalMessagesSentCounter prometheus.Counter - MessageMiningDuration prometheus.Gauge + TotalMessagesSentCounter m.Counter + MessageMiningDuration m.Gauge } func newMetrics() metrics { subsystem := "pss" return metrics{ - TotalMessagesSentCounter: prometheus.NewCounter(prometheus.CounterOpts{ + TotalMessagesSentCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_message_sent", Help: "Total messages sent.", }), - MessageMiningDuration: prometheus.NewGauge(prometheus.GaugeOpts{ + MessageMiningDuration: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "mining_duration", @@ -33,6 +32,6 @@ func newMetrics() metrics { } } -func (s *pss) Metrics() []prometheus.Collector { +func (s *pss) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/puller/metrics.go b/pkg/puller/metrics.go index bfa546f26e0..5af973da09e 100644 --- a/pkg/puller/metrics.go +++ b/pkg/puller/metrics.go @@ -6,46 +6,45 @@ package puller import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { - SyncWorkerIterCounter prometheus.Counter // counts the number of syncing iterations - SyncWorkerCounter prometheus.Gauge // count number of syncing jobs - SyncedCounter *prometheus.CounterVec // number of synced chunks - SyncWorkerErrCounter prometheus.Counter // count number of errors - MaxUintErrCounter prometheus.Counter // how many times we got maxuint as topmost + SyncWorkerIterCounter m.Counter // counts the number of syncing iterations + SyncWorkerCounter m.Gauge // count number of syncing jobs + SyncedCounter m.CounterMetricVector // number of synced chunks + SyncWorkerErrCounter m.Counter // count number of errors + MaxUintErrCounter m.Counter // how many times we got maxuint as topmost } func newMetrics() metrics { subsystem := "puller" return metrics{ - SyncWorkerIterCounter: prometheus.NewCounter(prometheus.CounterOpts{ + SyncWorkerIterCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "worker_iterations", Help: "Total worker iterations.", }), - SyncWorkerCounter: prometheus.NewGauge(prometheus.GaugeOpts{ + SyncWorkerCounter: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "worker", Help: "Total active worker jobs.", }), - SyncedCounter: prometheus.NewCounterVec(prometheus.CounterOpts{ + SyncedCounter: m.NewCounterVec(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "synced_chunks", Help: "Total synced chunks.", }, []string{"type"}), - SyncWorkerErrCounter: prometheus.NewCounter(prometheus.CounterOpts{ + SyncWorkerErrCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "worker_errors", Help: "Total worker errors.", }), - MaxUintErrCounter: prometheus.NewCounter(prometheus.CounterOpts{ + MaxUintErrCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "max_uint_errors", @@ -54,6 +53,6 @@ func newMetrics() metrics { } } -func (p *Puller) Metrics() []prometheus.Collector { +func (p *Puller) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(p.metrics) } diff --git a/pkg/pullsync/metrics.go b/pkg/pullsync/metrics.go index 57e26916c93..b594249de00 100644 --- a/pkg/pullsync/metrics.go +++ b/pkg/pullsync/metrics.go @@ -6,89 +6,88 @@ package pullsync import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { - Offered prometheus.Counter // number of chunks offered - Wanted prometheus.Counter // number of chunks wanted - MissingChunks prometheus.Counter // number of reserve get errs - ReceivedZeroAddress prometheus.Counter // number of delivered chunks with invalid address - ReceivedInvalidChunk prometheus.Counter // number of delivered chunks with invalid address - Delivered prometheus.Counter // number of chunk deliveries - SentOffered prometheus.Counter // number of chunks offered - SentWanted prometheus.Counter // number of chunks wanted - Sent prometheus.Counter // number of chunks sent - DuplicateRuid prometheus.Counter // number of duplicate RUID requests we got - LastReceived *prometheus.CounterVec // last timestamp of the received chunks per bin + Offered m.Counter // number of chunks offered + Wanted m.Counter // number of chunks wanted + MissingChunks m.Counter // number of reserve get errs + ReceivedZeroAddress m.Counter // number of delivered chunks with invalid address + ReceivedInvalidChunk m.Counter // number of delivered chunks with invalid address + Delivered m.Counter // number of chunk deliveries + SentOffered m.Counter // number of chunks offered + SentWanted m.Counter // number of chunks wanted + Sent m.Counter // number of chunks sent + DuplicateRuid m.Counter // number of duplicate RUID requests we got + LastReceived m.CounterMetricVector // last timestamp of the received chunks per bin } func newMetrics() metrics { subsystem := "pullsync" return metrics{ - Offered: prometheus.NewCounter(prometheus.CounterOpts{ + Offered: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "chunks_offered", Help: "Total chunks offered.", }), - Wanted: prometheus.NewCounter(prometheus.CounterOpts{ + Wanted: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "chunks_wanted", Help: "Total chunks wanted.", }), - MissingChunks: prometheus.NewCounter(prometheus.CounterOpts{ + MissingChunks: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "missing_chunks", Help: "Total reserve get errors.", }), - ReceivedZeroAddress: prometheus.NewCounter(prometheus.CounterOpts{ + ReceivedZeroAddress: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "received_zero_address", Help: "Total chunks delivered with zero address and no chunk data.", }), - ReceivedInvalidChunk: prometheus.NewCounter(prometheus.CounterOpts{ + ReceivedInvalidChunk: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "received_invalid_chunks", Help: "Total invalid chunks delivered.", }), - Delivered: prometheus.NewCounter(prometheus.CounterOpts{ + Delivered: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "chunks_delivered", Help: "Total chunks delivered.", }), - SentOffered: prometheus.NewCounter(prometheus.CounterOpts{ + SentOffered: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "chunks_sent_offered", Help: "Total chunks offered to peers.", }), - SentWanted: prometheus.NewCounter(prometheus.CounterOpts{ + SentWanted: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "chunks_sent_wanted", Help: "Total chunks wanted by peers.", }), - Sent: prometheus.NewCounter(prometheus.CounterOpts{ + Sent: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "chunks_sent", Help: "Total chunks sent.", }), - DuplicateRuid: prometheus.NewCounter(prometheus.CounterOpts{ + DuplicateRuid: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "duplicate_ruids", Help: "Total duplicate RUIDs.", }), - LastReceived: prometheus.NewCounterVec( - prometheus.CounterOpts{ + LastReceived: m.NewCounterVec( + m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "last_received", @@ -97,6 +96,6 @@ func newMetrics() metrics { } } -func (s *Syncer) Metrics() []prometheus.Collector { +func (s *Syncer) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/pullsync/pb/pullsync.pb.go b/pkg/pullsync/pb/pullsync.pb.go index 452ffeeca8d..abe43f47a05 100644 --- a/pkg/pullsync/pb/pullsync.pb.go +++ b/pkg/pullsync/pb/pullsync.pb.go @@ -5,10 +5,11 @@ package pb import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/pusher/metrics.go b/pkg/pusher/metrics.go index 0a92951b42b..c0689906503 100644 --- a/pkg/pusher/metrics.go +++ b/pkg/pusher/metrics.go @@ -6,48 +6,47 @@ package pusher import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { - TotalToPush prometheus.Counter - TotalSynced prometheus.Counter - TotalErrors prometheus.Counter - MarkAndSweepTime prometheus.Histogram - SyncTime prometheus.Histogram - ErrorTime prometheus.Histogram + TotalToPush m.Counter + TotalSynced m.Counter + TotalErrors m.Counter + MarkAndSweepTime m.Histogram + SyncTime m.Histogram + ErrorTime m.Histogram } func newMetrics() metrics { subsystem := "pusher" return metrics{ - TotalToPush: prometheus.NewCounter(prometheus.CounterOpts{ + TotalToPush: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_to_push", Help: "Total chunks to push (chunks may be repeated).", }), - TotalSynced: prometheus.NewCounter(prometheus.CounterOpts{ + TotalSynced: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_synced", Help: "Total chunks synced successfully with valid receipts.", }), - TotalErrors: prometheus.NewCounter(prometheus.CounterOpts{ + TotalErrors: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_errors", Help: "Total errors encountered.", }), - SyncTime: prometheus.NewHistogram(prometheus.HistogramOpts{ + SyncTime: m.NewHistogram(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "sync_time", Help: "Histogram of time spent to sync a chunk.", Buckets: []float64{0.1, 0.25, 0.5, 1, 2.5, 5, 10, 60}, }), - ErrorTime: prometheus.NewHistogram(prometheus.HistogramOpts{ + ErrorTime: m.NewHistogram(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "error_time", @@ -57,6 +56,6 @@ func newMetrics() metrics { } } -func (s *Service) Metrics() []prometheus.Collector { +func (s *Service) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/pushsync/metrics.go b/pkg/pushsync/metrics.go index 7f4f5884bf0..6c6fc0180f6 100644 --- a/pkg/pushsync/metrics.go +++ b/pkg/pushsync/metrics.go @@ -6,124 +6,123 @@ package pushsync import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { - TotalSent prometheus.Counter - TotalReceived prometheus.Counter - TotalHandlerErrors prometheus.Counter - TotalRequests prometheus.Counter - TotalSendAttempts prometheus.Counter - TotalFailedSendAttempts prometheus.Counter - TotalOutgoing prometheus.Counter - TotalOutgoingErrors prometheus.Counter - InvalidStampErrors prometheus.Counter - StampValidationTime *prometheus.HistogramVec - Forwarder prometheus.Counter - Storer prometheus.Counter - TotalHandlerTime *prometheus.HistogramVec - PushToPeerTime *prometheus.HistogramVec + TotalSent m.Counter + TotalReceived m.Counter + TotalHandlerErrors m.Counter + TotalRequests m.Counter + TotalSendAttempts m.Counter + TotalFailedSendAttempts m.Counter + TotalOutgoing m.Counter + TotalOutgoingErrors m.Counter + InvalidStampErrors m.Counter + StampValidationTime m.HistogramMetricVector + Forwarder m.Counter + Storer m.Counter + TotalHandlerTime m.HistogramMetricVector + PushToPeerTime m.HistogramMetricVector - ReceiptDepth *prometheus.CounterVec - ShallowReceiptDepth *prometheus.CounterVec - ShallowReceipt prometheus.Counter + ReceiptDepth m.CounterMetricVector + ShallowReceiptDepth m.CounterMetricVector + ShallowReceipt m.Counter } func newMetrics() metrics { subsystem := "pushsync" return metrics{ - TotalSent: prometheus.NewCounter(prometheus.CounterOpts{ + TotalSent: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_sent", Help: "Total chunks sent.", }), - TotalReceived: prometheus.NewCounter(prometheus.CounterOpts{ + TotalReceived: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_received", Help: "Total chunks received.", }), - TotalHandlerErrors: prometheus.NewCounter(prometheus.CounterOpts{ + TotalHandlerErrors: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_handler_errors", Help: "Total no of error occurred while handling an incoming delivery (either while storing or forwarding).", }), - TotalRequests: prometheus.NewCounter(prometheus.CounterOpts{ + TotalRequests: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_requests", Help: "Total no of requests to push a chunk into the network (from origin nodes or not).", }), - TotalSendAttempts: prometheus.NewCounter(prometheus.CounterOpts{ + TotalSendAttempts: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_send_attempts", Help: "Total no of attempts to push chunk.", }), - TotalFailedSendAttempts: prometheus.NewCounter(prometheus.CounterOpts{ + TotalFailedSendAttempts: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_failed_send_attempts", Help: "Total no of failed attempts to push chunk.", }), - TotalOutgoing: prometheus.NewCounter(prometheus.CounterOpts{ + TotalOutgoing: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_outgoing", Help: "Total no of chunks requested to be synced (calls on exported PushChunkToClosest)", }), - TotalOutgoingErrors: prometheus.NewCounter(prometheus.CounterOpts{ + TotalOutgoingErrors: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_outgoing_errors", Help: "Total no of errors of entire operation to sync a chunk (multiple attempts included)", }), - InvalidStampErrors: prometheus.NewCounter(prometheus.CounterOpts{ + InvalidStampErrors: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "invalid_stamps", Help: "No of invalid stamp errors.", }), - StampValidationTime: prometheus.NewHistogramVec(prometheus.HistogramOpts{ + StampValidationTime: m.NewHistogramVec(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "stamp_validation_time", Help: "Time taken to validate stamps.", }, []string{"status"}), - Forwarder: prometheus.NewCounter(prometheus.CounterOpts{ + Forwarder: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "forwarder", Help: "No of times the peer is a forwarder node.", }), - Storer: prometheus.NewCounter(prometheus.CounterOpts{ + Storer: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "storer", Help: "No of times the peer is a storer node.", }), - TotalHandlerTime: prometheus.NewHistogramVec( - prometheus.HistogramOpts{ + TotalHandlerTime: m.NewHistogramVec( + m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_handler_time", Help: "Histogram for time taken for the handler.", }, []string{"status"}, ), - PushToPeerTime: prometheus.NewHistogramVec( - prometheus.HistogramOpts{ + PushToPeerTime: m.NewHistogramVec( + m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "push_peer_time", Help: "Histogram for time taken to push a chunk to a peer.", }, []string{"status"}, ), - ShallowReceiptDepth: prometheus.NewCounterVec( - prometheus.CounterOpts{ + ShallowReceiptDepth: m.NewCounterVec( + m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "shallow_receipt_depth", @@ -131,14 +130,14 @@ func newMetrics() metrics { }, []string{"depth"}, ), - ShallowReceipt: prometheus.NewCounter(prometheus.CounterOpts{ + ShallowReceipt: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "shallow_receipt", Help: "Total shallow receipts.", }), - ReceiptDepth: prometheus.NewCounterVec( - prometheus.CounterOpts{ + ReceiptDepth: m.NewCounterVec( + m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "receipt_depth", @@ -149,6 +148,6 @@ func newMetrics() metrics { } } -func (s *PushSync) Metrics() []prometheus.Collector { +func (s *PushSync) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/pushsync/pb/pushsync.pb.go b/pkg/pushsync/pb/pushsync.pb.go index 352abd774d7..11fa8468607 100644 --- a/pkg/pushsync/pb/pushsync.pb.go +++ b/pkg/pushsync/pb/pushsync.pb.go @@ -5,10 +5,11 @@ package pb import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/resolver/client/ens/ens_integration_test.go b/pkg/resolver/client/ens/ens_integration_test.go index f5aa9b1d9d7..6170e59448d 100644 --- a/pkg/resolver/client/ens/ens_integration_test.go +++ b/pkg/resolver/client/ens/ens_integration_test.go @@ -12,6 +12,7 @@ import ( "github.com/ethersphere/bee/v2/pkg/resolver/client/ens" "github.com/ethersphere/bee/v2/pkg/swarm" + "github.com/ethersphere/bee/v2/pkg/util/testutil" ) func TestENSIntegration(t *testing.T) { diff --git a/pkg/retrieval/metrics.go b/pkg/retrieval/metrics.go index f987a75b2d6..a897a10d5ee 100644 --- a/pkg/retrieval/metrics.go +++ b/pkg/retrieval/metrics.go @@ -5,8 +5,6 @@ package retrieval import ( - "github.com/prometheus/client_golang/prometheus" - m "github.com/ethersphere/bee/v2/pkg/metrics" ) @@ -15,84 +13,84 @@ type metrics struct { // to be able to return them by Metrics() // using reflection - RequestCounter prometheus.Counter - RequestSuccessCounter prometheus.Counter - RequestFailureCounter prometheus.Counter - RequestDurationTime prometheus.Histogram - RequestAttempts prometheus.Histogram - PeerRequestCounter prometheus.Counter - TotalRetrieved prometheus.Counter - InvalidChunkRetrieved prometheus.Counter - ChunkPrice prometheus.Summary - TotalErrors prometheus.Counter - ChunkRetrieveTime prometheus.Histogram + RequestCounter m.Counter + RequestSuccessCounter m.Counter + RequestFailureCounter m.Counter + RequestDurationTime m.Histogram + RequestAttempts m.Histogram + PeerRequestCounter m.Counter + TotalRetrieved m.Counter + InvalidChunkRetrieved m.Counter + ChunkPrice m.Summary + TotalErrors m.Counter + ChunkRetrieveTime m.Histogram } func newMetrics() metrics { subsystem := "retrieval" return metrics{ - RequestCounter: prometheus.NewCounter(prometheus.CounterOpts{ + RequestCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "request_count", Help: "Number of requests to retrieve chunks.", }), - RequestSuccessCounter: prometheus.NewCounter(prometheus.CounterOpts{ + RequestSuccessCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "request_success_count", Help: "Number of requests which succeeded to retrieve chunk.", }), - RequestFailureCounter: prometheus.NewCounter(prometheus.CounterOpts{ + RequestFailureCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "request_failure_count", Help: "Number of requests which failed to retrieve chunk.", }), - RequestDurationTime: prometheus.NewHistogram(prometheus.HistogramOpts{ + RequestDurationTime: m.NewHistogram(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "request_duration_time", Help: "Histogram for time taken to complete retrieval request", }), - RequestAttempts: prometheus.NewHistogram(prometheus.HistogramOpts{ + RequestAttempts: m.NewHistogram(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "request_attempts", Help: "Histogram for total retrieval attempts pre each request.", }), - PeerRequestCounter: prometheus.NewCounter(prometheus.CounterOpts{ + PeerRequestCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "peer_request_count", Help: "Number of request to single peer.", }), - TotalRetrieved: prometheus.NewCounter(prometheus.CounterOpts{ + TotalRetrieved: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_retrieved", Help: "Total chunks retrieved.", }), - InvalidChunkRetrieved: prometheus.NewCounter(prometheus.CounterOpts{ + InvalidChunkRetrieved: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "invalid_chunk_retrieved", Help: "Invalid chunk retrieved from peer.", }), - ChunkPrice: prometheus.NewSummary(prometheus.SummaryOpts{ + ChunkPrice: m.NewSummary(m.SummaryOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "chunk_price", Help: "The price of the chunk that was paid.", }), - TotalErrors: prometheus.NewCounter(prometheus.CounterOpts{ + TotalErrors: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_errors", Help: "Total number of errors while retrieving chunk.", }), - ChunkRetrieveTime: prometheus.NewHistogram(prometheus.HistogramOpts{ + ChunkRetrieveTime: m.NewHistogram(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "retrieve_chunk_time", @@ -102,13 +100,13 @@ func newMetrics() metrics { } } -func (s *Service) Metrics() []prometheus.Collector { +func (s *Service) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } // StatusMetrics exposes metrics that are exposed on the status protocol. -func (s *Service) StatusMetrics() []prometheus.Collector { - return []prometheus.Collector{ +func (s *Service) StatusMetrics() []m.Collector { + return []m.Collector{ s.metrics.RequestAttempts, s.metrics.ChunkRetrieveTime, s.metrics.RequestDurationTime, diff --git a/pkg/retrieval/pb/retrieval.pb.go b/pkg/retrieval/pb/retrieval.pb.go index 315d6d363dd..968af48ef44 100644 --- a/pkg/retrieval/pb/retrieval.pb.go +++ b/pkg/retrieval/pb/retrieval.pb.go @@ -5,10 +5,11 @@ package pb import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/salud/metrics.go b/pkg/salud/metrics.go index 8ac5abdb517..26c2eb9d323 100644 --- a/pkg/salud/metrics.go +++ b/pkg/salud/metrics.go @@ -6,89 +6,88 @@ package salud import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { - AvgDur prometheus.Gauge - PDur prometheus.Gauge - PConns prometheus.Gauge - NetworkRadius prometheus.Gauge - NeighborhoodRadius prometheus.Gauge - Commitment prometheus.Gauge - ReserveSizePercentErr prometheus.Gauge - Healthy prometheus.Counter - Unhealthy prometheus.Counter - NeighborhoodAvgDur prometheus.Gauge - NeighborCount prometheus.Gauge + AvgDur m.Gauge + PDur m.Gauge + PConns m.Gauge + NetworkRadius m.Gauge + NeighborhoodRadius m.Gauge + Commitment m.Gauge + ReserveSizePercentErr m.Gauge + Healthy m.Counter + Unhealthy m.Counter + NeighborhoodAvgDur m.Gauge + NeighborCount m.Gauge } func newMetrics() metrics { subsystem := "salud" return metrics{ - AvgDur: prometheus.NewGauge(prometheus.GaugeOpts{ + AvgDur: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "dur", Help: "Average duration for snapshot response.", }), - PDur: prometheus.NewGauge(prometheus.GaugeOpts{ + PDur: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "pdur", Help: "Percentile of durations for snapshot response.", }), - PConns: prometheus.NewGauge(prometheus.GaugeOpts{ + PConns: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "pconns", Help: "Percentile of connections counts.", }), - NetworkRadius: prometheus.NewGauge(prometheus.GaugeOpts{ + NetworkRadius: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "network_radius", Help: "Most common radius across the connected peers.", }), - NeighborhoodRadius: prometheus.NewGauge(prometheus.GaugeOpts{ + NeighborhoodRadius: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "neighborhood_radius", Help: "Most common radius across the connected peers.", }), - Healthy: prometheus.NewCounter(prometheus.CounterOpts{ + Healthy: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "healthy", Help: "Count of healthy peers.", }), - Unhealthy: prometheus.NewCounter(prometheus.CounterOpts{ + Unhealthy: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "unhealthy", Help: "Count of unhealthy peers.", }), - Commitment: prometheus.NewGauge(prometheus.GaugeOpts{ + Commitment: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "batch_commitment", Help: "Most common batch commitment.", }), - ReserveSizePercentErr: prometheus.NewGauge(prometheus.GaugeOpts{ + ReserveSizePercentErr: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "reserve_size_percentage_err", Help: "Percentage error of the reservesize relative to the network average.", }), // Neighborhood-specific metrics - NeighborhoodAvgDur: prometheus.NewGauge(prometheus.GaugeOpts{ + NeighborhoodAvgDur: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "neighborhood_dur", Help: "Average duration for snapshot response from neighborhood peers.", }), - NeighborCount: prometheus.NewGauge(prometheus.GaugeOpts{ + NeighborCount: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "neighbors", @@ -97,6 +96,6 @@ func newMetrics() metrics { } } -func (s *service) Metrics() []prometheus.Collector { +func (s *service) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/settlement/pseudosettle/metrics.go b/pkg/settlement/pseudosettle/metrics.go index 971795cb6a8..32b8174564e 100644 --- a/pkg/settlement/pseudosettle/metrics.go +++ b/pkg/settlement/pseudosettle/metrics.go @@ -6,56 +6,55 @@ package pseudosettle import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { // all metrics fields must be exported // to be able to return them by Metrics() // using reflection - TotalReceivedPseudoSettlements prometheus.Counter - TotalSentPseudoSettlements prometheus.Counter - ReceivedPseudoSettlements prometheus.Counter - SentPseudoSettlements prometheus.Counter - ReceivedPseudoSettlementsErrors prometheus.Counter - SentPseudoSettlementsErrors prometheus.Counter + TotalReceivedPseudoSettlements m.Counter + TotalSentPseudoSettlements m.Counter + ReceivedPseudoSettlements m.Counter + SentPseudoSettlements m.Counter + ReceivedPseudoSettlementsErrors m.Counter + SentPseudoSettlementsErrors m.Counter } func newMetrics() metrics { subsystem := "pseudosettle" return metrics{ - TotalReceivedPseudoSettlements: prometheus.NewCounter(prometheus.CounterOpts{ + TotalReceivedPseudoSettlements: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_received_pseudosettlements", Help: "Amount of time settlements received from peers (income of the node)", }), - TotalSentPseudoSettlements: prometheus.NewCounter(prometheus.CounterOpts{ + TotalSentPseudoSettlements: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_sent_pseudosettlements", Help: "Amount of of time settlements sent to peers (costs paid by the node)", }), - ReceivedPseudoSettlements: prometheus.NewCounter(prometheus.CounterOpts{ + ReceivedPseudoSettlements: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "received_pseudosettlements", Help: "Number of time settlements received from peers", }), - SentPseudoSettlements: prometheus.NewCounter(prometheus.CounterOpts{ + SentPseudoSettlements: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "sent_pseudosettlements", Help: "Number of time settlements sent to peers", }), - ReceivedPseudoSettlementsErrors: prometheus.NewCounter(prometheus.CounterOpts{ + ReceivedPseudoSettlementsErrors: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "received_pseudosettlements_errors", Help: "Errors of time settlements received from peers", }), - SentPseudoSettlementsErrors: prometheus.NewCounter(prometheus.CounterOpts{ + SentPseudoSettlementsErrors: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "sent_pseudosettlements_errorss", @@ -64,6 +63,6 @@ func newMetrics() metrics { } } -func (s *Service) Metrics() []prometheus.Collector { +func (s *Service) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/settlement/pseudosettle/pb/pseudosettle.pb.go b/pkg/settlement/pseudosettle/pb/pseudosettle.pb.go index 41d81fbd1c6..b1088896979 100644 --- a/pkg/settlement/pseudosettle/pb/pseudosettle.pb.go +++ b/pkg/settlement/pseudosettle/pb/pseudosettle.pb.go @@ -5,10 +5,11 @@ package pb import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/settlement/swap/chequebook/factory.go b/pkg/settlement/swap/chequebook/factory.go index f548643bac1..c349f4cddce 100644 --- a/pkg/settlement/swap/chequebook/factory.go +++ b/pkg/settlement/swap/chequebook/factory.go @@ -10,6 +10,7 @@ import ( "math/big" "context" + "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethersphere/bee/v2/pkg/sctx" diff --git a/pkg/settlement/swap/metrics.go b/pkg/settlement/swap/metrics.go index 2e6fb1a4bb6..a7ab3d13708 100644 --- a/pkg/settlement/swap/metrics.go +++ b/pkg/settlement/swap/metrics.go @@ -6,53 +6,52 @@ package swap import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { - TotalReceived prometheus.Counter - TotalSent prometheus.Counter - ChequesReceived prometheus.Counter - ChequesSent prometheus.Counter - ChequesRejected prometheus.Counter - AvailableBalance prometheus.Gauge + TotalReceived m.Counter + TotalSent m.Counter + ChequesReceived m.Counter + ChequesSent m.Counter + ChequesRejected m.Counter + AvailableBalance m.Gauge } func newMetrics() metrics { subsystem := "swap" return metrics{ - TotalReceived: prometheus.NewCounter(prometheus.CounterOpts{ + TotalReceived: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_received", Help: "Amount of tokens received from peers (income of the node)", }), - TotalSent: prometheus.NewCounter(prometheus.CounterOpts{ + TotalSent: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_sent", Help: "Amount of tokens sent to peers (costs paid by the node)", }), - ChequesReceived: prometheus.NewCounter(prometheus.CounterOpts{ + ChequesReceived: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "cheques_received", Help: "Number of cheques received from peers", }), - ChequesSent: prometheus.NewCounter(prometheus.CounterOpts{ + ChequesSent: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "cheques_sent", Help: "Number of cheques sent to peers", }), - ChequesRejected: prometheus.NewCounter(prometheus.CounterOpts{ + ChequesRejected: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "cheques_rejected", Help: "Number of cheques rejected", }), - AvailableBalance: prometheus.NewGauge(prometheus.GaugeOpts{ + AvailableBalance: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "available_balance", @@ -61,6 +60,6 @@ func newMetrics() metrics { } } -func (s *Service) Metrics() []prometheus.Collector { +func (s *Service) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/settlement/swap/swapprotocol/export_test.go b/pkg/settlement/swap/swapprotocol/export_test.go index 31fc9e6b79f..7a7c31ab773 100644 --- a/pkg/settlement/swap/swapprotocol/export_test.go +++ b/pkg/settlement/swap/swapprotocol/export_test.go @@ -6,6 +6,7 @@ package swapprotocol import ( "context" + "github.com/ethersphere/bee/v2/pkg/p2p" ) diff --git a/pkg/settlement/swap/swapprotocol/pb/swap.pb.go b/pkg/settlement/swap/swapprotocol/pb/swap.pb.go index 5cf6ba49fd9..cf8279f49b1 100644 --- a/pkg/settlement/swap/swapprotocol/pb/swap.pb.go +++ b/pkg/settlement/swap/swapprotocol/pb/swap.pb.go @@ -5,10 +5,11 @@ package pb import ( fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/sharky/metrics.go b/pkg/sharky/metrics.go index 53db4675b06..9cebbe24d4c 100644 --- a/pkg/sharky/metrics.go +++ b/pkg/sharky/metrics.go @@ -6,22 +6,21 @@ package sharky import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) // metrics groups sharky related prometheus counters. type metrics struct { - TotalWriteCalls prometheus.Counter - TotalWriteCallsErr prometheus.Counter - TotalReadCalls prometheus.Counter - TotalReadCallsErr prometheus.Counter - TotalReleaseCalls prometheus.Counter - TotalReleaseCallsErr prometheus.Counter - ShardCount prometheus.Gauge - CurrentShardSize *prometheus.GaugeVec - ShardFragmentation *prometheus.GaugeVec - LastAllocatedShardSlot *prometheus.GaugeVec - LastReleasedShardSlot *prometheus.GaugeVec + TotalWriteCalls m.Counter + TotalWriteCallsErr m.Counter + TotalReadCalls m.Counter + TotalReadCallsErr m.Counter + TotalReleaseCalls m.Counter + TotalReleaseCallsErr m.Counter + ShardCount m.Gauge + CurrentShardSize m.GaugeMetricVector + ShardFragmentation m.GaugeMetricVector + LastAllocatedShardSlot m.GaugeMetricVector + LastReleasedShardSlot m.GaugeMetricVector } // newMetrics is a convenient constructor for creating new metrics. @@ -29,50 +28,50 @@ func newMetrics() metrics { const subsystem = "sharky" return metrics{ - TotalWriteCalls: prometheus.NewCounter(prometheus.CounterOpts{ + TotalWriteCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_write_calls", Help: "The total write calls made.", }), - TotalWriteCallsErr: prometheus.NewCounter(prometheus.CounterOpts{ + TotalWriteCallsErr: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_write_calls_err", Help: "The total write calls ended up with error.", }), - TotalReadCalls: prometheus.NewCounter(prometheus.CounterOpts{ + TotalReadCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_read_calls", Help: "The total read calls made.", }), - TotalReadCallsErr: prometheus.NewCounter(prometheus.CounterOpts{ + TotalReadCallsErr: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_read_calls_err", Help: "The total read calls ended up with error.", }), - TotalReleaseCalls: prometheus.NewCounter(prometheus.CounterOpts{ + TotalReleaseCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_release_calls", Help: "The total release calls made.", }), - TotalReleaseCallsErr: prometheus.NewCounter(prometheus.CounterOpts{ + TotalReleaseCallsErr: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_release_calls_err", Help: "The total release calls ended up with error.", }), - ShardCount: prometheus.NewGauge(prometheus.GaugeOpts{ + ShardCount: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "shard_count", Help: "The number of shards.", }), - CurrentShardSize: prometheus.NewGaugeVec( - prometheus.GaugeOpts{ + CurrentShardSize: m.NewGaugeVec( + m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "current_shard_size", @@ -80,8 +79,8 @@ func newMetrics() metrics { }, []string{"current_shard_size"}, ), - ShardFragmentation: prometheus.NewGaugeVec( - prometheus.GaugeOpts{ + ShardFragmentation: m.NewGaugeVec( + m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "shard_fragmentation", @@ -91,8 +90,8 @@ between actual lengths of chunks and the length of slot. `, }, []string{"shard_fragmentation"}, ), - LastAllocatedShardSlot: prometheus.NewGaugeVec( - prometheus.GaugeOpts{ + LastAllocatedShardSlot: m.NewGaugeVec( + m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "last_allocated_shard_slot", @@ -100,8 +99,8 @@ between actual lengths of chunks and the length of slot. }, []string{"shard_slot_no"}, ), - LastReleasedShardSlot: prometheus.NewGaugeVec( - prometheus.GaugeOpts{ + LastReleasedShardSlot: m.NewGaugeVec( + m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "last_released_shard_slot", @@ -113,6 +112,6 @@ between actual lengths of chunks and the length of slot. } // Metrics returns set of prometheus collectors. -func (s *Store) Metrics() []prometheus.Collector { +func (s *Store) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/shed/metrics.go b/pkg/shed/metrics.go index 58820407c9d..4a53e48866f 100644 --- a/pkg/shed/metrics.go +++ b/pkg/shed/metrics.go @@ -6,98 +6,97 @@ package shed import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { // all metrics fields must be exported // to be able to return them by Metrics() // using reflection - PutCounter prometheus.Counter - PutFailCounter prometheus.Counter - GetCounter prometheus.Counter - GetFailCounter prometheus.Counter - GetNotFoundCounter prometheus.Counter - HasCounter prometheus.Counter - HasFailCounter prometheus.Counter - DeleteCounter prometheus.Counter - DeleteFailCounter prometheus.Counter - IteratorCounter prometheus.Counter - WriteBatchCounter prometheus.Counter - WriteBatchFailCounter prometheus.Counter + PutCounter m.Counter + PutFailCounter m.Counter + GetCounter m.Counter + GetFailCounter m.Counter + GetNotFoundCounter m.Counter + HasCounter m.Counter + HasFailCounter m.Counter + DeleteCounter m.Counter + DeleteFailCounter m.Counter + IteratorCounter m.Counter + WriteBatchCounter m.Counter + WriteBatchFailCounter m.Counter } func newMetrics() metrics { subsystem := "shed" return metrics{ - PutCounter: prometheus.NewCounter(prometheus.CounterOpts{ + PutCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "put_count", Help: "Number of times the PUT operation is done.", }), - PutFailCounter: prometheus.NewCounter(prometheus.CounterOpts{ + PutFailCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "put_fail_count", Help: "Number of times the PUT operation failed.", }), - GetCounter: prometheus.NewCounter(prometheus.CounterOpts{ + GetCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "get_count", Help: "Number of times the GET operation is done.", }), - GetNotFoundCounter: prometheus.NewCounter(prometheus.CounterOpts{ + GetNotFoundCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "get_not_found_count", Help: "Number of times the GET operation could not find key.", }), - GetFailCounter: prometheus.NewCounter(prometheus.CounterOpts{ + GetFailCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "get_fail_count", Help: "Number of times the GET operation is failed.", }), - HasCounter: prometheus.NewCounter(prometheus.CounterOpts{ + HasCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "has_count", Help: "Number of times the HAS operation is done.", }), - HasFailCounter: prometheus.NewCounter(prometheus.CounterOpts{ + HasFailCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "has_fail_count", Help: "Number of times the HAS operation failed.", }), - DeleteCounter: prometheus.NewCounter(prometheus.CounterOpts{ + DeleteCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "delete_count", Help: "Number of times the DELETE operation is done.", }), - DeleteFailCounter: prometheus.NewCounter(prometheus.CounterOpts{ + DeleteFailCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "delete_fail_count", Help: "Number of times the DELETE operation failed.", }), - IteratorCounter: prometheus.NewCounter(prometheus.CounterOpts{ + IteratorCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "iterator_count", Help: "Number of times the ITERATOR operation is done.", }), - WriteBatchCounter: prometheus.NewCounter(prometheus.CounterOpts{ + WriteBatchCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "write_batch_count", Help: "Number of times the WRITE_BATCH operation is done.", }), - WriteBatchFailCounter: prometheus.NewCounter(prometheus.CounterOpts{ + WriteBatchFailCounter: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "write_batch_fail_count", @@ -106,6 +105,6 @@ func newMetrics() metrics { } } -func (s *DB) Metrics() []prometheus.Collector { +func (s *DB) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } diff --git a/pkg/status/internal/pb/status.pb.go b/pkg/status/internal/pb/status.pb.go index 225d292ab4c..b22ca9b71c3 100644 --- a/pkg/status/internal/pb/status.pb.go +++ b/pkg/status/internal/pb/status.pb.go @@ -6,10 +6,11 @@ package pb import ( encoding_binary "encoding/binary" fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/status/status.go b/pkg/status/status.go index 7fb5e46a592..3fc686fa957 100644 --- a/pkg/status/status.go +++ b/pkg/status/status.go @@ -10,14 +10,13 @@ import ( "strings" "github.com/ethersphere/bee/v2/pkg/log" + m "github.com/ethersphere/bee/v2/pkg/metrics" "github.com/ethersphere/bee/v2/pkg/p2p" "github.com/ethersphere/bee/v2/pkg/p2p/protobuf" "github.com/ethersphere/bee/v2/pkg/postage" "github.com/ethersphere/bee/v2/pkg/status/internal/pb" "github.com/ethersphere/bee/v2/pkg/swarm" "github.com/ethersphere/bee/v2/pkg/topology" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/common/expfmt" ) // loggerName is the tree path name of the logger for this package. @@ -60,12 +59,12 @@ type Service struct { reserve Reserve sync SyncReporter chainState postage.ChainStateGetter - metricsRegistry *prometheus.Registry + metricsRegistry m.MetricsRegistererGatherer } type Metrics struct { - UploadSpeed prometheus.Histogram - DownloadSpeed prometheus.Histogram + UploadSpeed m.Histogram + DownloadSpeed m.Histogram } // NewService creates a new status service. @@ -76,7 +75,7 @@ func NewService( beeMode string, chainState postage.ChainStateGetter, reserve Reserve, - metricsRegistry *prometheus.Registry, + metricsRegistry m.MetricsRegistererGatherer, ) *Service { return &Service{ logger: logger.WithName(loggerName).Register(), @@ -233,13 +232,13 @@ func (s *Service) encodeMetrics() (map[string]string, error) { } metrics := make(map[string]string, len(metricFamilies)) - for _, m := range metricFamilies { + for _, mf := range metricFamilies { var metricsBuilder strings.Builder - encoder := expfmt.NewEncoder(&metricsBuilder, expfmt.NewFormat(expfmt.TypeTextPlain)) - if err := encoder.Encode(m); err != nil { - return nil, fmt.Errorf("encode metric %s: %w", m.GetName(), err) + encoder := m.NewEncoder(&metricsBuilder, m.NewFormat(m.TypeTextPlain)) + if err := encoder.Encode(mf); err != nil { + return nil, fmt.Errorf("encode metric %s: %w", mf.GetName(), err) } - metrics[m.GetName()] = metricsBuilder.String() + metrics[mf.GetName()] = metricsBuilder.String() } return metrics, nil diff --git a/pkg/status/status_test.go b/pkg/status/status_test.go index 2b3f9383fe3..59f80ac501b 100644 --- a/pkg/status/status_test.go +++ b/pkg/status/status_test.go @@ -2,6 +2,9 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +//go:build !nometrics +// +build !nometrics + package status_test import ( diff --git a/pkg/storage/cache/metrics.go b/pkg/storage/cache/metrics.go index ec9a6051786..f27e27934df 100644 --- a/pkg/storage/cache/metrics.go +++ b/pkg/storage/cache/metrics.go @@ -6,25 +6,24 @@ package cache import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { - CacheHit prometheus.Counter - CacheMiss prometheus.Counter + CacheHit m.Counter + CacheMiss m.Counter } func newMetrics() metrics { subsystem := "storage_cache" return metrics{ - CacheHit: prometheus.NewCounter(prometheus.CounterOpts{ + CacheHit: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "cache_hit", Help: "Total cache hits.", }), - CacheMiss: prometheus.NewCounter(prometheus.CounterOpts{ + CacheMiss: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "cache_miss", @@ -33,6 +32,6 @@ func newMetrics() metrics { } } -func (c *Cache) Metrics() []prometheus.Collector { +func (c *Cache) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(c.metrics) } diff --git a/pkg/storageincentives/metrics.go b/pkg/storageincentives/metrics.go index b376d9d20b2..125af999a05 100644 --- a/pkg/storageincentives/metrics.go +++ b/pkg/storageincentives/metrics.go @@ -6,86 +6,85 @@ package storageincentives import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { // phase gauge and counter - CurrentPhase prometheus.Gauge - RevealPhase prometheus.Counter - CommitPhase prometheus.Counter - ClaimPhase prometheus.Counter - Winner prometheus.Counter - NeighborhoodSelected prometheus.Counter - SampleDuration prometheus.Gauge - Round prometheus.Gauge - InsufficientFundsToPlay prometheus.Counter + CurrentPhase m.Gauge + RevealPhase m.Counter + CommitPhase m.Counter + ClaimPhase m.Counter + Winner m.Counter + NeighborhoodSelected m.Counter + SampleDuration m.Gauge + Round m.Gauge + InsufficientFundsToPlay m.Counter // total calls to chain backend - BackendCalls prometheus.Counter - BackendErrors prometheus.Counter + BackendCalls m.Counter + BackendErrors m.Counter // metrics for err processing - ErrReveal prometheus.Counter - ErrCommit prometheus.Counter - ErrClaim prometheus.Counter - ErrWinner prometheus.Counter - ErrCheckIsPlaying prometheus.Counter + ErrReveal m.Counter + ErrCommit m.Counter + ErrClaim m.Counter + ErrWinner m.Counter + ErrCheckIsPlaying m.Counter } func newMetrics() metrics { subsystem := "storageincentives" return metrics{ - CurrentPhase: prometheus.NewGauge(prometheus.GaugeOpts{ + CurrentPhase: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "current_phase", Help: "Enum value of the current phase.", }), - RevealPhase: prometheus.NewCounter(prometheus.CounterOpts{ + RevealPhase: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "reveal_phases", Help: "Count of reveal phases entered.", }), - CommitPhase: prometheus.NewCounter(prometheus.CounterOpts{ + CommitPhase: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "commit_phases", Help: "Count of commit phases entered.", }), - InsufficientFundsToPlay: prometheus.NewCounter(prometheus.CounterOpts{ + InsufficientFundsToPlay: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "insufficient_funds_to_play", Help: "Count of games skipped due to insufficient balance to participate.", }), - ClaimPhase: prometheus.NewCounter(prometheus.CounterOpts{ + ClaimPhase: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "claim_phases", Help: "Count of claim phases entered.", }), - Winner: prometheus.NewCounter(prometheus.CounterOpts{ + Winner: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "winner", Help: "Count of won rounds.", }), - NeighborhoodSelected: prometheus.NewCounter(prometheus.CounterOpts{ + NeighborhoodSelected: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "neighborhood_selected", Help: "Count of the neighborhood being selected.", }), - SampleDuration: prometheus.NewGauge(prometheus.GaugeOpts{ + SampleDuration: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "reserve_sample_duration", Help: "Time taken to produce a reserve sample.", }), - Round: prometheus.NewGauge(prometheus.GaugeOpts{ + Round: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "round", @@ -93,13 +92,13 @@ func newMetrics() metrics { }), // total call - BackendCalls: prometheus.NewCounter(prometheus.CounterOpts{ + BackendCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "backend_calls", Help: "total chain backend calls", }), - BackendErrors: prometheus.NewCounter(prometheus.CounterOpts{ + BackendErrors: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "backend_errors", @@ -107,31 +106,31 @@ func newMetrics() metrics { }), // phase errors - ErrReveal: prometheus.NewCounter(prometheus.CounterOpts{ + ErrReveal: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "reveal_phase_errors", Help: "total reveal phase errors while processing", }), - ErrCommit: prometheus.NewCounter(prometheus.CounterOpts{ + ErrCommit: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "commit_phase_errors", Help: "total commit phase errors while processing", }), - ErrClaim: prometheus.NewCounter(prometheus.CounterOpts{ + ErrClaim: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "claim_phase_errors", Help: "total claim phase errors while processing", }), - ErrWinner: prometheus.NewCounter(prometheus.CounterOpts{ + ErrWinner: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "win_phase_errors", Help: "total win phase while processing", }), - ErrCheckIsPlaying: prometheus.NewCounter(prometheus.CounterOpts{ + ErrCheckIsPlaying: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "is_playing_errors", @@ -141,6 +140,6 @@ func newMetrics() metrics { } // TODO: register metric -func (a *Agent) Metrics() []prometheus.Collector { +func (a *Agent) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(a.metrics) } diff --git a/pkg/storer/internal/transaction/metrics.go b/pkg/storer/internal/transaction/metrics.go index 39434881d8d..abe91c5bbe3 100644 --- a/pkg/storer/internal/transaction/metrics.go +++ b/pkg/storer/internal/transaction/metrics.go @@ -6,12 +6,11 @@ package transaction import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { - MethodCalls *prometheus.CounterVec - MethodDuration *prometheus.HistogramVec + MethodCalls m.CounterMetricVector + MethodDuration m.HistogramMetricVector } // newMetrics is a convenient constructor for creating new metrics. @@ -19,8 +18,8 @@ func newMetrics() metrics { const subsystem = "transaction" return metrics{ - MethodCalls: prometheus.NewCounterVec( - prometheus.CounterOpts{ + MethodCalls: m.NewCounterVec( + m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "method_calls", @@ -28,8 +27,8 @@ func newMetrics() metrics { }, []string{"method", "status"}, ), - MethodDuration: prometheus.NewHistogramVec( - prometheus.HistogramOpts{ + MethodDuration: m.NewHistogramVec( + m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "method_duration", diff --git a/pkg/storer/internal/transaction/transaction.go b/pkg/storer/internal/transaction/transaction.go index ae97e06ce3b..5feb68e9969 100644 --- a/pkg/storer/internal/transaction/transaction.go +++ b/pkg/storer/internal/transaction/transaction.go @@ -31,7 +31,6 @@ import ( "github.com/ethersphere/bee/v2/pkg/storage" "github.com/ethersphere/bee/v2/pkg/storer/internal/chunkstore" "github.com/ethersphere/bee/v2/pkg/swarm" - "github.com/prometheus/client_golang/prometheus" "resenje.org/multex" ) @@ -142,13 +141,13 @@ func (s *store) Run(ctx context.Context, f func(Store) error) error { } // Metrics returns set of prometheus collectors. -func (s *store) Metrics() []prometheus.Collector { +func (s *store) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(s.metrics) } // StatusMetrics exposes metrics that are exposed on the status protocol. -func (s *store) StatusMetrics() []prometheus.Collector { - return []prometheus.Collector{ +func (s *store) StatusMetrics() []m.Collector { + return []m.Collector{ s.metrics.MethodDuration, } } diff --git a/pkg/storer/metrics.go b/pkg/storer/metrics.go index 68be82ac039..2112f73908d 100644 --- a/pkg/storer/metrics.go +++ b/pkg/storer/metrics.go @@ -12,29 +12,29 @@ import ( m "github.com/ethersphere/bee/v2/pkg/metrics" "github.com/ethersphere/bee/v2/pkg/storage" "github.com/ethersphere/bee/v2/pkg/swarm" - "github.com/prometheus/client_golang/prometheus" ) // metrics groups storer related prometheus counters. type metrics struct { - MethodCalls *prometheus.CounterVec - MethodCallsDuration *prometheus.HistogramVec - ReserveSize prometheus.Gauge - ReserveSizeWithinRadius prometheus.Gauge - ReserveCleanup prometheus.Counter - StorageRadius prometheus.Gauge - CacheSize prometheus.Gauge - EvictedChunkCount prometheus.Counter - ExpiredChunkCount prometheus.Counter - OverCapTriggerCount prometheus.Counter - ExpiredBatchCount prometheus.Counter - LevelDBStats *prometheus.HistogramVec - ExpiryTriggersCount prometheus.Counter - ExpiryRunsCount prometheus.Counter - ReserveMissingBatch prometheus.Gauge - ReserveSampleDuration *prometheus.HistogramVec - ReserveSampleRunSummary *prometheus.GaugeVec - ReserveSampleLastRunTimestamp prometheus.Gauge + MethodCalls m.CounterMetricVector + MethodCallsDuration m.HistogramMetricVector + ReserveSize m.Gauge + ReserveSizeWithinRadius m.Gauge + ReserveCleanup m.Counter + StorageRadius m.Gauge + CacheSize m.Gauge + EvictedChunkCount m.Counter + ExpiredChunkCount m.Counter + OverCapTriggerCount m.Counter + ExpiredBatchCount m.Counter + LevelDBStats m.HistogramMetricVector + ExpiryTriggersCount m.Counter + ExpiryRunsCount m.Counter + + ReserveMissingBatch m.Gauge + ReserveSampleDuration m.HistogramMetricVector + ReserveSampleRunSummary m.GaugeMetricVector + ReserveSampleLastRunTimestamp m.Gauge } // newMetrics is a convenient constructor for creating new metrics. @@ -42,8 +42,8 @@ func newMetrics() metrics { const subsystem = "localstore" return metrics{ - MethodCalls: prometheus.NewCounterVec( - prometheus.CounterOpts{ + MethodCalls: m.NewCounterVec( + m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "method_calls", @@ -51,8 +51,8 @@ func newMetrics() metrics { }, []string{"component", "method", "status"}, ), - MethodCallsDuration: prometheus.NewHistogramVec( - prometheus.HistogramOpts{ + MethodCallsDuration: m.NewHistogramVec( + m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "method_calls_duration", @@ -60,88 +60,88 @@ func newMetrics() metrics { }, []string{"component", "method"}, ), - ReserveSize: prometheus.NewGauge( - prometheus.GaugeOpts{ + ReserveSize: m.NewGauge( + m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "reserve_size", Help: "Number of chunks in reserve.", }, ), - ReserveMissingBatch: prometheus.NewGauge( - prometheus.GaugeOpts{ + ReserveMissingBatch: m.NewGauge( + m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "reserve_missing_batch", Help: "Number of chunks in reserve with missing batches.", }, ), - ReserveSizeWithinRadius: prometheus.NewGauge( - prometheus.GaugeOpts{ + ReserveSizeWithinRadius: m.NewGauge( + m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "reserve_size_within_radius", Help: "Number of chunks in reserve with proximity >= storage radius.", }, ), - ReserveCleanup: prometheus.NewCounter( - prometheus.CounterOpts{ + ReserveCleanup: m.NewCounter( + m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "reserve_cleanup", Help: "Number of cleaned-up expired chunks.", }, ), - StorageRadius: prometheus.NewGauge( - prometheus.GaugeOpts{ + StorageRadius: m.NewGauge( + m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "storage_radius", Help: "Radius of responsibility reserve storage.", }, ), - CacheSize: prometheus.NewGauge( - prometheus.GaugeOpts{ + CacheSize: m.NewGauge( + m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "cache_size", Help: "Number of chunks in cache.", }, ), - EvictedChunkCount: prometheus.NewCounter( - prometheus.CounterOpts{ + EvictedChunkCount: m.NewCounter( + m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "evicted_count", Help: "Number of chunks evicted from reserve.", }, ), - ExpiredChunkCount: prometheus.NewCounter( - prometheus.CounterOpts{ + ExpiredChunkCount: m.NewCounter( + m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "expired_count", Help: "Number of chunks expired from reserve due to stamp expirations.", }, ), - OverCapTriggerCount: prometheus.NewCounter( - prometheus.CounterOpts{ + OverCapTriggerCount: m.NewCounter( + m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "over_cap_trigger_count", Help: "Number of times the reserve was over capacity and triggered an eviction.", }, ), - ExpiredBatchCount: prometheus.NewCounter( - prometheus.CounterOpts{ + ExpiredBatchCount: m.NewCounter( + m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "expired_batch_count", Help: "Number of batches expired, that were processed.", }, ), - LevelDBStats: prometheus.NewHistogramVec( - prometheus.HistogramOpts{ + LevelDBStats: m.NewHistogramVec( + m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "leveldb_stats", @@ -149,24 +149,24 @@ func newMetrics() metrics { }, []string{"counter"}, ), - ExpiryTriggersCount: prometheus.NewCounter( - prometheus.CounterOpts{ + ExpiryTriggersCount: m.NewCounter( + m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "expiry_trigger_count", Help: "Number of batches expiry triggers.", }, ), - ExpiryRunsCount: prometheus.NewCounter( - prometheus.CounterOpts{ + ExpiryRunsCount: m.NewCounter( + m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "expiry_run_count", Help: "Number of times the expiry worker was fired.", }, ), - ReserveSampleDuration: prometheus.NewHistogramVec( - prometheus.HistogramOpts{ + ReserveSampleDuration: m.NewHistogramVec( + m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "reserve_sample_duration_seconds", @@ -175,8 +175,8 @@ func newMetrics() metrics { }, []string{"status"}, ), - ReserveSampleRunSummary: prometheus.NewGaugeVec( - prometheus.GaugeOpts{ + ReserveSampleRunSummary: m.NewGaugeVec( + m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "reserve_sample_run_summary", @@ -184,8 +184,8 @@ func newMetrics() metrics { }, []string{"metric"}, ), - ReserveSampleLastRunTimestamp: prometheus.NewGauge( - prometheus.GaugeOpts{ + ReserveSampleLastRunTimestamp: m.NewGauge( + m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "reserve_sample_last_run_timestamp", diff --git a/pkg/storer/storer.go b/pkg/storer/storer.go index e3318c9b278..04673558ddd 100644 --- a/pkg/storer/storer.go +++ b/pkg/storer/storer.go @@ -40,7 +40,6 @@ import ( "github.com/ethersphere/bee/v2/pkg/topology" "github.com/ethersphere/bee/v2/pkg/tracing" "github.com/ethersphere/bee/v2/pkg/util/syncutil" - "github.com/prometheus/client_golang/prometheus" "github.com/spf13/afero" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/filter" @@ -295,7 +294,7 @@ func initDiskRepository( if opts.LdbStats.Load() != nil { go func() { - ldbStats := opts.LdbStats.Load() + ldbStats := opts.LdbStats.Load().Vec logger := log.NewLogger(loggerName).Register() ticker := time.NewTicker(15 * time.Second) defer ticker.Stop() @@ -370,10 +369,14 @@ func initDiskRepository( const lockKeyNewSession string = "new_session" +type HistogramVecBox struct { + Vec m.HistogramMetricVector +} + // Options provides a container to configure different things in the storer. type Options struct { // These are options related to levelDB. Currently, the underlying storage used is levelDB. - LdbStats atomic.Pointer[prometheus.HistogramVec] + LdbStats atomic.Pointer[HistogramVecBox] LdbOpenFilesLimit uint64 LdbBlockCacheCapacity uint64 LdbWriteBufferSize uint64 @@ -480,7 +483,7 @@ func New(ctx context.Context, dirPath string, opts *Options) (*DB, error) { lock := multex.New() metrics := newMetrics() - opts.LdbStats.CompareAndSwap(nil, metrics.LevelDBStats) + opts.LdbStats.CompareAndSwap(nil, &HistogramVecBox{metrics.LevelDBStats}) if dirPath == "" { st, dbCloser, err = initInmemRepository() @@ -601,22 +604,22 @@ func (db *DB) ResetReserve(ctx context.Context) error { } // Metrics returns set of prometheus collectors. -func (db *DB) Metrics() []prometheus.Collector { +func (db *DB) Metrics() []m.Collector { collectors := m.PrometheusCollectorsFromFields(db.metrics) - if v, ok := db.storage.(m.Collector); ok { + if v, ok := db.storage.(m.MetricsCollector); ok { collectors = append(collectors, v.Metrics()...) } return collectors } // StatusMetrics exposes metrics that are exposed on the status protocol. -func (db *DB) StatusMetrics() []prometheus.Collector { - collectors := []prometheus.Collector{ +func (db *DB) StatusMetrics() []m.Collector { + collectors := []m.Collector{ db.metrics.MethodCallsDuration, } type Collector interface { - StatusMetrics() []prometheus.Collector + StatusMetrics() []m.Collector } if v, ok := db.storage.(Collector); ok { diff --git a/pkg/topology/kademlia/metrics.go b/pkg/topology/kademlia/metrics.go index 7fc9a53751e..5fd5054b0c4 100644 --- a/pkg/topology/kademlia/metrics.go +++ b/pkg/topology/kademlia/metrics.go @@ -6,31 +6,30 @@ package kademlia import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) -// metrics groups kademlia related prometheus counters. +// metrics groups kademlia related m counters. type metrics struct { - PickCalls prometheus.Counter - PickCallsFalse prometheus.Counter - CurrentDepth prometheus.Gauge - CurrentStorageDepth prometheus.Gauge - CurrentlyKnownPeers prometheus.Gauge - CurrentlyConnectedPeers prometheus.Gauge - InternalMetricsFlushTime prometheus.Histogram - InternalMetricsFlushTotalErrors prometheus.Counter - TotalBeforeExpireWaits prometheus.Counter - TotalInboundConnections prometheus.Counter - TotalInboundDisconnections prometheus.Counter - TotalOutboundConnections prometheus.Counter - TotalOutboundConnectionAttempts prometheus.Counter - TotalOutboundConnectionFailedAttempts prometheus.Counter - TotalBootNodesConnectionAttempts prometheus.Counter - StartAddAddressBookOverlaysTime prometheus.Histogram - PeerLatencyEWMA prometheus.Histogram - Blocklist prometheus.Counter - ReachabilityStatus *prometheus.GaugeVec - PeersReachabilityStatus *prometheus.GaugeVec + PickCalls m.Counter + PickCallsFalse m.Counter + CurrentDepth m.Gauge + CurrentStorageDepth m.Gauge + CurrentlyKnownPeers m.Gauge + CurrentlyConnectedPeers m.Gauge + InternalMetricsFlushTime m.Histogram + InternalMetricsFlushTotalErrors m.Counter + TotalBeforeExpireWaits m.Counter + TotalInboundConnections m.Counter + TotalInboundDisconnections m.Counter + TotalOutboundConnections m.Counter + TotalOutboundConnectionAttempts m.Counter + TotalOutboundConnectionFailedAttempts m.Counter + TotalBootNodesConnectionAttempts m.Counter + StartAddAddressBookOverlaysTime m.Histogram + PeerLatencyEWMA m.Histogram + Blocklist m.Counter + ReachabilityStatus m.GaugeMetricVector + PeersReachabilityStatus m.GaugeMetricVector } // newMetrics is a convenient constructor for creating new metrics. @@ -38,116 +37,116 @@ func newMetrics() metrics { const subsystem = "kademlia" return metrics{ - PickCalls: prometheus.NewCounter(prometheus.CounterOpts{ + PickCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "pick_calls", Help: "The number of pick method call made.", }), - PickCallsFalse: prometheus.NewCounter(prometheus.CounterOpts{ + PickCallsFalse: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "pick_calls_false", Help: "The number of pick method call made which returned false.", }), - CurrentDepth: prometheus.NewGauge(prometheus.GaugeOpts{ + CurrentDepth: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "current_depth", Help: "The current value of depth.", }), - CurrentStorageDepth: prometheus.NewGauge(prometheus.GaugeOpts{ + CurrentStorageDepth: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "current_storage_depth", Help: "The current value of storage depth.", }), - CurrentlyKnownPeers: prometheus.NewGauge(prometheus.GaugeOpts{ + CurrentlyKnownPeers: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "currently_known_peers", Help: "Number of currently known peers.", }), - CurrentlyConnectedPeers: prometheus.NewGauge(prometheus.GaugeOpts{ + CurrentlyConnectedPeers: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "currently_connected_peers", Help: "Number of currently connected peers.", }), - InternalMetricsFlushTime: prometheus.NewHistogram(prometheus.HistogramOpts{ + InternalMetricsFlushTime: m.NewHistogram(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "internal_metrics_flush_time", Help: "The time spent flushing the internal metrics about peers to the state-store.", }), - InternalMetricsFlushTotalErrors: prometheus.NewCounter(prometheus.CounterOpts{ + InternalMetricsFlushTotalErrors: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "internal_metrics_flush_total_errors", Help: "Number of total errors occurred during flushing the internal metrics to the state-store.", }), - TotalBeforeExpireWaits: prometheus.NewCounter(prometheus.CounterOpts{ + TotalBeforeExpireWaits: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_before_expire_waits", Help: "Total before expire waits made.", }), - TotalInboundConnections: prometheus.NewCounter(prometheus.CounterOpts{ + TotalInboundConnections: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_inbound_connections", Help: "Total inbound connections made.", }), - TotalInboundDisconnections: prometheus.NewCounter(prometheus.CounterOpts{ + TotalInboundDisconnections: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_inbound_disconnections", Help: "Total inbound disconnections made.", }), - TotalOutboundConnections: prometheus.NewCounter(prometheus.CounterOpts{ + TotalOutboundConnections: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_outbound_connections", Help: "Total outbound connections made.", }), - TotalOutboundConnectionAttempts: prometheus.NewCounter(prometheus.CounterOpts{ + TotalOutboundConnectionAttempts: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_outbound_connection_attempts", Help: "Total outbound connection attempts made.", }), - TotalOutboundConnectionFailedAttempts: prometheus.NewCounter(prometheus.CounterOpts{ + TotalOutboundConnectionFailedAttempts: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_outbound_connection_failed_attempts", Help: "Total outbound connection failed attempts made.", }), - TotalBootNodesConnectionAttempts: prometheus.NewCounter(prometheus.CounterOpts{ + TotalBootNodesConnectionAttempts: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_bootnodes_connection_attempts", Help: "Total boot-nodes connection attempts made.", }), - StartAddAddressBookOverlaysTime: prometheus.NewHistogram(prometheus.HistogramOpts{ + StartAddAddressBookOverlaysTime: m.NewHistogram(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "start_add_addressbook_overlays_time", Help: "The time spent adding overlays peers from addressbook on kademlia start.", }), - PeerLatencyEWMA: prometheus.NewHistogram(prometheus.HistogramOpts{ + PeerLatencyEWMA: m.NewHistogram(m.HistogramOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "peer_latency_ewma", Help: "Peer latency EWMA value distribution.", }), - Blocklist: prometheus.NewCounter(prometheus.CounterOpts{ + Blocklist: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "blocklist", Help: "The number of times peers have been blocklisted.", }), - ReachabilityStatus: prometheus.NewGaugeVec( - prometheus.GaugeOpts{ + ReachabilityStatus: m.NewGaugeVec( + m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "reachability_status", @@ -155,8 +154,8 @@ func newMetrics() metrics { }, []string{"reachability_status"}, ), - PeersReachabilityStatus: prometheus.NewGaugeVec( - prometheus.GaugeOpts{ + PeersReachabilityStatus: m.NewGaugeVec( + m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "peers_reachability_status", @@ -167,7 +166,7 @@ func newMetrics() metrics { } } -// Metrics returns set of prometheus collectors. -func (k *Kad) Metrics() []prometheus.Collector { +// Metrics returns set of m collectors. +func (k *Kad) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(k.metrics) } diff --git a/pkg/topology/lightnode/metrics.go b/pkg/topology/lightnode/metrics.go index cea1504f8a9..ee7a9632db2 100644 --- a/pkg/topology/lightnode/metrics.go +++ b/pkg/topology/lightnode/metrics.go @@ -6,13 +6,12 @@ package lightnode import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) -// metrics groups lightnode related prometheus counters. +// metrics groups lightnode related m counters. type metrics struct { - CurrentlyConnectedPeers prometheus.Gauge - CurrentlyDisconnectedPeers prometheus.Gauge + CurrentlyConnectedPeers m.Gauge + CurrentlyDisconnectedPeers m.Gauge } // newMetrics is a convenient constructor for creating new metrics. @@ -20,13 +19,13 @@ func newMetrics() metrics { const subsystem = "lightnode" return metrics{ - CurrentlyConnectedPeers: prometheus.NewGauge(prometheus.GaugeOpts{ + CurrentlyConnectedPeers: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "currently_connected_peers", Help: "Number of currently connected peers.", }), - CurrentlyDisconnectedPeers: prometheus.NewGauge(prometheus.GaugeOpts{ + CurrentlyDisconnectedPeers: m.NewGauge(m.GaugeOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "currently_disconnected_peers", @@ -34,7 +33,7 @@ func newMetrics() metrics { })} } -// Metrics returns set of prometheus collectors. -func (c *Container) Metrics() []prometheus.Collector { +// Metrics returns set of m collectors. +func (c *Container) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(c.metrics) } diff --git a/pkg/transaction/wrapped/metrics.go b/pkg/transaction/wrapped/metrics.go index a5cfaa554a0..14d0e63824c 100644 --- a/pkg/transaction/wrapped/metrics.go +++ b/pkg/transaction/wrapped/metrics.go @@ -6,117 +6,116 @@ package wrapped import ( m "github.com/ethersphere/bee/v2/pkg/metrics" - "github.com/prometheus/client_golang/prometheus" ) type metrics struct { - TotalRPCCalls prometheus.Counter - TotalRPCErrors prometheus.Counter + TotalRPCCalls m.Counter + TotalRPCErrors m.Counter - TransactionReceiptCalls prometheus.Counter - TransactionCalls prometheus.Counter - BlockNumberCalls prometheus.Counter - BlockHeaderCalls prometheus.Counter - BalanceCalls prometheus.Counter - NonceAtCalls prometheus.Counter - PendingNonceCalls prometheus.Counter - CallContractCalls prometheus.Counter - SuggestGasTipCapCalls prometheus.Counter - EstimateGasCalls prometheus.Counter - SendTransactionCalls prometheus.Counter - FilterLogsCalls prometheus.Counter - ChainIDCalls prometheus.Counter + TransactionReceiptCalls m.Counter + TransactionCalls m.Counter + BlockNumberCalls m.Counter + BlockHeaderCalls m.Counter + BalanceCalls m.Counter + NonceAtCalls m.Counter + PendingNonceCalls m.Counter + CallContractCalls m.Counter + SuggestGasTipCapCalls m.Counter + EstimateGasCalls m.Counter + SendTransactionCalls m.Counter + FilterLogsCalls m.Counter + ChainIDCalls m.Counter } func newMetrics() metrics { subsystem := "eth_backend" return metrics{ - TotalRPCCalls: prometheus.NewCounter(prometheus.CounterOpts{ + TotalRPCCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_rpc_calls", Help: "Count of rpc calls", }), - TotalRPCErrors: prometheus.NewCounter(prometheus.CounterOpts{ + TotalRPCErrors: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "total_rpc_errors", Help: "Count of rpc errors", }), - TransactionCalls: prometheus.NewCounter(prometheus.CounterOpts{ + TransactionCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "calls_transaction", Help: "Count of eth_getTransaction rpc calls", }), - TransactionReceiptCalls: prometheus.NewCounter(prometheus.CounterOpts{ + TransactionReceiptCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "calls_transaction_receipt", Help: "Count of eth_getTransactionReceipt rpc errors", }), - BlockNumberCalls: prometheus.NewCounter(prometheus.CounterOpts{ + BlockNumberCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "calls_block_number", Help: "Count of eth_blockNumber rpc calls", }), - BlockHeaderCalls: prometheus.NewCounter(prometheus.CounterOpts{ + BlockHeaderCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "calls_block_header", Help: "Count of eth_getBlockByNumber (header only) calls", }), - BalanceCalls: prometheus.NewCounter(prometheus.CounterOpts{ + BalanceCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "calls_balance", Help: "Count of eth_getBalance rpc calls", }), - NonceAtCalls: prometheus.NewCounter(prometheus.CounterOpts{ + NonceAtCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "calls_nonce_at", Help: "Count of eth_getTransactionCount (pending false) rpc calls", }), - PendingNonceCalls: prometheus.NewCounter(prometheus.CounterOpts{ + PendingNonceCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "calls_pending_nonce_at", Help: "Count of eth_getTransactionCount (pending true) rpc calls", }), - CallContractCalls: prometheus.NewCounter(prometheus.CounterOpts{ + CallContractCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "calls_eth_call", Help: "Count of eth_call rpc calls", }), - SuggestGasTipCapCalls: prometheus.NewCounter(prometheus.CounterOpts{ + SuggestGasTipCapCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "calls_suggest_gas_tip_cap", Help: "Count of eth_maxPriorityFeePerGas rpc calls", }), - EstimateGasCalls: prometheus.NewCounter(prometheus.CounterOpts{ + EstimateGasCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "calls_estimate_gasprice", Help: "Count of eth_estimateGas rpc calls", }), - SendTransactionCalls: prometheus.NewCounter(prometheus.CounterOpts{ + SendTransactionCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "calls_send_transaction", Help: "Count of eth_sendRawTransaction rpc calls", }), - FilterLogsCalls: prometheus.NewCounter(prometheus.CounterOpts{ + FilterLogsCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "calls_filter_logs", Help: "Count of eth_getLogs rpc calls", }), - ChainIDCalls: prometheus.NewCounter(prometheus.CounterOpts{ + ChainIDCalls: m.NewCounter(m.CounterOpts{ Namespace: m.Namespace, Subsystem: subsystem, Name: "calls_chain_id", @@ -125,6 +124,6 @@ func newMetrics() metrics { } } -func (b *wrappedBackend) Metrics() []prometheus.Collector { +func (b *wrappedBackend) Metrics() []m.Collector { return m.PrometheusCollectorsFromFields(b.metrics) }