I have a Kubernetes cluster with Grafana Alloy deployed as a DeamonSet with the following configuration to fetch container logs:
local.file_match "pod_logs" {
path_targets = [{"__path__" = "/var/log/pods/*/*/*.log"}]
sync_period = "5s"
}
loki.source.file "log_scrape" {
targets = local.file_match.pod_logs.targets
forward_to = [loki.write.default.receiver]
tail_from_end = true
}
loki.write "default" {
endpoint {
url = "http://observability-infra-loki-gateway/loki/api/v1/push"
}
}
Currently, the only label applied is the filename that looks like this /var/log/pods/argocd_infra-argocd-application-controller-0_3d4f2ae7-0b16-4486-963f-015b3ded1762/application-controller/0.log
I want to extract the namespace and pod name as new labels from this label. I thought about using the loki.relabel
step but haven’t found any examples of configuring it with regex.
The following regex should work but not sure how to use it to create new labels.
/var/log/pods/([^/]+)_([^_/]+)_
with two capture groups.