From d86e49706fe17fd684ebc4e72350ff82235de225 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Fri, 25 Mar 2022 14:22:31 +0100 Subject: [PATCH] aperture: only register prometheus metrics when enabled To avoid running into an issue in the race unit test with the Prometheus histogram metrics that aren't concurrency safe, we don't register any of them if Prometheus isn't enabled in the first place. This shouldn't be an issue in production, since we don't start multiple instances of Aperture _within the same process_ at the same time. --- aperture.go | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/aperture.go b/aperture.go index e06641b..e885936 100644 --- a/aperture.go +++ b/aperture.go @@ -680,22 +680,27 @@ func createProxy(cfg *Config, challenger *LndChallenger, func createHashMailServer(cfg *Config) ([]proxy.LocalService, func(), error) { var localServices []proxy.LocalService - // Before we register both servers, we'll also ensure that the - // collector will export latency metrics for the histogram. - grpc_prometheus.EnableHandlingTimeHistogram() - serverOpts := []grpc.ServerOption{ - grpc.ChainUnaryInterceptor( - grpc_prometheus.UnaryServerInterceptor, - ), - grpc.ChainStreamInterceptor( - grpc_prometheus.StreamServerInterceptor, - ), grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{ MinTime: time.Minute, }), } + // Before we register both servers, we'll also ensure that the collector + // will export latency metrics for the histogram. + if cfg.Prometheus != nil && cfg.Prometheus.Enabled { + grpc_prometheus.EnableHandlingTimeHistogram() + serverOpts = append( + serverOpts, + grpc.ChainUnaryInterceptor( + grpc_prometheus.UnaryServerInterceptor, + ), + grpc.ChainStreamInterceptor( + grpc_prometheus.StreamServerInterceptor, + ), + ) + } + // Create a gRPC server for the hashmail server. hashMailServer := newHashMailServer(hashMailServerConfig{ msgRate: cfg.HashMail.MessageRate, @@ -710,7 +715,9 @@ func createHashMailServer(cfg *Config) ([]proxy.LocalService, func(), error) { ) // Export the gRPC information for the public gRPC server. - grpc_prometheus.Register(hashMailGRPC) + if cfg.Prometheus != nil && cfg.Prometheus.Enabled { + grpc_prometheus.Register(hashMailGRPC) + } // And a REST proxy for it as well. // The default JSON marshaler of the REST proxy only sets OrigName to