grafana / k8s-monitoring-helm

Apache License 2.0
135 stars 57 forks source link

Loki stage.template functions are not available #584

Open fhqwhgadss opened 1 week ago

fhqwhgadss commented 1 week ago

I find myself unable to use the custom functions in Loki stage.template blocks added through logs.pod_logs.extraStageBlocks.

Here is my values.yaml with one of the example usages of stage.template from the docs:

cluster:
  name: "k8s-monitor"
  platform: ""

externalServices:
  loki:
    host: "http://loki-loki-distributed-gateway"
    protocol: "loki"
    queryEndpoint: /loki/api/v1/query
    writeEndpoint: /loki/api/v1/push
    authMode: none

logs:
  enabled: true

  pod_logs:
    enabled: true
    extraStageBlocks: |
      stage.template {
        source   = "app"
        template = "{{ ToLower .Value }}_some_suffix"
      }

metrics:
  enabled: false

configValidator:
  enabled: true

test:
  enabled: true

prometheus-operator-crds:
  enabled: false

When deploying, the config validator fails with the following output:

Error: UPGRADE FAILED: template: k8s-monitoring/templates/hooks/validate-configuration.yaml:123:8: 
executing "k8s-monitoring/templates/hooks/validate-configuration.yaml" at <include "alloyConfig" .>: 
error calling include: template: k8s-monitoring/templates/_configs.tpl:92:8: executing "alloyConfig" at <include "alloy.config.logs.pod_logs_processor" .>: 
error calling include: template: k8s-monitoring/templates/alloy_config/_pod_logs_processor.alloy.txt:53:3: 
executing "alloy.config.logs.pod_logs_processor" at <tpl .Values.logs.pod_logs.extraStageBlocks $>: 
error calling tpl: error during tpl function execution for "stage.template {\n  source   = \"app\"\n  template = \"{{ ToLower .Value }}_some_suffix\"\n}\n": 
parse error at (k8s-monitoring/templates/hooks/validate-configuration.yaml:3): function "ToLower" not defined
petewall commented 1 week ago

The issue is that the extraStageBlocks are first evaluated by Helm, which things you're adding a Helm template directive.

It's ugly, but try this:

logs:
  enabled: true

  pod_logs:
    enabled: true
    extraStageBlocks: |
      stage.template {
        source   = "app"
        template = "{{ "{{" }} ToLower .Value }}_some_suffix"
      }
petewall commented 1 week ago

Or, put the whole think inside a Helm template string:


The issue is that the extraStageBlocks are first evaluated by Helm, which things you're adding a Helm template directive.

It's ugly, but try this:

logs:
  enabled: true

  pod_logs:
    enabled: true
    extraStageBlocks: |
      stage.template {
        source   = "app"
        template = {{ "{{ ToLower .Value }}_some_suffix" | quote }}
      }
fhqwhgadss commented 1 week ago

Or, put the whole think inside a Helm template string:


The issue is that the extraStageBlocks are first evaluated by Helm, which things you're adding a Helm template directive.

It's ugly, but try this:

logs:
  enabled: true

  pod_logs:
    enabled: true
    extraStageBlocks: |
      stage.template {
        source   = "app"
        template = {{ "{{ ToLower .Value }}_some_suffix" | quote }}
      }

That does the trick, thank you!