Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions internal/apiserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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+):`)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
38 changes: 36 additions & 2 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2021 Kaleido, Inc.
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion test/e2e/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down