Alloy 配置语法
Grafana Alloy 使用 Alloy 配置语法动态配置和连接组件。Alloy 收集、转换和交付遥测数据。每个配置组件执行特定任务或定义数据流和组件连接。

以下示例演示了基本概念以及 Alloy 配置如何形成一个管道。
// Collection: mount a local directory with a certain path spec
local.file_match "applogs" {
path_targets = [{"__path__" = "/tmp/app-logs/app.log"}]
}
// Collection: Take the file match as input, and scrape those mounted log files
loki.source.file "local_files" {
targets = local.file_match.applogs.targets
// This specifies which component should process the logs next, the "link in the chain"
forward_to = [loki.process.add_new_label.receiver]
}
// Transformation: pull some data out of the log message, and turn it into a label
loki.process "add_new_label" {
stage.logfmt {
mapping = {
"extracted_level" = "level",
}
}
// Add the value of "extracted_level" from the extracted map as a "level" label
stage.labels {
values = {
"level" = "extracted_level",
}
}
// The next link in the chain is the local_loki "receiver" (receives the telemetry)
forward_to = [loki.write.local_loki.receiver]
}
// Anything that comes into this component gets written to the loki remote API
loki.write "local_loki" {
endpoint {
url = "http://loki:3100/loki/api/v1/push"
}
}
Alloy 语法通过使文件更易于阅读和编写来减少配置错误。它使用块、属性和表达式。您可以从文档中复制和粘贴块以快速入门。
Alloy 语法是声明性的,因此组件、块和属性的顺序无关紧要。组件之间的关系决定了管道的操作顺序。
块
使用 *块* 来配置组件和属性组。每个块可以包含属性或嵌套块。块代表管道中的步骤。
prometheus.remote_write "default" {
endpoint {
url = "https://:9009/api/prom/push"
}
}
前面的示例包含两个块
prometheus.remote_write "default"
:一个带标签的块,用于创建prometheus.remote_write
组件。标签是字符串"default"
。endpoint
:组件内部的一个无标签块,用于配置发送指标的端点。此块设置url
属性来指定端点。
属性
使用 *属性* 来配置单个设置。属性遵循格式 ATTRIBUTE_NAME = ATTRIBUTE_VALUE
。
以下示例将 log_level
属性设置为 "debug"
。
log_level = "debug"
表达式
使用表达式计算属性值。简单表达式包括常量,例如 "debug"
、32
或 [1, 2, 3, 4]
。Alloy 语法支持复杂表达式,例如
- 引用组件导出:
local.file.password_file.content
- 数学运算:
1 + 2
、3 * 4
、(5 * 6) + (7 + 8)
- 等式检查:
local.file.file_a.content == local.file.file_b.content
- 调用标准库中的函数:
sys.env("HOME")
检索HOME
环境变量。
您可以在组件定义中对任何属性使用表达式。
引用组件导出
最常见的表达式是引用组件的导出,例如 local.file.password_file.content
。通过组合组件名称(例如,local.file
)、标签(例如,password_file
)和导出名称(例如,content
),并用点分隔,来形成引用。
配置语法设计目标
Alloy 的特点是
- 快速:配置语言快速,控制器评估变更迅速。
- 简单:配置语言易于读写,降低学习曲线。
- 易于调试:配置语言提供详细的错误信息。
Alloy 配置语法是一种独特的语言,具有自定义语法和特性,例如一等函数。
- 块将相关设置分组,通常代表组件的创建。块的名称由用
.
分隔的标识符、一个可选的用户标签以及包含属性和嵌套块的主体组成。 - 属性出现在块内,并将值赋给名称。
- 表达式表示值,可以是字面量,也可以通过引用和组合其他值来表示。您可以使用表达式计算属性值。
工具
您可以使用以下工具编写 Alloy 配置文件
- 编辑器支持:
- 使用
alloy fmt
命令进行代码格式化