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.
This commit is contained in:
Oliver Gugger
2022-03-25 14:22:31 +01:00
parent 842669dd3a
commit d86e49706f

View File

@@ -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