From 4b494c3ec323c883e915507b5b45aae0df913c9e Mon Sep 17 00:00:00 2001 From: Travis Date: Mon, 12 Jul 2021 14:28:47 -0500 Subject: [PATCH] Update metric names to use "featurebase" prefix If the `--future.rename` flag is set (to true), this commit will cause metric names to be prefixed with "featurebase" instead of "pilosa". --- prometheus/prometheus.go | 2 +- prometheus/prometheus_test.go | 6 ++++-- server/config.go | 11 +++++++++++ server/server.go | 10 ++++++---- statsd/statsd.go | 21 +++++++++++---------- statsd/statsd_test.go | 4 ++-- 6 files changed, 35 insertions(+), 19 deletions(-) diff --git a/prometheus/prometheus.go b/prometheus/prometheus.go index 3c322376a..05805fac9 100644 --- a/prometheus/prometheus.go +++ b/prometheus/prometheus.go @@ -27,7 +27,7 @@ import ( const ( // namespace is prepended to each metric event name with "_" - defaultNamespace = "pilosa" + defaultNamespace = "general" ) // Ensure client implements interface. diff --git a/prometheus/prometheus_test.go b/prometheus/prometheus_test.go index 6b960e278..25e846727 100644 --- a/prometheus/prometheus_test.go +++ b/prometheus/prometheus_test.go @@ -47,7 +47,9 @@ func TestPrometheusClient_WithTags(t *testing.T) { func TestPrometheusClient_Methods(t *testing.T) { // Create a new client. - c, err := pilosaPrometheus.NewPrometheusClient() + c, err := pilosaPrometheus.NewPrometheusClient( + pilosaPrometheus.OptClientNamespace("testns"), + ) if err != nil { t.Fatal(err) } @@ -64,7 +66,7 @@ func TestPrometheusClient_Methods(t *testing.T) { if err != nil { t.Fatal(err) } - for _, metricName := range []string{"pilosa_ct", "pilosa_cc", "pilosa_gg", "pilosa_hh", "pilosa_tt"} { + for _, metricName := range []string{"testns_ct", "testns_cc", "testns_gg", "testns_hh", "testns_tt"} { if metricExists(metricName, metricFams) { continue } diff --git a/server/config.go b/server/config.go index f278d790e..18acd8968 100644 --- a/server/config.go +++ b/server/config.go @@ -35,6 +35,9 @@ const ( defaultBindPort = "10101" defaultBindGRPCPort = "20101" defaultDiagnosticsInterval = 1 * time.Hour + + namespacePilosa = "pilosa" + namespaceFeaturebase = "featurebase" ) // TLSConfig contains TLS configuration @@ -237,6 +240,14 @@ type Config struct { } `toml:"future"` } +// Namespace returns the namespace to use based on the Future flag. +func (c *Config) Namespace() string { + if c.Future.Rename { + return namespaceFeaturebase + } + return namespacePilosa +} + // MustValidate checks that all ports in a Config are unique and not zero. // We disallow zero because the tests need to be using from the pre-allocated // block of ports maintained by the pilosa/test/port port-mapper. diff --git a/server/server.go b/server/server.go index 11ff5bf1b..b8bcbf313 100644 --- a/server/server.go +++ b/server/server.go @@ -391,7 +391,7 @@ func (m *Command) SetupServer() error { diagnosticsInterval = defaultDiagnosticsInterval } - statsClient, err := newStatsClient(m.Config.Metric.Service, m.Config.Metric.Host) + statsClient, err := newStatsClient(m.Config.Metric.Service, m.Config.Metric.Host, m.Config.Namespace()) if err != nil { return errors.Wrap(err, "new stats client") } @@ -611,14 +611,16 @@ func (m *Command) Close() error { } // newStatsClient creates a stats client from the config -func newStatsClient(name string, host string) (stats.StatsClient, error) { +func newStatsClient(name string, host string, namespace string) (stats.StatsClient, error) { switch name { case "expvar": return stats.NewExpvarStatsClient(), nil case "statsd": - return statsd.NewStatsClient(host) + return statsd.NewStatsClient(host, namespace) case "prometheus": - return prometheus.NewPrometheusClient() + return prometheus.NewPrometheusClient( + prometheus.OptClientNamespace(namespace), + ) case "nop", "none": return stats.NopStatsClient, nil default: diff --git a/statsd/statsd.go b/statsd/statsd.go index f859f3b3f..9366fde77 100644 --- a/statsd/statsd.go +++ b/statsd/statsd.go @@ -27,9 +27,6 @@ import ( // statsD defailt host is "127.0.0.1:8125" const ( - // prefix is appended to each metric event name - prefix = "pilosa." - // bufferLen Stats lient buffer size. bufferLen = 1024 ) @@ -42,10 +39,13 @@ type statsClient struct { client *statsd.Client tags []string logger logger.Logger + + // prefix is appended to each metric event name + prefix string } // NewStatsClient returns a new instance of StatsClient. -func NewStatsClient(host string) (*statsClient, error) { +func NewStatsClient(host string, namespace string) (*statsClient, error) { c, err := statsd.NewBuffered(host, bufferLen) if err != nil { return nil, err @@ -54,6 +54,7 @@ func NewStatsClient(host string) (*statsClient, error) { return &statsClient{ client: c, logger: logger.NopLogger, + prefix: namespace + ".", }, nil } @@ -81,7 +82,7 @@ func (c *statsClient) WithTags(tags ...string) stats.StatsClient { // Count tracks the number of times something occurs per second. func (c *statsClient) Count(name string, value int64, rate float64) { - if err := c.client.Count(prefix+name, value, c.tags, rate); err != nil { + if err := c.client.Count(c.prefix+name, value, c.tags, rate); err != nil { c.logger.Errorf("statsd.StatsClient.Count error: %s", err) } } @@ -89,35 +90,35 @@ func (c *statsClient) Count(name string, value int64, rate float64) { // CountWithCustomTags tracks the number of times something occurs per second with custom tags. func (c *statsClient) CountWithCustomTags(name string, value int64, rate float64, t []string) { tags := append(c.tags, t...) - if err := c.client.Count(prefix+name, value, tags, rate); err != nil { + if err := c.client.Count(c.prefix+name, value, tags, rate); err != nil { c.logger.Errorf("statsd.StatsClient.Count error: %s", err) } } // Gauge sets the value of a metric. func (c *statsClient) Gauge(name string, value float64, rate float64) { - if err := c.client.Gauge(prefix+name, value, c.tags, rate); err != nil { + if err := c.client.Gauge(c.prefix+name, value, c.tags, rate); err != nil { c.logger.Errorf("statsd.StatsClient.Gauge error: %s", err) } } // Histogram tracks statistical distribution of a metric. func (c *statsClient) Histogram(name string, value float64, rate float64) { - if err := c.client.Histogram(prefix+name, value, c.tags, rate); err != nil { + if err := c.client.Histogram(c.prefix+name, value, c.tags, rate); err != nil { c.logger.Errorf("statsd.StatsClient.Histogram error: %s", err) } } // Set tracks number of unique elements. func (c *statsClient) Set(name string, value string, rate float64) { - if err := c.client.Set(prefix+name, value, c.tags, rate); err != nil { + if err := c.client.Set(c.prefix+name, value, c.tags, rate); err != nil { c.logger.Errorf("statsd.StatsClient.Set error: %s", err) } } // Timing tracks timing information for a metric. func (c *statsClient) Timing(name string, value time.Duration, rate float64) { - if err := c.client.Timing(prefix+name, value, c.tags, rate); err != nil { + if err := c.client.Timing(c.prefix+name, value, c.tags, rate); err != nil { c.logger.Errorf("statsd.StatsClient.Timing error: %s", err) } } diff --git a/statsd/statsd_test.go b/statsd/statsd_test.go index e060b2b18..14835300d 100644 --- a/statsd/statsd_test.go +++ b/statsd/statsd_test.go @@ -25,7 +25,7 @@ import ( func TestStatsClient_WithTags(t *testing.T) { // Create a new client. - c, err := statsd.NewStatsClient("localhost:19444") + c, err := statsd.NewStatsClient("localhost:19444", "testnamespace") if err != nil { t.Fatal(err) } @@ -46,7 +46,7 @@ func TestStatsClient_WithTags(t *testing.T) { func TestStatsClient_Methods(t *testing.T) { // Create a new client. - c, err := statsd.NewStatsClient("localhost:19444") + c, err := statsd.NewStatsClient("localhost:19444", "testnamespace") if err != nil { t.Fatal(err) }