From e248942fb154d927ea83b1c31d725978271330a6 Mon Sep 17 00:00:00 2001 From: stack72 Date: Tue, 20 Nov 2018 17:24:40 +0200 Subject: [PATCH] metrics/circonus: Adding support for sending to a local circonus agent There are times that we want to send metrics to a local circonus agent rather than across the wire to the Circonus API. This is now supported You either need to supply Circonus APIKey or Circonus SubmissionURL and the circonus go-metrics library will choose as expected This follows the same configuration options as that of Vault, Nomad & Consul --- config/config.go | 11 ++++++----- config/load.go | 1 + config/load_test.go | 7 +++++++ metrics/circonus.go | 5 +++-- metrics/circonus_test.go | 15 ++++++++------- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/config/config.go b/config/config.go index 3dcc30067..22ff1e153 100644 --- a/config/config.go +++ b/config/config.go @@ -83,11 +83,12 @@ type Runtime struct { } type Circonus struct { - APIKey string - APIApp string - APIURL string - CheckID string - BrokerID string + APIKey string + APIApp string + APIURL string + CheckID string + BrokerID string + SubmissionURL string } type Log struct { diff --git a/config/load.go b/config/load.go index c5a022c4b..60d039acf 100644 --- a/config/load.go +++ b/config/load.go @@ -157,6 +157,7 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c f.StringVar(&cfg.Metrics.Circonus.APIURL, "metrics.circonus.apiurl", defaultConfig.Metrics.Circonus.APIURL, "Circonus API URL") f.StringVar(&cfg.Metrics.Circonus.BrokerID, "metrics.circonus.brokerid", defaultConfig.Metrics.Circonus.BrokerID, "Circonus Broker ID") f.StringVar(&cfg.Metrics.Circonus.CheckID, "metrics.circonus.checkid", defaultConfig.Metrics.Circonus.CheckID, "Circonus Check ID") + f.StringVar(&cfg.Metrics.Circonus.SubmissionURL, "metrics.circonus.submissionurl", defaultConfig.Metrics.Circonus.SubmissionURL, "Circonus Check SubmissionURL") f.StringVar(&cfg.Registry.Backend, "registry.backend", defaultConfig.Registry.Backend, "registry backend") f.DurationVar(&cfg.Registry.Timeout, "registry.timeout", defaultConfig.Registry.Timeout, "timeout for registry to become available") f.DurationVar(&cfg.Registry.Retry, "registry.retry", defaultConfig.Registry.Retry, "retry interval during startup") diff --git a/config/load_test.go b/config/load_test.go index c8f938359..b78cf7163 100644 --- a/config/load_test.go +++ b/config/load_test.go @@ -744,6 +744,13 @@ func TestLoad(t *testing.T) { return cfg }, }, + { + args: []string{"-metrics.circonus.submissionurl", "value"}, + cfg: func(cfg *Config) *Config { + cfg.Metrics.Circonus.SubmissionURL = "value" + return cfg + }, + }, { args: []string{"-runtime.gogc", "555"}, cfg: func(cfg *Config) *Config { diff --git a/metrics/circonus.go b/metrics/circonus.go index c4bfe8f69..0caf29e22 100644 --- a/metrics/circonus.go +++ b/metrics/circonus.go @@ -24,8 +24,8 @@ func circonusRegistry(prefix string, circ config.Circonus, interval time.Duratio var initError error once.Do(func() { - if circ.APIKey == "" { - initError = errors.New("metrics: Circonus API token key") + if circ.APIKey == "" && circ.SubmissionURL == "" { + initError = errors.New("metrics: Circonus API token key or SubmissionURL") return } @@ -41,6 +41,7 @@ func circonusRegistry(prefix string, circ config.Circonus, interval time.Duratio cfg := &cgm.Config{} + cfg.CheckManager.Check.SubmissionURL = circ.SubmissionURL cfg.CheckManager.API.TokenKey = circ.APIKey cfg.CheckManager.API.TokenApp = circ.APIApp cfg.CheckManager.API.URL = circ.APIURL diff --git a/metrics/circonus_test.go b/metrics/circonus_test.go index 808b3e183..7075f421e 100644 --- a/metrics/circonus_test.go +++ b/metrics/circonus_test.go @@ -53,18 +53,19 @@ func TestTimer(t *testing.T) { func TestAll(t *testing.T) { start := time.Now() - if os.Getenv("CIRCONUS_API_TOKEN") == "" { - t.Skip("skipping test; $CIRCONUS_API_TOKEN not set") + if os.Getenv("CIRCONUS_API_TOKEN") == "" && os.Getenv("CIRCONUS_SUBMISSION_URL") == "" { + t.Skip("skipping test; $CIRCONUS_API_TOKEN or $CIRCONUS_SUBMISSION_URL not set") } t.Log("Testing cgm functionality -- this *will* create/use a check") cfg := config.Circonus{ - APIKey: os.Getenv("CIRCONUS_API_TOKEN"), - APIApp: os.Getenv("CIRCONUS_API_APP"), - APIURL: os.Getenv("CIRCONUS_API_URL"), - CheckID: os.Getenv("CIRCONUS_CHECK_ID"), - BrokerID: os.Getenv("CIRCONUS_BROKER_ID"), + SubmissionURL: os.Getenv("CIRCONUS_SUBMISSION_URL"), + APIKey: os.Getenv("CIRCONUS_API_TOKEN"), + APIApp: os.Getenv("CIRCONUS_API_APP"), + APIURL: os.Getenv("CIRCONUS_API_URL"), + CheckID: os.Getenv("CIRCONUS_CHECK_ID"), + BrokerID: os.Getenv("CIRCONUS_BROKER_ID"), } interval, err := time.ParseDuration("60s")