array
array
命名空间包含与数组相关的函数。
array.concat
array.concat
函数将一个或多个值列表连接成一个单个列表。array.concat
的每个参数都必须是列表值。列表中的元素可以是任何类型。
示例
> array.concat([])
[]
> array.concat([1, 2], [3, 4])
[1, 2, 3, 4]
> array.concat([1, 2], [], [bool, null])
[1, 2, bool, null]
> array.concat([[1, 2], [3, 4]], [[5, 6]])
[[1, 2], [3, 4], [5, 6]]
array.combine_maps
实验性功能: 这是一个实验性功能。实验性功能经常会发生破坏性更改,并且可能被移除而无等效替代。必须将
stability.level
标志设置为experimental
才能使用此功能。
array.combine_maps
函数允许您在两个映射数组中合并具有匹配特定键值的映射。当合并来自不同 prometheus.discovery.*
或 prometheus.exporter.*
组件的目标标签时,它特别有用。它接受三个参数
- 前两个参数类型为
list(map(string))
。映射的键是字符串。每个键的值可以是任何 Alloy 类型,例如string
、integer
、map
或capsule
。 - 第三个输入是一个包含字符串的
array
。这些字符串是合并映射时值必须匹配的键。
不包含第三个参数中提供的所有键的映射将被丢弃。当合并映射并且两者包含相同的键时,将使用来自第二个参数的最后一个值。
伪函数代码
for every map in arg1:
for every map in arg2:
if the condition key matches in both:
merge maps and add to result
示例
> array.combine_maps([{"instance"="1.1.1.1", "team"="A"}], [{"instance"="1.1.1.1", "cluster"="prod"}], ["instance"])
[{"instance"="1.1.1.1", "team"="A", "cluster"="prod"}]
// Second map overrides the team in the first map
> array.combine_maps([{"instance"="1.1.1.1", "team"="A"}], [{"instance"="1.1.1.1", "team"="B"}], ["instance"])
[{"instance"="1.1.1.1", "team"="B"}]
// If multiple maps from the first argument match with multiple maps from the second argument, different combinations will be created.
> array.combine_maps([{"instance"="1.1.1.1", "team"="A"}, {"instance"="1.1.1.1", "team"="B"}], [{"instance"="1.1.1.1", "cluster"="prod"}, {"instance"="1.1.1.1", "cluster"="ops"}], ["instance"])
[{"instance"="1.1.1.1", "team"="A", "cluster"="prod"}, {"instance"="1.1.1.1", "team"="A", "cluster"="ops"}, {"instance"="1.1.1.1", "team"="B", "cluster"="prod"}, {"instance"="1.1.1.1", "team"="B", "cluster"="ops"}]
使用发现和导出器组件的示例
> array.combine_maps(discovery.kubernetes.k8s_pods.targets, prometheus.exporter.postgres, ["instance"])
> array.combine_maps(prometheus.exporter.redis.default.targets, [{"instance"="1.1.1.1", "testLabelKey" = "testLabelVal"}], ["instance"])
您可以在测试中找到更多示例。