Skip to content

Commit

Permalink
feat: templatize telegraf config output (#87)
Browse files Browse the repository at this point in the history
* feat: templatize telegraf config output

* fix: templatize all keys in output
  • Loading branch information
ishanarya0 authored Nov 2, 2023
1 parent 2a0d67d commit 8579741
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
35 changes: 35 additions & 0 deletions modules/firehose/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ func (fd *firehoseDriver) getHelmRelease(res resource.Resource, conf Config,
return nil, err
}

for key, val := range conf.Telegraf.Config.Output {
valAsMap, ok := val.(map[string]interface{})
if !ok {
continue
}

valAsMap, err = renderTplOfMapStringAny(valAsMap, mergedLabelsAndEnvVariablesMap)
if err != nil {
return nil, err
}

conf.Telegraf.Config.Output[key] = valAsMap
}

telegrafConf = Telegraf{
Enabled: true,
Image: conf.Telegraf.Image,
Expand Down Expand Up @@ -420,3 +434,24 @@ func (us UsageSpec) merge(overide UsageSpec) UsageSpec {

return clone
}

func renderTplOfMapStringAny(labelsTpl map[string]any, labelsValues map[string]string) (map[string]any, error) {
outputMap := make(map[string]string)

for key, value := range labelsTpl {
if strValue, ok := value.(string); ok {
outputMap[key] = strValue
}
}

outputMap, err := renderTpl(outputMap, labelsValues)
if err != nil {
return nil, err
}

for key, val := range outputMap {
labelsTpl[key] = val
}

return labelsTpl, nil
}
8 changes: 4 additions & 4 deletions modules/firehose/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestFirehoseDriver(t *testing.T) {
"output": map[string]any{
"prometheus_remote_write": map[string]any{
"enabled": true,
"url": "http://goto.com",
"url": "http://goto.namespace-1.com",
},
},
"additional_global_tags": map[string]string{
Expand Down Expand Up @@ -358,7 +358,7 @@ func TestFirehoseDriver(t *testing.T) {
"output": map[string]any{
"prometheus_remote_write": map[string]any{
"enabled": true,
"url": "http://goto.com",
"url": "http://goto.namespace-1.com",
},
},
"additional_global_tags": map[string]string{
Expand Down Expand Up @@ -543,7 +543,7 @@ func TestFirehoseDriver(t *testing.T) {
"output": map[string]any{
"prometheus_remote_write": map[string]any{
"enabled": true,
"url": "http://goto.com",
"url": "http://goto.namespace-1.com",
},
},
"additional_global_tags": map[string]string{
Expand Down Expand Up @@ -686,7 +686,7 @@ func firehoseDriverConf() driverConf {
Output: map[string]any{
"prometheus_remote_write": map[string]any{
"enabled": true,
"url": "http://goto.com",
"url": "http://goto.{{ .namespace }}.com",
},
},
AdditionalGlobalTags: map[string]string{
Expand Down

0 comments on commit 8579741

Please sign in to comment.