From 6e2b91f9abaea16551e21d5c11ab639baee2d3f5 Mon Sep 17 00:00:00 2001 From: Mark Gritter Date: Tue, 16 Jun 2020 10:50:24 -0500 Subject: [PATCH] Configure metrics wrapper with the "global" object, not just the fanout. (#9099) --- helper/metricsutil/wrapped_metrics.go | 6 ++++-- helper/metricsutil/wrapped_metrics_test.go | 14 +++++++++++++- internalshared/configutil/telemetry.go | 4 ++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/helper/metricsutil/wrapped_metrics.go b/helper/metricsutil/wrapped_metrics.go index 6fb4aa65cc26..54110abf321e 100644 --- a/helper/metricsutil/wrapped_metrics.go +++ b/helper/metricsutil/wrapped_metrics.go @@ -22,7 +22,7 @@ type ClusterMetricSink struct { MaxGaugeCardinality int GaugeInterval time.Duration - // Sink is the go-metrics sink to send to + // Sink is the go-metrics instance to send to. Sink metrics.MetricSink } @@ -57,9 +57,11 @@ func (m *ClusterMetricSink) MeasureSinceWithLabels(key []string, start time.Time // BlackholeSink is a default suitable for use in unit tests. func BlackholeSink() *ClusterMetricSink { + sink, _ := metrics.New(metrics.DefaultConfig(""), + &metrics.BlackholeSink{}) return &ClusterMetricSink{ ClusterName: "", - Sink: &metrics.BlackholeSink{}, + Sink: sink, } } diff --git a/helper/metricsutil/wrapped_metrics_test.go b/helper/metricsutil/wrapped_metrics_test.go index 975159984597..d1ad82a47a84 100644 --- a/helper/metricsutil/wrapped_metrics_test.go +++ b/helper/metricsutil/wrapped_metrics_test.go @@ -16,6 +16,18 @@ func isLabelPresent(toFind Label, ls []Label) bool { return false } +// We can use a sink directly, or wrap the top-level +// go-metrics implementation for testing purposes. +func defaultMetrics(sink metrics.MetricSink) *metrics.Metrics { + // No service name + config := metrics.DefaultConfig("") + + // No host name + config.EnableHostname = false + m, _ := metrics.New(config, sink) + return m +} + func TestClusterLabelPresent(t *testing.T) { testClusterName := "test-cluster" @@ -28,7 +40,7 @@ func TestClusterLabelPresent(t *testing.T) { 2000000*time.Hour) clusterSink := &ClusterMetricSink{ ClusterName: testClusterName, - Sink: inmemSink, + Sink: defaultMetrics(inmemSink), } key1 := []string{"aaa", "bbb"} diff --git a/internalshared/configutil/telemetry.go b/internalshared/configutil/telemetry.go index 0abfffad898d..8bb833e55519 100644 --- a/internalshared/configutil/telemetry.go +++ b/internalshared/configutil/telemetry.go @@ -320,7 +320,7 @@ func SetupTelemetry(opts *SetupTelemetryOpts) (*metrics.InmemSink, *metricsutil. metricsConf.EnableHostname = false } fanout = append(fanout, inm) - _, err := metrics.NewGlobal(metricsConf, fanout) + globalMetrics, err := metrics.NewGlobal(metricsConf, fanout) if err != nil { return nil, nil, false, err @@ -332,7 +332,7 @@ func SetupTelemetry(opts *SetupTelemetryOpts) (*metrics.InmemSink, *metricsutil. ClusterName: opts.ClusterName, MaxGaugeCardinality: 500, GaugeInterval: 10 * time.Minute, - Sink: fanout, + Sink: globalMetrics, } return inm, wrapper, prometheusEnabled, nil