-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prototype targets.merge function (array.combine_maps) #1826
Changes from 19 commits
fc92214
f39107a
a60e189
416905d
7d6d624
96798db
12e1b68
67cb461
24d6fb0
7a1d450
5546a78
a18eefa
f6c3fdf
9af4284
f6941f9
7a337e9
5279bb4
29f673c
55bea93
f401abc
137c132
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,50 @@ Elements within the list can be any type. | |
> array.concat([[1, 2], [3, 4]], [[5, 6]]) | ||
[[1, 2], [3, 4], [5, 6]] | ||
``` | ||
|
||
## array.combine_maps | ||
|
||
{{< docs/shared lookup="stability/experimental.md" source="alloy" version="<ALLOY_VERSION>" >}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we create a separate shared markdown for "feature" and leave the "component" alone? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to avoid having two almost identical shared paragraphs but I'm ok to have both if "feature" is too vague for "component". @clayton-cornell what do you think? |
||
|
||
The `array.combine_maps` function allows you to join two arrays of maps if certain keys have matching values in both maps. It's particularly useful when combining labels of targets coming from different `prometheus.discovery.*` or `prometheus.exporter.*` components. | ||
It takes three arguments: | ||
|
||
* The first two arguments are a of type `list(map(string))`. The keys of the map are strings. | ||
The value for each key could be of any Alloy type such as a `string`, `integer`, `map`, or a `capsule`. | ||
* The third input is an `array` containing strings. The strings are the keys whose value has to match for maps to be combined. | ||
|
||
The maps that don't contain all the keys provided in the third argument will be discarded. When maps are combined and both contain the same keys, the last value from the second argument will be used. | ||
|
||
Pseudo function code: | ||
``` | ||
for every map in arg1: | ||
for every map in arg2: | ||
if the condition key matches in both: | ||
merge maps and add to result | ||
``` | ||
|
||
### Examples | ||
thampiotr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```alloy | ||
> 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"}] | ||
``` | ||
|
||
Examples using discovery and exporter components: | ||
```alloy | ||
> 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"]) | ||
``` | ||
|
||
You can find more examples in the [tests][]. | ||
|
||
[tests]: https://github.com/grafana/alloy/blob/main/syntax/vm/vm_stdlib_test.go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm adding it directly to the rc changelog to avoid another PR to main to switch it when we patch it