Skip to content

Commit

Permalink
metrics: added global prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
dissoupov committed Jan 8, 2022
1 parent 9bb16c6 commit 0fdcf53
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.7
v0.9
82 changes: 21 additions & 61 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand All @@ -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 {
Expand All @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"`)
}

Expand Down
3 changes: 2 additions & 1 deletion metrics/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion metrics/util/certexpiration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions metrics/util/uptime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package util

import (
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -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)
}

0 comments on commit 0fdcf53

Please sign in to comment.