diff --git a/.VERSION b/.VERSION index f8cc577..6d21a1c 100644 --- a/.VERSION +++ b/.VERSION @@ -1 +1 @@ -v0.7 \ No newline at end of file +v0.9 \ No newline at end of file diff --git a/metrics/metrics.go b/metrics/metrics.go index b9a331b..08e494e 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -11,8 +11,7 @@ import ( var logger = xlog.NewPackageLogger("github.com/go-phorce/dolly", "metrics") -// SetGauge should retain the last value it is set to -func (m *Metrics) SetGauge(key []string, val float32, tags ...Tag) { +func (m *Metrics) prepare(typ string, key []string, tags ...Tag) (bool, []string, []Tag) { if len(m.GlobalTags) > 0 { tags = append(tags, m.GlobalTags...) } @@ -24,7 +23,7 @@ func (m *Metrics) SetGauge(key []string, val float32, tags ...Tag) { } } if m.EnableTypePrefix { - key = insert(0, "gauge", key) + key = insert(0, typ, key) } if m.ServiceName != "" { if m.EnableServiceLabel { @@ -33,89 +32,50 @@ func (m *Metrics) SetGauge(key []string, val float32, tags ...Tag) { key = insert(0, m.ServiceName, key) } } + if m.GlobalPrefix != "" { + key = insert(0, m.GlobalPrefix, key) + } allowed, labelsFiltered := m.allowMetric(key, tags) + return allowed, key, labelsFiltered +} + +// SetGauge should retain the last value it is set to +func (m *Metrics) SetGauge(key []string, val float32, tags ...Tag) { + allowed, keys, labels := m.prepare("gauge", key, tags...) if !allowed { return } - m.sink.SetGauge(key, val, labelsFiltered) + m.sink.SetGauge(keys, val, labels) } // IncrCounter should accumulate values func (m *Metrics) IncrCounter(key []string, val float32, tags ...Tag) { - if len(m.GlobalTags) > 0 { - tags = append(tags, m.GlobalTags...) - } - if m.HostName != "" && m.EnableHostnameLabel { - tags = append(tags, Tag{"host", m.HostName}) - } - if m.EnableTypePrefix { - key = insert(0, "counter", key) - } - if m.ServiceName != "" { - if m.EnableServiceLabel { - tags = append(tags, Tag{"service", m.ServiceName}) - } else { - key = insert(0, m.ServiceName, key) - } - } - allowed, labelsFiltered := m.allowMetric(key, tags) + allowed, keys, labels := m.prepare("counter", key, tags...) if !allowed { return } - m.sink.IncrCounter(key, val, labelsFiltered) + m.sink.IncrCounter(keys, val, labels) } // AddSample is for timing information, where quantiles are used func (m *Metrics) AddSample(key []string, val float32, tags ...Tag) { - if len(m.GlobalTags) > 0 { - tags = append(tags, m.GlobalTags...) - } - if m.HostName != "" && m.EnableHostnameLabel { - tags = append(tags, Tag{"host", m.HostName}) - } - if m.EnableTypePrefix { - key = insert(0, "sample", key) - } - if m.ServiceName != "" { - if m.EnableServiceLabel { - tags = append(tags, Tag{"service", m.ServiceName}) - } else { - key = insert(0, m.ServiceName, key) - } - } - allowed, labelsFiltered := m.allowMetric(key, tags) + allowed, keys, labels := m.prepare("sample", key, tags...) if !allowed { return } - m.sink.AddSample(key, val, labelsFiltered) + m.sink.AddSample(keys, val, labels) } // MeasureSince is for timing information func (m *Metrics) MeasureSince(key []string, start time.Time, tags ...Tag) { - if len(m.GlobalTags) > 0 { - tags = append(tags, m.GlobalTags...) - } - if m.HostName != "" && m.EnableHostnameLabel { - tags = append(tags, Tag{"host", m.HostName}) - } - if m.EnableTypePrefix { - key = insert(0, "timer", key) - } - if m.ServiceName != "" { - if m.EnableServiceLabel { - tags = append(tags, Tag{"service", m.ServiceName}) - } else { - key = insert(0, m.ServiceName, key) - } - } - allowed, labelsFiltered := m.allowMetric(key, tags) + elapsed := time.Now().Sub(start) + msec := float32(elapsed.Nanoseconds()) / float32(m.TimerGranularity) + + allowed, keys, labels := m.prepare("timer", key, tags...) if !allowed { return } - now := time.Now() - elapsed := now.Sub(start) - msec := float32(elapsed.Nanoseconds()) / float32(m.TimerGranularity) - m.sink.AddSample(key, msec, labelsFiltered) + m.sink.AddSample(keys, msec, labels) } // UpdateFilter overwrites the existing filter with the given rules. diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go index fa38336..963a72d 100644 --- a/metrics/metrics_test.go +++ b/metrics/metrics_test.go @@ -89,6 +89,7 @@ func Test_SetProviderPrometheus(t *testing.T) { GlobalTags: []metrics.Tag{ {"env_tag", "test_value"}, }, + GlobalPrefix: "dolly", }, d) require.NoError(t, err) run(prov, 10) @@ -100,8 +101,8 @@ func Test_SetProviderPrometheus(t *testing.T) { promhttp.Handler().ServeHTTP(w, r) require.Equal(t, http.StatusOK, w.Code) - body := string(w.Body.Bytes()) - assert.Contains(t, body, "test_metrics_since_count") + body := w.Body.String() + assert.Contains(t, body, "dolly_test_metrics_since_count") assert.Contains(t, body, `env_tag="test_value"`) } diff --git a/metrics/start.go b/metrics/start.go index d87a5f1..5408e0f 100644 --- a/metrics/start.go +++ b/metrics/start.go @@ -21,6 +21,7 @@ type Config struct { TimerGranularity time.Duration // Granularity of timers. ProfileInterval time.Duration // Interval to profile runtime metrics GlobalTags []Tag // Tags to add to every metric + GlobalPrefix string // Prefix to add to every metric AllowedPrefixes []string // A list of metric prefixes to allow, with '.' as the separator BlockedPrefixes []string // A list of metric prefixes to block, with '.' as the separator @@ -54,7 +55,7 @@ func DefaultConfig(serviceName string) *Config { c := &Config{ ServiceName: serviceName, // Use client provided service HostName: "", - EnableHostname: true, // Enable hostname prefix + EnableHostname: false, // Enable hostname prefix EnableRuntimeMetrics: true, // Enable runtime profiling EnableTypePrefix: false, // Disable type prefix TimerGranularity: time.Millisecond, // Timers are in milliseconds diff --git a/metrics/util/certexpiration_test.go b/metrics/util/certexpiration_test.go index d6ed962..12d6085 100644 --- a/metrics/util/certexpiration_test.go +++ b/metrics/util/certexpiration_test.go @@ -16,7 +16,9 @@ import ( func Test_PublishCertExpiration(t *testing.T) { im := metrics.NewInmemSink(time.Minute, time.Minute*5) - _, err := metrics.NewGlobal(metrics.DefaultConfig("service"), im) + cfg := metrics.DefaultConfig("service") + cfg.EnableHostname = true + _, err := metrics.NewGlobal(cfg, im) require.NoError(t, err) crt, _, err := testify.MakeSelfCertRSA(24) diff --git a/metrics/util/uptime_test.go b/metrics/util/uptime_test.go index e501876..35fa997 100644 --- a/metrics/util/uptime_test.go +++ b/metrics/util/uptime_test.go @@ -2,7 +2,6 @@ package util import ( "fmt" - "os" "testing" "time" @@ -40,7 +39,8 @@ func Test_PublishUptime(t *testing.T) { require.True(t, exists, "Expected metric with key %s to exist, but it doesn't", key) assert.Equal(t, expectedCount, s.Count, "Unexpected count for metric %s", key) } - hostname, _ := os.Hostname() - assertGauge(fmt.Sprintf("svc1.%s.uptime.seconds;service=svc1", hostname)) + //hostname, _ := os.Hostname() + //assertGauge(fmt.Sprintf("svc1.%s.uptime.seconds;service=svc1", hostname)) + assertGauge(fmt.Sprintf("svc1.uptime.seconds;service=svc1")) assertCounter(fmt.Sprintf("svc1.heartbeat;service=svc1"), 1) }