chore(go.d/snmp): refactor static_tags to structured key/value format (#21180)
Ilya Mashchenko committed
Oct 21, 2025 at 19:49 UTC
8e0ec6de4ca0b456b3acf5d89bce5f58e69ee868
8 files changed
+39
-28
src/go/plugin/go.d/collector/snmp/ddsnmp/ddprofiledefinition/metrics.go
+14
-2
@@ -56,8 +56,8 @@ type MetricsConfig struct {
56
Symbols []SymbolConfig `yaml:"symbols,omitempty" json:"symbols,omitempty"`
57
58
// `static_tags` is not exposed as json at the moment since we need to evaluate if we want to expose it via UI
59
- StaticTags []string `yaml:"static_tags,omitempty" json:"-"`
60
- MetricTags MetricTagConfigList `yaml:"metric_tags,omitempty" json:"metric_tags,omitempty"`
59
+ StaticTags []StaticMetricTagConfig `yaml:"static_tags,omitempty" json:"-"`
60
+ MetricTags MetricTagConfigList `yaml:"metric_tags,omitempty" json:"metric_tags,omitempty"`
61
62
Options MetricsConfigOption `yaml:"options,omitempty" json:"options,omitempty"`
63
@@ -202,6 +202,18 @@ func (m MetricTagConfig) Clone() MetricTagConfig {
202
return m2
203
}
204
205
+type StaticMetricTagConfig struct {
206
+ Tag string `yaml:"tag" json:"tag"`
207
+ Value string `yaml:"value" json:"value"`
208
+}
209
+
210
+func (s StaticMetricTagConfig) Clone() StaticMetricTagConfig {
211
+ return StaticMetricTagConfig{
212
+ Tag: s.Tag,
213
+ Value: s.Value,
214
+ }
215
+}
216
+
217
// MetricTagConfigList holds configs for a list of metric tags
218
type MetricTagConfigList []MetricTagConfig
219
src/go/plugin/go.d/collector/snmp/ddsnmp/ddprofiledefinition/metrics_test.go
+3
-4
@@ -140,9 +140,8 @@ func TestCloneMetricsConfig(t *testing.T) {
140
ExtractValueCompiled: regexp.MustCompile(".*"),
141
},
142
},
143
- StaticTags: []string{
144
- "foo",
145
- "bar",
143
+ StaticTags: []StaticMetricTagConfig{
144
+ {Tag: "foo", Value: "bar"},
145
},
146
MetricTags: []MetricTagConfig{
147
{
@@ -161,7 +160,7 @@ func TestCloneMetricsConfig(t *testing.T) {
160
161
conf2 := conf.Clone()
162
assert.Equal(t, conf, conf2)
164
- conf2.StaticTags[0] = "baz"
163
+ conf2.StaticTags[0] = StaticMetricTagConfig{Tag: "bar", Value: "baz"}
164
conf2.MetricTags[0].IndexTransform = []MetricIndexTransform{{5, 7}}
165
conf2.Options.Placement = 2
166
conf2.Options.MetricSuffix = ".bar"
src/go/plugin/go.d/collector/snmp/ddsnmp/ddprofiledefinition/profile_definition.go
+1
-1
@@ -15,7 +15,7 @@ type ProfileDefinition struct {
15
SysobjectIDMetadata []SysobjectIDMetadataEntryConfig `yaml:"sysobjectid_metadata,omitempty"`
16
Metrics []MetricsConfig `yaml:"metrics,omitempty" json:"metrics,omitempty"`
17
MetricTags []MetricTagConfig `yaml:"metric_tags,omitempty" json:"metric_tags,omitempty"`
18
- StaticTags []string `yaml:"static_tags,omitempty" json:"static_tags,omitempty"`
18
+ StaticTags []StaticMetricTagConfig `yaml:"static_tags,omitempty" json:"static_tags,omitempty"`
19
20
VirtualMetrics []VirtualMetricConfig `yaml:"virtual_metrics,omitempty" json:"virtual_metrics,omitempty"`
21
src/go/plugin/go.d/collector/snmp/ddsnmp/ddsnmpcollector/collector_global_tags.go
+1
-1
@@ -48,7 +48,7 @@ func (gc *globalTagsCollector) Collect(prof *ddsnmp.Profile) (map[string]string,
48
return tags, nil
49
}
50
51
-func (gc *globalTagsCollector) processStaticTags(staticTags []string, globalTags map[string]string) {
51
+func (gc *globalTagsCollector) processStaticTags(staticTags []ddprofiledefinition.StaticMetricTagConfig, globalTags map[string]string) {
52
ta := tagAdder{tags: globalTags}
53
ta.addTags(parseStaticTags(staticTags))
54
}
src/go/plugin/go.d/collector/snmp/ddsnmp/ddsnmpcollector/collector_global_tags_test.go
+8
-8
@@ -29,7 +29,7 @@ func TestGlobalTagsCollector_Collect(t *testing.T) {
29
profile: &ddsnmp.Profile{
30
Definition: &ddprofiledefinition.ProfileDefinition{
31
MetricTags: []ddprofiledefinition.MetricTagConfig{},
32
- StaticTags: []string{},
32
+ StaticTags: []ddprofiledefinition.StaticMetricTagConfig{},
33
},
34
},
35
setupMock: func(m *snmpmock.MockHandler) {},
@@ -39,10 +39,10 @@ func TestGlobalTagsCollector_Collect(t *testing.T) {
39
"static tags only": {
40
profile: &ddsnmp.Profile{
41
Definition: &ddprofiledefinition.ProfileDefinition{
42
- StaticTags: []string{
43
- "environment:production",
44
- "region:us-east-1",
45
- "service:network",
42
+ StaticTags: []ddprofiledefinition.StaticMetricTagConfig{
43
+ {Tag: "environment", Value: "production"},
44
+ {Tag: "region", Value: "us-east-1"},
45
+ {Tag: "service", Value: "network"},
46
},
47
},
48
},
@@ -106,9 +106,9 @@ func TestGlobalTagsCollector_Collect(t *testing.T) {
106
"mixed static and dynamic tags": {
107
profile: &ddsnmp.Profile{
108
Definition: &ddprofiledefinition.ProfileDefinition{
109
- StaticTags: []string{
110
- "environment:production",
111
- "managed:true",
109
+ StaticTags: []ddprofiledefinition.StaticMetricTagConfig{
110
+ {Tag: "environment", Value: "production"},
111
+ {Tag: "managed", Value: "true"},
112
},
113
MetricTags: []ddprofiledefinition.MetricTagConfig{
114
{
src/go/plugin/go.d/collector/snmp/ddsnmp/ddsnmpcollector/collector_scalar_test.go
+3
-3
@@ -274,9 +274,9 @@ func TestScalarCollector_Collect(t *testing.T) {
274
OID: "1.3.6.1.2.1.1.3.0",
275
Name: "sysUpTime",
276
},
277
- StaticTags: []string{
278
- "source:system",
279
- "type:uptime",
277
+ StaticTags: []ddprofiledefinition.StaticMetricTagConfig{
278
+ {Tag: "source", Value: "system"},
279
+ {Tag: "type", Value: "uptime"},
280
},
281
},
282
},
src/go/plugin/go.d/collector/snmp/ddsnmp/ddsnmpcollector/collector_table.go
+4
-4
@@ -597,11 +597,11 @@ func (tc *tableCollector) snmpGet(oids []string) (map[string]gosnmp.SnmpPDU, err
597
return pdus, nil
598
}
599
600
-func parseStaticTags(staticTags []string) map[string]string {
601
- tags := make(map[string]string)
600
+func parseStaticTags(staticTags []ddprofiledefinition.StaticMetricTagConfig) map[string]string {
601
+ tags := make(map[string]string, len(staticTags))
602
for _, tag := range staticTags {
603
- if n, v, _ := strings.Cut(tag, ":"); n != "" && v != "" {
604
- tags[n] = v
603
+ if tag.Tag != "" && tag.Value != "" {
604
+ tags[tag.Tag] = tag.Value
605
}
606
}
607
return tags
src/go/plugin/go.d/collector/snmp/ddsnmp/ddsnmpcollector/collector_table_test.go
+5
-5
@@ -780,9 +780,9 @@ func TestTableCollector_Collect(t *testing.T) {
780
Name: "ifInOctets",
781
},
782
},
783
- StaticTags: []string{
784
- "source:interface",
785
- "table:if",
783
+ StaticTags: []ddprofiledefinition.StaticMetricTagConfig{
784
+ {Tag: "source", Value: "interface"},
785
+ {Tag: "table", Value: "if"},
786
},
787
MetricTags: []ddprofiledefinition.MetricTagConfig{
788
{
@@ -1004,8 +1004,8 @@ func TestTableCollector_Collect(t *testing.T) {
1004
Name: "ifInOctets",
1005
},
1006
},
1007
- StaticTags: []string{
1008
- "source:network",
1007
+ StaticTags: []ddprofiledefinition.StaticMetricTagConfig{
1008
+ {Tag: "source", Value: "network"},
1009
},
1010
MetricTags: []ddprofiledefinition.MetricTagConfig{
1011
{