Add `hostname` label to Grafana Loki `loki.relabel`

Hi there! We use Grafana Alloy for Loki to gather all logs to push to a centralized Loki server. We wish to add the server hostname as a label. We’ve tried the following, but it doesn’t seem to work, and cannot find the right resources on how to set this up. Any tips?

loki.relabel "journal" {
  forward_to = []

  rule {
    source_labels = ["__journal__systemd_unit"]
    target_label  = "unit"
  }

  rule {
    source_labels = ["__hostname__"]
    target_label  = "hostname"
  }
}

loki.source.journal "read"  {
  forward_to    = [loki.process.filter_logs.receiver]
  relabel_rules = loki.relabel.journal.rules
  labels        = {component = "loki.source.journal"}
}

Have you tried with __journal__hostname as source label?

1 Like

That did the trick! Thanks!

Any way to get the full hostname? i.e. it’s now sending ams0, we wish to send the hostname from hostname -f (i.e. ams0.prod.domain.tld)

Since that’s a label from journald, you’d have to configure journald to include that. I am not very knowledge at that, but perhaps look at journald.conf.

1 Like

Thanks for your help! Will look there.