Pipeline attribute injection
Grafana Cloud

Pipeline attribute injection

Pipelines in Grafana Fleet Management can inject variable collector attribute values into their configuration. This powerful feature enables you to create dynamic configurations that adapt to each collector’s characteristics, reducing the need for multiple similar pipelines.

How it works

When a collector registers with Fleet Management, it can have various attributes assigned to it, such as environment type, department, or location. These attributes can be injected into configuration pipelines using the syntax argument.attributes.value["ATTRIBUTE_KEY"]. This feature allows you to:

  • Create a single pipeline that works across different environments.
  • Dynamically set labels based on collector attributes.
  • Reduce the number of pipelines needed to manage your fleet.
  • Make configurations more maintainable and scalable.

Example: Label metrics with collector attributes

In this example, we create a pipeline that adds labels to metrics based on collector attributes. Labeling helps you organize and filter metrics in Grafana Cloud.

Consider a fleet of collectors that each have these attributes:

  • env: The environment such as TEST, STAGING, PROD.
  • department: The internal department name such as BILLING, ENGINEERING, WEBAPPS.
  • collector.ID: A unique identifier for each collector.

The following pipeline demonstrates how to inject these attributes as labels:

alloy
discovery.kubernetes "example" {
  selectors {
    field = "spec.nodeName=" + sys.env("HOSTNAME")
    role = "pod"
  }
  role = "pod"
}

discovery.relabel "example" {
  targets = discovery.kubernetes.example.targets

  rule {
    source_labels = ["__address__"]
    target_label  = "env"
    replacement   = argument.attributes.value["env"]
  }

  rule {
    source_labels = ["__address__"]
    target_label  = "department"
    replacement   = argument.attributes.value["department"]
  }

  rule {
    source_labels = ["__address__"]
    target_label  = "collector_id"
    replacement   = argument.attributes.value["collector.ID"]
  }
}

prometheus.scrape "example" {
  targets    = discovery.relabel.example.targets
  forward_to = [prometheus.remote_write.example.receiver]
}

prometheus.remote_write "example" {
  endpoint {
    url = "<PROMETHEUS_REMOTE_WRITE_URL>"

    basic_auth {
      username = "<USERNAME>"
      password = "<PASSWORD>"
    }
  }
}

This configuration:

  1. Discovers Kubernetes pods using the hostname.
  2. Adds three labels to the metrics:
    • env: Set to the collector’s environment attribute.
    • department: Set to the collector’s department attribute.
    • collector_id: Set to the collector’s unique ID.
  3. Scrapes the metrics and forwards them to Grafana Cloud.

Note

The collector.ID attribute is automatically assigned to each collector. You can modify the value of this attribute by overriding the value of the id argument in the collector’s remotecfg block, but you cannot edit the attribute key or modify the key-value pair from the Fleet Management UI. This unique ID is useful for tracking metrics back to specific collectors in your fleet.