Skip to main content

Configuring Relabeling Rules for Data Control

Relabeling is Alloy's primary tool for controlling label metadata. It lets you standardize labels, drop noisy series, and control target metadata before scraping.

Learning goals:

  • Understand the different relabeling layers in Alloy
  • Write relabeling rules for metrics, logs, and targets
  • Apply relabeling to solve common data hygiene issues

Relabeling Layers in Alloy

Alloy has multiple relabeling components, each at a different stage:

  • discovery.relabel: modifies targets before scraping
  • prometheus.relabel: modifies metric labels after scraping
  • loki.relabel: modifies log labels before writing

Traces don't use relabeling; instead, you use processors like otelcol.processor.attributes to add or update attributes.

Target Relabeling (Discovery)

docs/grafanaalloy/examples/05-traefik.alloy
// Discovery block to set the address
discovery.relabel "traefik_node" {
targets = [{
__address__ = "localhost:8080",
}]

// Set a clean instance name
rule {
target_label = "instance"
replacement = "traefik-proxy"
}
}

// Scraper block
prometheus.scrape "traefik" {
targets = discovery.relabel.traefik_node.output
// Pointing to your global standardization gate
forward_to = [prometheus.relabel.common_labels.receiver]

job_name = "traefik"
}

Metric Relabeling (Prometheus)

docs/grafanaalloy/examples/01-global.alloy (excerpt)
prometheus.relabel "common_labels" {
forward_to = [prometheus.remote_write.grafana_cloud.receiver]

rule {
target_label = "env"
replacement = "production"
}
}

This relabeler standardizes the env label for all metrics forwarded to remote write.

Log Relabeling (Loki)

docs/grafanaalloy/examples/08-journal-logs.alloy
loki.relabel "journal" {
forward_to = []

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

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

Tips for Effective Relabeling

  • Use simple regex rules whenever possible.
  • Apply drop rules early to reduce downstream load.
  • Keep label cardinality low (avoid unique IDs as labels).
  • Centralize common labels in a shared relabeler like common_labels.

Common Pitfalls

  • Rule order mistakes: drop rules run before later rules and can remove data you still need.
  • Mixing stages: Target relabeling and metric relabeling serve different purposes.
  • Over-cleaning labels: Removing too many labels can make debugging difficult.

Summary

Relabeling is Alloy's main tool for metadata control. You now understand:

  • When to use discovery.relabel, prometheus.relabel, and loki.relabel
  • How to standardize labels and drop noisy series
  • Why target relabeling and metric relabeling are different

Quiz

Relabeling Rules - Quick Check

What is the primary purpose of relabeling in Grafana Alloy?

Question 1/5