Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion manager/formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"gopkg.in/yaml.v3"
)

// InvalidConsulMetaKey targets invalid chars in metadata keys
var InvalidConsulMetaKey = regexp.MustCompile("[^a-zA-Z0-9_-]")

// FormatJson attempts to convert tags into a JSON
// string. Returns error if the conversion fails.
func FormatJson(tags Tags) (string, error) {
Expand Down Expand Up @@ -174,10 +177,25 @@ func FormatTelegraf(tags Tags) (string, error) {
// conversion fails.
func FormatConsul(tags Tags) (string, error) {

filtered := make(Tags)
// Iterate through all the tags
for key, value := range tags {

// Skip empty
if key == "" {
continue
}

// Replace any invalid characters with an underscore
k := InvalidConsulMetaKey.ReplaceAllString(key, "_")

filtered[k] = value // Keys are deduplicated here
}

consul := struct {
NodeMeta Tags `json:"node_meta"`
}{
NodeMeta: tags,
NodeMeta: filtered,
}

// Try and convert specified consul data to JSON
Expand Down