EXPERIMENTAL: This is an
experimental feature.
Experimental features are subject to frequent breaking changes, and may be removed with no equivalent replacement.
To enable and use an experimental feature, you must set the stability.levelflag to experimental.
The foreach block runs a separate pipeline for each item inside a list.
Name of the variable referring to the current item in the collection.
yes
enable_metrics
bool
Whether to expose debug metrics in the Alloy /metrics endpoint.
false
no
id
string
Name of the field to use from collection items for child component’s identification.
""
no
hash_string_id
bool
Whether to hash the string representation of the id of the collection items.
false
no
The items in the collection list can be of any type
type, such as a bool, a string, a list, or a map.
When using a collection of strings or when the id field is a string, you can set hash_string_id to true to hash the string representation of the id field instead of using the string itself. This is recommended when the strings are long because the values will be used to identify the components that are created dynamically in metrics, logs and in the UI.
Warning
Setting enable_metrics to true when collection has lots of elements may cause a large number of metrics to appear on the Alloy /metric endpoint.
Note
When id is set and enable_metrics is true, component paths in metrics use the value of the specified field from collection items instead of a hash of the entire item.
For example:
The template block contains the definition of Alloy components which will be ran for every item in the collection.
The contents of the block look like a normal Alloy configuration file,
except that you can use the keyword defined in var to refer to the current item in the collection.
Components inside the template block can use exports of components defined outside of the foreach block.
However, components outside of the foreach cannot use exports from components defined inside the template block of a foreach.
Example
The following example shows you how to run Prometheus exporters dynamically on service discovery targets.
prometheus.exporter.* components often require the address of one particular instance being monitored.
For example, prometheus.exporter.redis has a redis_addr attribute for the Redis instance under observation.
On the other hand, discovery.* components such as discovery.kubernetes output a list of targets such as this:
You can use a foreach to loop over each target and start a separate component pipeline for it.
The following example configuration shows how a prometheus.exporter.redis instance is started for each Redis instance discovered by discovery.kubernetes.
Additional Kubernetes labels from discovery.kubernetes are also added to the metrics created by prometheus.exporter.redis.
alloy
discovery.kubernetes"default"{role ="pod"}discovery.relabel"redis"{targets = discovery.kubernetes.default.targets
// Remove all targets except the Redis ones.rule{source_labels =["__meta_kubernetes_pod_container_name"]regex ="redis-cont"action ="keep"}}// Collect metrics for each Redis instance.foreach"redis"{collection = discovery.relabel.redis.output
var ="each"template{prometheus.exporter.redis"default"{// This is the "__address__" label from discovery.kubernetes.redis_addr = each["__address__"]}prometheus.scrape"default"{targets = prometheus.exporter.redis.default.targets
forward_to =[prometheus.relabel.default.receiver]}// Add labels from discovery.kubernetes.prometheus.relabel"default"{rule{replacement = each["__meta_kubernetes_namespace"]target_label ="k8s_namespace"action ="replace"}rule{replacement = each["__meta_kubernetes_pod_container_name"]target_label ="k8s_pod_container_name"action ="replace"}forward_to =[prometheus.remote_write.mimir.receiver]}}}prometheus.remote_write"mimir"{endpoint{url ="https://prometheus-xxx.grafana.net/api/prom/push"basic_auth{username = sys.env("<PROMETHEUS_USERNAME>")password = sys.env("<GRAFANA_CLOUD_API_KEY>")}}}
Replace the following:
<PROMETHEUS_USERNAME>: Your Prometheus username.
<GRAFANA_CLOUD_API_KEY>: Your Grafana Cloud API key.