Unknown escape sequence when parsing alloy.config

Hi,

The following config is synthesized from the Grafana docs but gives syntax errors

Error: /etc/alloy/config.alloy:88:17: unknown escape sequence

Error: /etc/alloy/config.alloy:88:22: unknown escape sequence

Error: /etc/alloy/config.alloy:88:24: unknown escape sequence

loki.write "k8s_logs" {
  endpoint {
    url = "http://loki-simple-scalable-gateway/loki/api/v1/push"
  }
}
// local.file_match discovers files on the local filesystem using glob patterns and the doublestar library. It returns an array of file paths.
local.file_match "node_logs" {
  path_targets = [{
      // Monitor syslog to scrape node-logs
      __path__  = "/var/log/syslog",
      job       = "node/syslog",
      node_name = env("HOSTNAME"),
      cluster   = "easi-dev", // Template this
  }]
}

// loki.source.file reads log entries from files and forwards them to other loki.* components.
// You can specify multiple loki.source.file components by giving them different labels.
loki.source.file "node_logs" {
  targets    = local.file_match.node_logs.targets
  forward_to = [loki.write.k8s_logs.receiver]
}

// discovery.kubernetes allows you to find scrape targets from Kubernetes resources.
// It watches cluster state and ensures targets are continually synced with what is currently running in your cluster.
discovery.kubernetes "pod" {
  role = "pod"
}

// discovery.relabel rewrites the label set of the input targets by applying one or more relabeling rules.
// If no rules are defined, then the input targets are exported as-is.
discovery.relabel "pod_logs" {
  targets = discovery.kubernetes.pod.targets

  // Label creation - "namespace" field from "__meta_kubernetes_namespace"
  rule {
    source_labels = ["__meta_kubernetes_namespace"]
    action = "replace"
    target_label = "namespace"
  }

  // Label creation - "pod" field from "__meta_kubernetes_pod_name"
  rule {
    source_labels = ["__meta_kubernetes_pod_name"]
    action = "replace"
    target_label = "pod"
  }

  // Label creation - "container" field from "__meta_kubernetes_pod_container_name"
  rule {
    source_labels = ["__meta_kubernetes_pod_container_name"]
    action = "replace"
    target_label = "container"
  }

  // Label creation -  "app" field from "__meta_kubernetes_pod_label_app_kubernetes_io_name"
  rule {
    source_labels = ["__meta_kubernetes_pod_label_app_kubernetes_io_name"]
    action = "replace"
    target_label = "app"
  }

  // Label creation -  "job" field from "__meta_kubernetes_namespace" and "__meta_kubernetes_pod_container_name"
  // Concatenate values __meta_kubernetes_namespace/__meta_kubernetes_pod_container_name
  rule {
    source_labels = ["__meta_kubernetes_namespace", "__meta_kubernetes_pod_container_name"]
    action = "replace"
    target_label = "job"
    separator = "/"
    replacement = "$1"
  }

  // Label creation - "container" field from "__meta_kubernetes_pod_uid" and "__meta_kubernetes_pod_container_name"
  // Concatenate values __meta_kubernetes_pod_uid/__meta_kubernetes_pod_container_name.log
  rule {
    source_labels = ["__meta_kubernetes_pod_uid", "__meta_kubernetes_pod_container_name"]
    action = "replace"
    target_label = "__path__"
    separator = "/"
    replacement = "/var/log/pods/*$1/*.log"
  }

  // Label creation -  "container_runtime" field from "__meta_kubernetes_pod_container_id"
  rule {
    source_labels = ["__meta_kubernetes_pod_container_id"]
    action = "replace"
    target_label = "container_runtime"
    regex = "^(\S+):\/\/.+$"
    replacement = "$1"
  }
}

// loki.source.kubernetes tails logs from Kubernetes containers using the Kubernetes API.
loki.source.kubernetes "pod_logs" {
  targets    = discovery.relabel.pod_logs.output
  forward_to = [loki.process.pod_logs.receiver]
}

// loki.process receives log entries from other Loki components, applies one or more processing stages,
// and forwards the results to the list of receivers in the component’s arguments.
loki.process "pod_logs" {
  stage.static_labels {
      values = {
        cluster = "easi-dev", // Template this
      }
  }

  forward_to = [loki.write.k8s_logs.receiver]
}

What would be the reason for this.

Cheers,

whatnick

Hey bud, the error message points to line 88, indexes 17, 22, and 24.

In your config, that’s the line regex = "^(\S+):\/\/.+$"
In double quoted strings \ is the start of an escape sequence but \S and \/ aren’t known escape sequences.

The most readable way to rewrite this line is to use raw strings which are delimited by backticks (`).

regex = `^(\S+):\/\/.+$`

Thanks a lot for the assist. These snippets are from the docs - Collect Kubernetes logs and forward them to Loki | Grafana Alloy documentation . It would be nice to have the backtick raw text sample there. If using a configmap via k8s manifests instead of helm values the string formatting does not seem to be an issue since it is not parsed in and fed into a configmap generator.

I’ve opened an issue in Alloy to look into adding another example to the Collect doc topic you linked: Docs feedback: · Issue #1681 · grafana/alloy · GitHub