Skip to content

Commit b1b5e07

Browse files
authored
Merge pull request #610 from nlgotz/ng/consul-target-name
feat: Templated names and tags for Consul targets
2 parents 9e56b1b + 2dec2f6 commit b1b5e07

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

docs/user_guide/targets/target_discovery/consul_discovery.md

+22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ loader:
2323
password: admin
2424
```
2525
26+
### Templating with Consul
27+
28+
It is possible to set the target name to something other than the Consul Service ID using the `name` field under the config. The target name can be customized using [Go Templates](https://golang.org/pkg/text/template/).
29+
30+
In addition to setting the target name, it is also possible to use Go Templates on `event-tags` as well.
31+
32+
The templates use the Service under Consul, so access to things like `ID`, `Tags`, `Meta`, etc. are all available.
33+
34+
```yaml
35+
loader:
36+
type: consul
37+
services:
38+
- name: cluster1-gnmi-server
39+
config:
40+
name: "{{.Meta.device}}"
41+
event-tags:
42+
location: "{{.Meta.site_name}}"
43+
model: "{{.Meta.device_type}}"
44+
tag-1: "{{.Meta.tag_1}}"
45+
boring-static-tag: "hello"
46+
```
47+
2648
### Configuration
2749

2850
```yaml

pkg/config/loader.go

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ func (c *Config) GetLoader() error {
5050
}
5151
return os.ExpandEnv(v)
5252
})
53-
fmt.Printf("LOADER: %+v\n", c.Loader)
5453
return nil
5554
}
5655
}

pkg/loaders/consul_loader/consul_loader.go

+52
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package consul_loader
1010

1111
import (
12+
"bytes"
1213
"context"
1314
"encoding/json"
1415
"errors"
@@ -17,7 +18,9 @@ import (
1718
"log"
1819
"net"
1920
"strconv"
21+
"strings"
2022
"sync"
23+
"text/template"
2124
"time"
2225

2326
"gopkg.in/yaml.v2"
@@ -110,6 +113,8 @@ type serviceDef struct {
110113
Config map[string]interface{} `mapstructure:"config,omitempty" json:"config,omitempty"`
111114

112115
tags map[string]struct{}
116+
targetNameTemplate *template.Template
117+
targetTagsTemplate map[string]*template.Template
113118
}
114119

115120
func (c *consulLoader) Init(ctx context.Context, cfg map[string]interface{}, logger *log.Logger, opts ...loaders.Option) error {
@@ -403,7 +408,54 @@ SRV:
403408
tc.Address = se.Node.Address
404409
}
405410
tc.Address = net.JoinHostPort(tc.Address, strconv.Itoa(se.Service.Port))
411+
412+
var buffer bytes.Buffer
413+
406414
tc.Name = se.Service.ID
415+
416+
if configName, ok := sd.Config["name"].(string); ok {
417+
nameTemplate, err := template.New("targetName").Option("missingkey=zero").Parse(configName)
418+
if err != nil {
419+
c.logger.Println("Could not parse nameTemplate")
420+
}
421+
sd.targetNameTemplate = nameTemplate
422+
423+
buffer.Reset()
424+
err = sd.targetNameTemplate.Execute(&buffer, se.Service)
425+
if err != nil {
426+
c.logger.Println("Could not execute nameTemplate")
427+
continue
428+
}
429+
tc.Name = buffer.String()
430+
}
431+
432+
// Create Event tags from Consul via templates
433+
if configEventTags, ok := sd.Config["event-tags"].(map[string]interface{}); ok {
434+
// Allow to use join function in tags
435+
templateFunctions := template.FuncMap{"join": strings.Join}
436+
437+
sd.targetTagsTemplate = make(map[string]*template.Template)
438+
for tagName, tagTemplateString := range configEventTags {
439+
tagTemplate, err := template.New(tagName).Funcs(templateFunctions).Option("missingkey=zero").Parse(fmt.Sprintf("%v",tagTemplateString))
440+
if err != nil {
441+
c.logger.Println("Could not parse tagTemplate:", tagName)
442+
continue
443+
}
444+
sd.targetTagsTemplate[tagName] = tagTemplate
445+
}
446+
447+
eventTags := make(map[string]string)
448+
for tagName, tagTemplate := range sd.targetTagsTemplate {
449+
buffer.Reset()
450+
err := tagTemplate.Execute(&buffer, se.Service)
451+
if err != nil {
452+
c.logger.Println("Could not execute tagTemplate:", tagName)
453+
return nil, err
454+
}
455+
eventTags[tagName] = buffer.String()
456+
}
457+
tc.EventTags = eventTags
458+
}
407459
return tc, nil
408460
}
409461

0 commit comments

Comments
 (0)