helm-charts icon indicating copy to clipboard operation
helm-charts copied to clipboard

[telegraf] fix: add starlark_processor template

Open hungran opened this issue 2 years ago • 2 comments

Hi, This is another way intend to fix this issue https://github.com/influxdata/helm-charts/issues/544 Related: https://github.com/influxdata/helm-charts/pull/545 & https://github.com/influxdata/helm-charts/pull/239

values.yaml looks like:

config:
  processors: 
    - starlark: true
    - foo: 
        bar: a

result:

---


apiVersion: v1
kind: ConfigMap
metadata:
  name: example
  labels:
    helm.sh/chart: example-0.1.0
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: example
    app.kubernetes.io/instance: example
data:
  telegraf.conf: |+
    
    [agent]
    
    [[processors.starlark]]
      ## The Starlark source can be set as a string in this configuration file, or
      ## by referencing a file containing the script.  Only one source or script
      ## should be set at once.
    
      ## Source of the Starlark script.
      source = '''
    def apply(metric):
       if metric.name == "prometheus_remote_write":
            for k, v in metric.fields.items():
                metric.name = k
                metric.fields["value"] = v
                metric.fields.pop(k)
       return metric
    '''
    [[processors.foo]]
      bar = "a"

I don't know if the starlark has another scripts so I put source only like instruction can do if there are any field could be needed under starlark

  • [ ] CHANGELOG.md updated
  • [ ] Rebased/mergable
  • [ ] Tests pass
  • [x] Sign CLA (if not already signed)

hungran avatar Mar 12 '23 04:03 hungran

@powersj can you have a look?

hungran avatar Mar 12 '23 05:03 hungran

could have this under tmpl func if we need another script for starlark

{{/*
Helper function to create a starlark processor configuration block
*/}}
{{- define "telegraf.starlark_processor" -}}
[[processors.starlark]]
{{- range $key, $value := . -}}
{{- if $value -}}
{{- $tp := typeOf $value -}}
{{- if eq $tp "string" -}}
{{ $key | nindent 2 }} = {{ $value | nindent 2 }}
{{- end }}
{{- if eq $tp "float64" }}
{{ $key }} = {{ $value | int64 }}
{{- end }}
{{- if eq $tp "int" }}
{{ $key }} = {{ $value | int64 }}
{{- end }}
{{- if eq $tp "bool" }}
{{ $key }} = {{ $value }}
{{- end }}
{{- end }}
{{- end }}
{{- end -}}

hungran avatar Mar 13 '23 01:03 hungran