diff --git a/agent/config/runtime_test.go b/agent/config/runtime_test.go index e761ec95a1..7c8a35a2e6 100644 --- a/agent/config/runtime_test.go +++ b/agent/config/runtime_test.go @@ -8,7 +8,6 @@ import ( "errors" "flag" "fmt" - "github.com/armon/go-metrics/prometheus" "io/ioutil" "net" "os" @@ -19,6 +18,8 @@ import ( "testing" "time" + "github.com/armon/go-metrics/prometheus" + "github.com/stretchr/testify/require" "github.com/hashicorp/consul/agent/cache" @@ -7817,9 +7818,15 @@ func TestSanitize(t *testing.T) { "DogstatsdTags": [], "FilterDefault": false, "MetricsPrefix": "", - "PrometheusRetentionTime": "0s", "StatsdAddr": "", - "StatsiteAddr": "" + "StatsiteAddr": "", + "PrometheusOpts": { + "Expiration": "0s", + "Registerer": null, + "GaugeDefinitions": [], + "CounterDefinitions": [], + "SummaryDefinitions": [] + } }, "TranslateWANAddrs": false, "TxnMaxReqLen": 5678000000000000, diff --git a/lib/telemetry.go b/lib/telemetry.go index 5c98bda791..ceef4f9736 100644 --- a/lib/telemetry.go +++ b/lib/telemetry.go @@ -220,6 +220,10 @@ func (c *TelemetryConfig) MergeDefaults(defaults *TelemetryConfig) { // implementing this for the types we actually have for now. Test failure // should catch the case where we add new types later. switch f.Kind() { + case reflect.Struct: + if f.Type() == reflect.TypeOf(prometheus.PrometheusOpts{}) { + continue + } case reflect.Slice: if !f.IsNil() { continue @@ -281,13 +285,7 @@ func prometheusSink(cfg TelemetryConfig, hostname string) (metrics.MetricSink, e return nil, nil } - prometheusOpts := prometheus.PrometheusOpts{ - Expiration: cfg.PrometheusOpts.Expiration, - GaugeDefinitions: cfg.PrometheusOpts.GaugeDefinitions, - CounterDefinitions: cfg.PrometheusOpts.CounterDefinitions, - SummaryDefinitions: cfg.PrometheusOpts.SummaryDefinitions, - } - sink, err := prometheus.NewPrometheusSinkFrom(prometheusOpts) + sink, err := prometheus.NewPrometheusSinkFrom(cfg.PrometheusOpts) if err != nil { return nil, err } diff --git a/lib/telemetry_test.go b/lib/telemetry_test.go index f81b7b5c1a..4ee012f1ec 100644 --- a/lib/telemetry_test.go +++ b/lib/telemetry_test.go @@ -5,11 +5,14 @@ import ( "testing" "time" + "github.com/armon/go-metrics/prometheus" + "github.com/stretchr/testify/require" ) func makeFullTelemetryConfig(t *testing.T) TelemetryConfig { var ( + promOpts = prometheus.PrometheusOpts{} strSliceVal = []string{"foo"} strVal = "foo" intVal = int64(1 * time.Second) @@ -27,6 +30,12 @@ func makeFullTelemetryConfig(t *testing.T) TelemetryConfig { // now for brevity but will fail the test if a new field type is added since // this is likely not implemented in MergeDefaults either. switch f.Kind() { + case reflect.Struct: + if f.Type() != reflect.TypeOf(promOpts) { + t.Fatalf("unknown struct type in TelemetryConfig: actual %v, expected: %v", f.Type(), reflect.TypeOf(promOpts)) + } + // TODO(kit): This should delve into the fields and set them individually rather than using an empty struct + f.Set(reflect.ValueOf(promOpts)) case reflect.Slice: if f.Type() != reflect.TypeOf(strSliceVal) { t.Fatalf("unknown slice type in TelemetryConfig." +