Skip to content

Commit d5b034c

Browse files
authored
Merge pull request #14 from InjectiveLabs/f/default-tags
feat: default tags
2 parents 2b2c14a + 621fc40 commit d5b034c

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type StatterConfig struct {
4141
EnvName string // dev/test/staging/prod
4242
HostName string // hostname
4343
Version string // version
44+
DefaultTags []interface{} // default tags for all metrics
4445
StuckFunctionTimeout time.Duration // stuck time
4546
MockingThreshold time.Duration // mocking threshold
4647
MockingEnabled bool // whether to enable mock statter, which only produce logs
@@ -52,6 +53,7 @@ type StatterConfig struct {
5253
}
5354

5455
func (m *StatterConfig) BaseTags() []string {
56+
defaultTags := Combine(m.DefaultTags...)
5557
var baseTags []string
5658

5759
switch m.Agent {
@@ -63,6 +65,9 @@ func (m *StatterConfig) BaseTags() []string {
6365
if len(config.HostName) > 0 {
6466
baseTags = append(baseTags, "machine:"+config.HostName)
6567
}
68+
for k, v := range defaultTags {
69+
baseTags = append(baseTags, k+":"+v)
70+
}
6671
// telegraf by default
6772
default:
6873
if len(config.EnvName) > 0 {
@@ -71,6 +76,9 @@ func (m *StatterConfig) BaseTags() []string {
7176
if len(config.HostName) > 0 {
7277
baseTags = append(baseTags, "machine", config.HostName)
7378
}
79+
for k, v := range defaultTags {
80+
baseTags = append(baseTags, k, v)
81+
}
7482
}
7583

7684
return baseTags

client_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package metrics
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestBaseTags(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
config *StatterConfig
13+
expected []string
14+
}{
15+
{
16+
name: "Datadog agent with default tags",
17+
config: &StatterConfig{
18+
Agent: DatadogAgent,
19+
EnvName: "prod",
20+
HostName: "host1",
21+
DefaultTags: []interface{}{
22+
"key1", "value1",
23+
"key2", "value2",
24+
},
25+
},
26+
expected: []string{"env:prod", "machine:host1", "key1:value1", "key2:value2"},
27+
},
28+
{
29+
name: "Telegraf agent with default tags",
30+
config: &StatterConfig{
31+
Agent: TelegrafAgent,
32+
EnvName: "dev",
33+
HostName: "host2",
34+
DefaultTags: []interface{}{
35+
"key1", "value1",
36+
"key2", "value2",
37+
},
38+
},
39+
expected: []string{"env", "dev", "machine", "host2", "key1", "value1", "key2", "value2"},
40+
},
41+
{
42+
name: "Datadog agent with no default tags",
43+
config: &StatterConfig{
44+
Agent: DatadogAgent,
45+
EnvName: "staging",
46+
HostName: "host3",
47+
DefaultTags: nil,
48+
},
49+
expected: []string{"env:staging", "machine:host3"},
50+
},
51+
{
52+
name: "Telegraf agent with no default tags",
53+
config: &StatterConfig{
54+
Agent: TelegrafAgent,
55+
EnvName: "test",
56+
HostName: "host4",
57+
DefaultTags: nil,
58+
},
59+
expected: []string{"env", "test", "machine", "host4"},
60+
},
61+
{
62+
name: "Empty config",
63+
config: &StatterConfig{
64+
Agent: DatadogAgent,
65+
DefaultTags: nil,
66+
},
67+
expected: nil,
68+
},
69+
}
70+
71+
for _, tt := range tests {
72+
t.Run(tt.name, func(t *testing.T) {
73+
t.Cleanup(func() {
74+
config = nil
75+
})
76+
config = tt.config
77+
result := tt.config.BaseTags()
78+
assert.ElementsMatch(t, tt.expected, result)
79+
})
80+
}
81+
}

0 commit comments

Comments
 (0)