组件
组件是 Alloy 的构建块。每个组件执行一个单一任务,例如检索密钥或收集 Prometheus 指标。
组件包含以下内容
- 参数: 配置组件的设置。
- 导出: 组件提供给其他组件的命名值。
每个组件都有一个描述其职责的名称。例如,local.file
组件检索磁盘上文件的内容。
您可以通过在配置文件中指定组件名称和用户定义的标签来定义组件,然后是用于配置组件的参数。
discovery.kubernetes "pods" {
role = "pod"
}
discovery.kubernetes "nodes" {
role = "node"
}
您可以通过将组件名称与其标签组合来引用组件。例如,您可以将标签为 foo
的 local.file
组件引用为 local.file.foo
。
组件名称和标签的组合在配置文件中必须是唯一的。这种命名方法允许您定义同一组件的多个实例,只要每个实例具有唯一的标签即可。
流水线
配置文件中组件的大多数参数都是常量值,例如将 log_level
属性设置为 "debug"
。
log_level = "debug"
您使用表达式在运行时动态计算参数的值。表达式可以检索环境变量值(log_level = sys.env("LOG_LEVEL")
)或引用另一个组件的导出字段(log_level = local.file.log_level.content
)。
当一个组件的参数引用另一个组件的导出字段时,就创建了一个依赖关系。该组件的参数依赖于其他组件的导出。每当引用的组件的导出更新时,该组件的输入都会被重新评估。
数据通过这些引用形成的流称为流水线。
一个示例流水线可能如下所示
- 一个
local.file
组件监视包含 API 密钥的文件。 - 一个
prometheus.remote_write
组件接收指标并将它们转发到外部数据库,使用local.file
中的 API 密钥进行身份验证。 - 一个
discovery.kubernetes
组件发现并导出可以收集指标的 Kubernetes Pods。 - 一个
prometheus.scrape
组件引用前一个组件的导出,并将收集到的指标发送到prometheus.remote_write
组件。

以下配置文件代表了该流水线。
// Get our API key from disk.
//
// This component has an exported field called "content", holding the content
// of the file.
//
// local.file will watch the file and update its exports any time the
// file changes.
local.file "api_key" {
filename = "/var/data/secrets/api-key"
// Mark this file as sensitive to prevent its value from being shown in the
// UI.
is_secret = true
}
// Create a prometheus.remote_write component, which other components can send
// metrics to.
//
// This component exports a "receiver" value, which can be used by other
// components to send metrics.
prometheus.remote_write "prod" {
endpoint {
url = "https://prod:9090/api/v1/write"
basic_auth {
username = "admin"
// Use the password file to authenticate with the production database.
password = local.file.api_key.content
}
}
}
// Find Kubernetes pods where we can collect metrics.
//
// This component exports a "targets" value, which contains the list of
// discovered pods.
discovery.kubernetes "pods" {
role = "pod"
}
// Collect metrics from Kubernetes pods and send them to prod.
prometheus.scrape "default" {
targets = discovery.kubernetes.pods.targets
forward_to = [prometheus.remote_write.prod.receiver]
}