Alloy - Loki - how to create a new label based on the regex content from another field in the JSON log line

Hi
I have a JSON stream log, that I need to manipulate.

logic to implement :

if condition = url_path_field contains “pdp”:
page_group_label = “pdp”

elif condition = url_path_field contains “plp”:
page_group_label = “plp”

elif condition = url_path_field contains “/”:
page_group_label = “home”

elif condition = url_path_field contains “basket”:
page_group_label = “basket”

else:
page_group_label = “other”

Thx in advance

You’ll need to use match to perform action based on condition (see loki.process | Grafana Alloy documentation). There is also no if/else logic, so you need to first set a default value, then change it based on condition. Example (not tested):

stage.static_labels {
    values = {
      page_group_label = "other",
    }
}

stage.match {
    selector = `{url_path_field=~".*pdp.*"}`

    stage.static_labels {
      values = {
        page_group_label = "pdp",
      }
    }
}

stage.match {
    selector = `{url_path_field=~".*plp.*"}`

    stage.static_labels {
      values = {
        page_group_label = "plp",
      }
    }
}

stage.match {
    selector = `{url_path_field=~".*basket.*"}`

    stage.static_labels {
      values = {
        page_group_label = "basket",
      }
    }
}

stage.match {
    selector = `{url_path_field="/"}`

    stage.static_labels {
      values = {
        page_group_label = "home",
      }
    }
}

Hi Tony
Thx for the help

i have added the following code to the alloy - loki.process :

// test
stage.static_labels {
values = { page_group_label = “other” }
}

stage.match {
selector = “{properties.requestUri=”/“}”
stage.static_labels {
values = { page_group_label = “home” }
}
}

Json:

error msg :

any suggestion?

Try this:

    selector = "{url_path_field=\"/\"}"