diff --git a/internal/apiserver/server.go b/internal/apiserver/server.go index 04e0cae791..6129d6f8da 100644 --- a/internal/apiserver/server.go +++ b/internal/apiserver/server.go @@ -44,8 +44,6 @@ import ( "github.com/hyperledger/firefly/internal/orchestrator" "github.com/hyperledger/firefly/pkg/database" "github.com/hyperledger/firefly/pkg/fftypes" - "github.com/prometheus/client_golang/prometheus" - muxprom "gitlab.com/hfuss/mux-prometheus/pkg/middleware" ) var ffcodeExtractor = regexp.MustCompile(`^(FF\d+):`) @@ -436,23 +434,12 @@ func (as *apiServer) swaggerHandler(routes []*oapispec.Route, url string) func(r } } -func (as *apiServer) configurePrometheusInstrumentation(namespace, subsystem string, r *mux.Router) { - if as.metricsEnabled { - instrumentation := muxprom.NewCustomInstrumentation( - true, - namespace, - subsystem, - prometheus.DefBuckets, - map[string]string{}, - metrics.Registry(), - ) - r.Use(instrumentation.Middleware) - } -} - func (as *apiServer) createMuxRouter(ctx context.Context, o orchestrator.Orchestrator) *mux.Router { r := mux.NewRouter() - as.configurePrometheusInstrumentation("ff_apiserver", "rest", r) + + if as.metricsEnabled { + r.Use(metrics.GetRestServerInstrumentation().Middleware) + } for _, route := range routes { if route.JSONHandler != nil { @@ -479,7 +466,9 @@ func (as *apiServer) createMuxRouter(ctx context.Context, o orchestrator.Orchest func (as *apiServer) createAdminMuxRouter(o orchestrator.Orchestrator) *mux.Router { r := mux.NewRouter() - as.configurePrometheusInstrumentation("ff_apiserver", "admin", r) + if as.metricsEnabled { + r.Use(metrics.GetAdminServerInstrumentation().Middleware) + } for _, route := range adminRoutes { if route.JSONHandler != nil { diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index b721fca558..4a56f11b0b 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -1,4 +1,4 @@ -// Copyright © 2021 Kaleido, Inc. +// Copyright © 2022 Kaleido, Inc. // // SPDX-License-Identifier: Apache-2.0 // @@ -19,9 +19,12 @@ package metrics import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" + muxprom "gitlab.com/hfuss/mux-prometheus/pkg/middleware" ) var registry *prometheus.Registry +var adminInstrumentation *muxprom.Instrumentation +var restInstrumentation *muxprom.Instrumentation var BatchPinCounter prometheus.Counter // MetricsBatchPin is the prometheus metric for total number of batch pins submitted @@ -38,6 +41,35 @@ func Registry() *prometheus.Registry { return registry } +// GetAdminServerInstrumentation returns the admin server's Prometheus middleware, ensuring its metrics are never +// registered twice +func GetAdminServerInstrumentation() *muxprom.Instrumentation { + if adminInstrumentation == nil { + adminInstrumentation = newInstrumentation("admin") + } + return adminInstrumentation +} + +// GetRestServerInstrumentation returns the REST server's Prometheus middleware, ensuring its metrics are never +// registered twice +func GetRestServerInstrumentation() *muxprom.Instrumentation { + if restInstrumentation == nil { + restInstrumentation = newInstrumentation("rest") + } + return restInstrumentation +} + +func newInstrumentation(subsystem string) *muxprom.Instrumentation { + return muxprom.NewCustomInstrumentation( + true, + "ff_apiserver", + subsystem, + prometheus.DefBuckets, + map[string]string{}, + Registry(), + ) +} + func initMetricsCollectors() { BatchPinCounter = prometheus.NewCounter(prometheus.CounterOpts{ Name: MetricsBatchPin, @@ -51,7 +83,9 @@ func registerMetricsCollectors() { registry.MustRegister(BatchPinCounter) } -// Clear will reset the Prometheus metrics registry, useful for testing +// Clear will reset the Prometheus metrics registry and instrumentations, useful for testing func Clear() { registry = nil + adminInstrumentation = nil + restInstrumentation = nil } diff --git a/test/e2e/run.sh b/test/e2e/run.sh index b3fc9f0ab8..a228f70e9b 100755 --- a/test/e2e/run.sh +++ b/test/e2e/run.sh @@ -67,7 +67,7 @@ if [ "$BUILD_FIREFLY" == "true" ]; then fi if [ "$DOWNLOAD_CLI" == "true" ]; then - go install github.com/hyperledger/firefly-cli/ff@v0.0.40 + go install github.com/hyperledger/firefly-cli/ff@v0.0.41 checkOk $? fi