@cryptotaxi247 / netdata-1 / commits / d5384db76

go.d.plugin: sd compose: allow multi config template (#17157)

Ilya Mashchenko committed Mar 14, 2024 at 13:04 UTC d5384db76074f6344a6fa9396f22393e3b0f420a
3 files changed +49 -9
src/go/collectors/go.d.plugin/agent/discovery/sd/pipeline/compose.go
+34 -5
@@ -4,6 +4,7 @@ package pipeline
4
5 import (
6 "bytes"
7 + "errors"
8 "fmt"
9 "text/template"
10
@@ -70,14 +71,13 @@ func (c *configComposer) compose(tgt model.Target) []confgroup.Config {
71 continue
72 }
73
73 - var cfg confgroup.Config
74 -
75 - if err := yaml.Unmarshal(c.buf.Bytes(), &cfg); err != nil {
76 - c.Warningf("failed on yaml unmarshalling: %v", err)
74 + cfgs, err := c.parseTemplateData(c.buf.Bytes())
75 + if err != nil {
76 + c.Warningf("failed to parse template data: %v", err)
77 continue
78 }
79
80 - configs = append(configs, cfg)
80 + configs = append(configs, cfgs...)
81 }
82 }
83
@@ -87,6 +87,35 @@ func (c *configComposer) compose(tgt model.Target) []confgroup.Config {
87 return configs
88 }
89
90 +func (c *configComposer) parseTemplateData(bs []byte) ([]confgroup.Config, error) {
91 + var data any
92 + if err := yaml.Unmarshal(bs, &data); err != nil {
93 + return nil, err
94 + }
95 +
96 + type (
97 + single = map[any]any
98 + multi = []any
99 + )
100 +
101 + switch data.(type) {
102 + case single:
103 + var cfg confgroup.Config
104 + if err := yaml.Unmarshal(bs, &cfg); err != nil {
105 + return nil, err
106 + }
107 + return []confgroup.Config{cfg}, nil
108 + case multi:
109 + var cfgs []confgroup.Config
110 + if err := yaml.Unmarshal(bs, &cfgs); err != nil {
111 + return nil, err
112 + }
113 + return cfgs, nil
114 + default:
115 + return nil, errors.New("unknown config format")
116 + }
117 +}
118 +
119 func newComposeRules(cfg []ComposeRuleConfig) ([]*composeRule, error) {
120 var rules []*composeRule
121
src/go/collectors/go.d.plugin/agent/discovery/sd/pipeline/compose_test.go
+3 -1
@@ -38,7 +38,8 @@ func TestConfigComposer_compose(t *testing.T) {
38 name: {{ .Name }}-5
39 - selector: "bar6"
40 template: |
41 - name: {{ .Name }}-6
41 + - name: {{ .Name }}-6
42 + - name: {{ .Name }}-7
43 `
44 tests := map[string]struct {
45 target model.Target
@@ -70,6 +71,7 @@ func TestConfigComposer_compose(t *testing.T) {
71 {"name": "mock-4"},
72 {"name": "mock-5"},
73 {"name": "mock-6"},
74 + {"name": "mock-7"},
75 },
76 },
77 }
src/go/collectors/go.d.plugin/config/go.d/sd/net_listeners.conf
+12 -3
@@ -278,9 +278,18 @@ compose:
278 dsn: netdata@tcp(127.0.0.1:{{.Port}})/
279 - selector: "nginx"
280 template: |
281 - module: nginx
282 - name: local
283 - url: http://localhost:{{.Port}}/stub_status
281 + - module: nginx
282 + name: local
283 + url: http://localhost:{{.Port}}/stub_status
284 + - module: nginx
285 + name: local
286 + url: http://localhost:{{.Port}}/basic_status
287 + - module: nginx
288 + name: local
289 + url: http://localhost:{{.Port}}/nginx_status
290 + - module: nginx
291 + name: local
292 + url: http://localhost:{{.Port}}/status
293 - selector: "ntpd"
294 template: |
295 module: ntpd