Skip to content

Commit

Permalink
Augment keys of all metric types in the same way
Browse files Browse the repository at this point in the history
It is unexpected that the common key prefix for some metrics types is
different. This makes it unnecessary difficult to find the metrics in
the database. This commit unifies the prefix for all metrics.

The prefix of each key is now `service name, type prefix, hostname`. All
of them are optional. `service name` is added if it is available and not
set as a label because the configuration flag `EnableServiceLabel` is
set. `type prefix` is added if it is enabled with the configuration flag
`EnableTypePrefix`. `hostname` is added if it is available and not set
as a label because `EnableHostnameLabel` is set.

The behaviour of `EmitKeys`, `IncrCounter`, `IncrCounterWithLabels`,
`AddSample`, `AddSampleWithLabels`, `MeasureSince` and
`MeasureSinceWithLabels` changes with this commit. For `EmitKey` the
hostname will be added (details see above) but the service name will no
longer be added when `EnableServiceLabel` is set to `true`. For the
other types the hostname will be added if it is available and
`EnableHostnameLabel` is set to `false`.
  • Loading branch information
stefanbirkner committed Aug 30, 2020
1 parent 97b2452 commit 3633199
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 31 deletions.
46 changes: 18 additions & 28 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,15 @@ func (m *Metrics) SetGauge(key []string, val float32) {
}

func (m *Metrics) SetGaugeWithLabels(key []string, val float32, labels []Label) {
key = m.augmentKey(key, "gauge")
if m.HostName != "" {
if m.EnableHostnameLabel {
labels = append(labels, Label{"host", m.HostName})
} else if m.EnableHostname {
key = insert(0, m.HostName, key)
}
}
if m.EnableTypePrefix {
key = insert(0, "gauge", key)
}
if m.ServiceName != "" {
if m.EnableServiceLabel {
labels = append(labels, Label{"service", m.ServiceName})
} else {
key = insert(0, m.ServiceName, key)
}
}
allowed, labelsFiltered := m.allowMetric(key, labels)
Expand All @@ -43,12 +37,7 @@ func (m *Metrics) SetGaugeWithLabels(key []string, val float32, labels []Label)
}

func (m *Metrics) EmitKey(key []string, val float32) {
if m.EnableTypePrefix {
key = insert(0, "kv", key)
}
if m.ServiceName != "" {
key = insert(0, m.ServiceName, key)
}
key = m.augmentKey(key, "kv")
allowed, _ := m.allowMetric(key, nil)
if !allowed {
return
Expand All @@ -61,17 +50,13 @@ func (m *Metrics) IncrCounter(key []string, val float32) {
}

func (m *Metrics) IncrCounterWithLabels(key []string, val float32, labels []Label) {
key = m.augmentKey(key, "counter")
if m.HostName != "" && m.EnableHostnameLabel {
labels = append(labels, Label{"host", m.HostName})
}
if m.EnableTypePrefix {
key = insert(0, "counter", key)
}
if m.ServiceName != "" {
if m.EnableServiceLabel {
labels = append(labels, Label{"service", m.ServiceName})
} else {
key = insert(0, m.ServiceName, key)
}
}
allowed, labelsFiltered := m.allowMetric(key, labels)
Expand All @@ -86,17 +71,13 @@ func (m *Metrics) AddSample(key []string, val float32) {
}

func (m *Metrics) AddSampleWithLabels(key []string, val float32, labels []Label) {
key = m.augmentKey(key, "sample")
if m.HostName != "" && m.EnableHostnameLabel {
labels = append(labels, Label{"host", m.HostName})
}
if m.EnableTypePrefix {
key = insert(0, "sample", key)
}
if m.ServiceName != "" {
if m.EnableServiceLabel {
labels = append(labels, Label{"service", m.ServiceName})
} else {
key = insert(0, m.ServiceName, key)
}
}
allowed, labelsFiltered := m.allowMetric(key, labels)
Expand All @@ -111,17 +92,13 @@ func (m *Metrics) MeasureSince(key []string, start time.Time) {
}

func (m *Metrics) MeasureSinceWithLabels(key []string, start time.Time, labels []Label) {
key = m.augmentKey(key, "timer")
if m.HostName != "" && m.EnableHostnameLabel {
labels = append(labels, Label{"host", m.HostName})
}
if m.EnableTypePrefix {
key = insert(0, "timer", key)
}
if m.ServiceName != "" {
if m.EnableServiceLabel {
labels = append(labels, Label{"service", m.ServiceName})
} else {
key = insert(0, m.ServiceName, key)
}
}
allowed, labelsFiltered := m.allowMetric(key, labels)
Expand Down Expand Up @@ -172,6 +149,19 @@ func (m *Metrics) UpdateFilterAndLabels(allow, block, allowedLabels, blockedLabe
}
}

func (m *Metrics) augmentKey(key []string, prefix string) []string {
if m.HostName != "" && m.EnableHostname && !m.EnableHostnameLabel {
key = insert(0, m.HostName, key)
}
if m.EnableTypePrefix {
key = insert(0, prefix, key)
}
if m.ServiceName != "" && !m.EnableServiceLabel {
key = insert(0, m.ServiceName, key)
}
return key
}

// labelIsAllowed return true if a should be included in metric
// the caller should lock m.filterLock while calling this method
func (m *Metrics) labelIsAllowed(label *Label) bool {
Expand Down
45 changes: 45 additions & 0 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ func TestMetrics_EmitKey(t *testing.T) {
t.Fatalf("")
}

m, met = mockMetric()
met.HostName = "test"
met.EnableHostname = true
met.EmitKey([]string{"key"}, float32(1))
if m.getKeys()[0][0] != "test" || m.getKeys()[0][1] != "key" {
t.Fatalf("")
}
if m.vals[0] != 1 {
t.Fatalf("")
}

m, met = mockMetric()
met.EnableTypePrefix = true
met.EmitKey([]string{"key"}, float32(1))
Expand Down Expand Up @@ -122,6 +133,17 @@ func TestMetrics_IncrCounter(t *testing.T) {
t.Fatalf("")
}

m, met = mockMetric()
met.HostName = "test"
met.EnableHostname = true
met.IncrCounter([]string{"key"}, float32(1))
if m.getKeys()[0][0] != "test" || m.getKeys()[0][1] != "key" {
t.Fatalf("")
}
if m.vals[0] != 1 {
t.Fatalf("")
}

m, met = mockMetric()
met.EnableTypePrefix = true
met.IncrCounter([]string{"key"}, float32(1))
Expand Down Expand Up @@ -166,6 +188,17 @@ func TestMetrics_AddSample(t *testing.T) {
t.Fatalf("")
}

m, met = mockMetric()
met.HostName = "test"
met.EnableHostname = true
met.AddSample([]string{"key"}, float32(1))
if m.getKeys()[0][0] != "test" || m.getKeys()[0][1] != "key" {
t.Fatalf("")
}
if m.vals[0] != 1 {
t.Fatalf("")
}

m, met = mockMetric()
met.EnableTypePrefix = true
met.AddSample([]string{"key"}, float32(1))
Expand Down Expand Up @@ -213,6 +246,18 @@ func TestMetrics_MeasureSince(t *testing.T) {
t.Fatalf("")
}

m, met = mockMetric()
met.TimerGranularity = time.Millisecond
met.HostName = "test"
met.EnableHostname = true
met.MeasureSince([]string{"key"}, n)
if m.getKeys()[0][0] != "test" || m.getKeys()[0][1] != "key" {
t.Fatalf("")
}
if m.vals[0] > 0.1 {
t.Fatalf("")
}

m, met = mockMetric()
met.TimerGranularity = time.Millisecond
met.EnableTypePrefix = true
Expand Down
6 changes: 3 additions & 3 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (

// Config is used to configure metrics settings
type Config struct {
ServiceName string // Prefixed with keys to separate services
ServiceName string // Service name to be used in keys and labels
HostName string // Hostname to use. If not provided and EnableHostname, it will be os.Hostname
EnableHostname bool // Enable prefixing gauge values with hostname
EnableHostnameLabel bool // Enable adding hostname to labels
EnableServiceLabel bool // Enable adding service to labels
EnableHostnameLabel bool // Add hostname to labels. Otherwise the key is prefixed with hostname
EnableServiceLabel bool // Add service name to labels. Otherwise the key is prefixed with service name
EnableRuntimeMetrics bool // Enables profiling of runtime metrics (GC, Goroutines, Memory)
EnableTypePrefix bool // Prefixes key with a type ("counter", "gauge", "timer")
TimerGranularity time.Duration // Granularity of timers.
Expand Down

0 comments on commit 3633199

Please sign in to comment.