Composite label made of values two labels

Hello everyone!
I need some help!

I need to create a label using the values of two other labels.
In short, the task is to dynamically assign a tenant to log messages based on the values of other labels. One of the labels is set earlier via stage.static_labels, and the second one is the standard namespace.

I’ve tried different approaches, but maybe I’m doing something wrong.

loki.process "set_tenant" {
        forward_to = [loki.write.loki_user.receiver]

        stage.labels {
                values = {
                        tenant = "{{$__labels__.cluster}}-{{$__labels__.__meta_kubernetes_namespace}}",
                }
        }

        stage.tenant {
                label = "tenant"
        }
}

I’ve tried other options — I assigned the namespace label and tried to use it.

        stage.labels {
                values = {
                        tenant = "{{ .labels.cluster }}-{{ .labels.namespace }}",
                }
        }
        stage.labels {
                values = {
                        tenant = "{{ cluster }}-{{ namespace }}",
                }
        }

You need to use template first to combine two labels into a temporary key, then create a label with that key afterwards.

1 Like

Thank you so much, it worked!
Maybe it will be useful to someone — here’s a working example:

    loki.process "set_tenant" {
            forward_to = [loki.write.loki.receiver]

            stage.template {
              source = "tmp_tenant"
              template = "{{ .cluster }}-{{ .namespace }}"
            }

            stage.labels {
                    values = {
                            tenant = "tmp_tenant",
                    }
            }

            stage.tenant {
                    label = "tenant"
            }
    }