Skip to content
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

Merged
merged 21 commits into from
Nov 11, 2024

Conversation

ptodev
Copy link
Collaborator

@ptodev ptodev commented Oct 4, 2024

I hope this can enable users to decorate prometheus.exporter metrics with labels from discovery components.

The config could look like this:

discovery.kubernetes "default" {
  role = "service"
}

prometheus.exporter.unix "default" {}

// Probably we'd need some discovery.relabel to make sure both targets have a common key/val to serve as condition of joining

prometheus.scrape "demo" {
  targets    = targets.merge(prometheus.exporter.unix.default.targets, discovery.kubernetes.default.targets, ["__meta_kubernetes_service_cluster_ip"])
  forward_to = [prometheus.remote_write.mimir.receiver]
}

prometheus.remote_write "mimir" {
  endpoint {
    url = "https://prometheus-prod-05-gb-south-0.grafana.net/api/prom/push"

    basic_auth {
      username = ""
      password = ""
    }
  }
}

Which issue(s) this PR fixes

Fixes #1443

docs/sources/reference/stdlib/map.md Outdated Show resolved Hide resolved
docs/sources/reference/stdlib/map.md Outdated Show resolved Hide resolved
@mattdurham
Copy link
Collaborator

I wonder on the naming, the name makes me feel like its very SQL like and should behave like a SQL inner join.

@ptodev ptodev force-pushed the ptodev/inner-join-func branch from 64767f2 to b248d1c Compare October 9, 2024 09:47
@ptodev ptodev changed the title Prototype map.inner_join function Prototype targets.merge function Oct 9, 2024
@ptodev
Copy link
Collaborator Author

ptodev commented Oct 9, 2024

I wonder on the naming, the name makes me feel like its very SQL like and should behave like a SQL inner join.

@mattdurham Good point - I renamed it from "inner_join" to "merge". I also changed the namespace from "map" to "targets", because I realised that even "map" is not an appropriate namespace for this function as it operates on arrays of maps.

@mattdurham
Copy link
Collaborator

Overall looks good! Though the complexity of this feels like it borders on a component, which means we could use the live debugging and mark as experimental. Since this has become specific to targets thoughts to add it to relabel.discovery?

@ptodev ptodev force-pushed the ptodev/inner-join-func branch from b248d1c to 2c5dea2 Compare October 17, 2024 12:43
@ptodev
Copy link
Collaborator Author

ptodev commented Oct 31, 2024

Overall looks good! Though the complexity of this feels like it borders on a component, which means we could use the live debugging and mark as experimental. Since this has become specific to targets thoughts to add it to relabel.discovery?

@mattdurham The main reasons for having it as an std lib function are:

  • It is technically not specific to targets. You could have any lists of maps and the function could still work. I namespaced it as "targets", just because I couldn't think of a better name.
  • It's probably more performant as stdlib than as a component. This is just a guess and I'm not sure what the benchmarks would be in reality.

I do agree that there would be benefits to the component approach:

  • Works with the UI - not just live debugging, but the actual UI that shows inputs.
  • We can gate it behind the "experimental" cmd flag.
  • It's easier for people who use discovery components to find it.

We could do a few things to make the std function a more compelling option:

  • Rename this function to something that doesn't include "targets" in the name. For example, map.merge()? Technically, the inputs are not maps because we're merging arrays of maps... but that's probably fine.
  • We'd have to rename the objects type to "maps". I asked the team about this a while back, and no one objected. It is already documented as "map" in the component reference docs.
  • I could add examples of how to use the "targets.merge"/"map.merge" function with non-discovery functionality. For example, we could get two maps from encoding.from_json and merge them.
  • It just dawned on me that we can also do filtering with this too. E.g. map.merge(arr_w_maps, [{"val" = "a"}], ["val"]) will only pass through the maps in arr_w_maps which have "val" = "a". It's a bit weird that you can do filtering with a function called merge, but I can't think of a better alternative right now 😄 I could include an example for that too.

In the long term, we could also:

  • Make the UI work better with std lib functions.
  • Make it possible to gate std lib features behind an "experimental" command line argument.

cc @thampiotr he made the original suggestion to use std lib.

@ptodev ptodev force-pushed the ptodev/inner-join-func branch from 2c5dea2 to 0ef415c Compare October 31, 2024 19:18
@ptodev
Copy link
Collaborator Author

ptodev commented Oct 31, 2024

Maybe map.combine might be better than map.merge, since the function makes a new map for each combination of LHS map and a RHS map.

Copy link
Contributor

@thampiotr thampiotr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there could be a way to get live debugging for stdlib functions?

docs/sources/reference/stdlib/targets.md Outdated Show resolved Hide resolved
docs/sources/reference/stdlib/targets.md Outdated Show resolved Hide resolved
@wildum
Copy link
Contributor

wildum commented Nov 6, 2024

I wonder if there could be a way to get live debugging for stdlib functions?

In the Alloy UI, users can see the arguments and exports so they should already have the possibility to see the inputs and the output of the merge function. It's not live but seeing the latest values should be good enough.

Live debugging is activated per component (you click on a button in the component page to open the stream of data). I'm not sure how it would look like in the UI to have it for a function that's inside a component.

Implementation wise it's also not trivial. The functions are defined in the syntax pkg which should not depend on a particular Alloy feature. We would need some kind of wrapper that plugs the live debugging and runs because it needs to be able to enable/disable the stream depending on user input

syntax/internal/stdlib/stdlib.go Outdated Show resolved Hide resolved
syntax/internal/stdlib/stdlib.go Outdated Show resolved Hide resolved
syntax/internal/stdlib/stdlib.go Outdated Show resolved Hide resolved
syntax/internal/stdlib/stdlib.go Outdated Show resolved Hide resolved
syntax/internal/stdlib/stdlib.go Outdated Show resolved Hide resolved
syntax/internal/stdlib/stdlib.go Outdated Show resolved Hide resolved
syntax/vm/vm_stdlib_test.go Show resolved Hide resolved
@wildum wildum force-pushed the ptodev/inner-join-func branch from 955fc04 to 96798db Compare November 7, 2024 15:54
@@ -44,6 +45,8 @@ v1.5.0-rc.0
- (_Experimental_) Add a `prometheus.write.queue` component to add an alternative to `prometheus.remote_write`
which allowing the writing of metrics to a prometheus endpoint. (@mattdurham)

- (_Experimental_) Add the function `arrary.combine_maps` to the stdlib. (@ptodev, @wildum)

Copy link
Contributor

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

@thampiotr thampiotr changed the title Prototype targets.merge function Prototype targets.merge function (array.combine_maps) Nov 8, 2024
Copy link
Contributor

@thampiotr thampiotr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Left some comments and would like to take it for a spin. With some more tests and docs I think we can land this.

CHANGELOG.md Outdated Show resolved Hide resolved
docs/sources/reference/stdlib/array.md Outdated Show resolved Hide resolved
docs/sources/reference/stdlib/array.md Outdated Show resolved Hide resolved
docs/sources/reference/stdlib/array.md Outdated Show resolved Hide resolved
docs/sources/reference/stdlib/array.md Show resolved Hide resolved
syntax/internal/stdlib/stdlib.go Outdated Show resolved Hide resolved
syntax/internal/stdlib/stdlib.go Outdated Show resolved Hide resolved
Comment on lines 227 to 228
case TypeNull:
return 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this have some weird side-effects, like Len() called on null capsule or sth?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this. Looking at the code, I don't think that this can be triggered because the type is checked before every call to Len. Maybe Paulin made this change before checking the types and then forgot about it or I'm missing something

syntax/internal/value/value.go Show resolved Hide resolved
syntax/vm/vm_stdlib_test.go Show resolved Hide resolved
wildum and others added 11 commits November 8, 2024 16:18
Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>
Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>
Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>
Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>
Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>
Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>
@wildum wildum force-pushed the ptodev/inner-join-func branch from 47d088d to 5279bb4 Compare November 8, 2024 18:03
@wildum wildum marked this pull request as ready for review November 11, 2024 11:28
@wildum wildum requested review from clayton-cornell and a team as code owners November 11, 2024 11:28
@wildum wildum merged commit f3108e7 into main Nov 11, 2024
18 checks passed
@wildum wildum deleted the ptodev/inner-join-func branch November 11, 2024 11:35
thampiotr added a commit that referenced this pull request Nov 11, 2024
* Prototype map.inner_join function

* Rename to "targets.merge", remove unnecessary params

* Add failure tests

* Delete incorrect comment

* rename targets.merge to array.combine_maps, mark it as experimental, make is more permissive and add tests

* update changelog

* Update CHANGELOG.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update docs/sources/reference/stdlib/array.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update docs/sources/reference/stdlib/array.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update docs/sources/reference/stdlib/array.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* rename mapCombine to combineMaps

* document panic

* add equal test

* add more tests

* Update syntax/internal/stdlib/stdlib.go

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update syntax/internal/stdlib/stdlib.go

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* add examples in doc

* fix error propagation

* remove value nul on len function

* refactor code into a traversal function

* update doc to avoid modifying the experimental shared doc

---------

Co-authored-by: William Dumont <william.dumont@grafana.com>
Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>
thampiotr added a commit that referenced this pull request Nov 11, 2024
* update changelog version

* Prototype targets.merge function (array.combine_maps) (#1826)

* Prototype map.inner_join function

* Rename to "targets.merge", remove unnecessary params

* Add failure tests

* Delete incorrect comment

* rename targets.merge to array.combine_maps, mark it as experimental, make is more permissive and add tests

* update changelog

* Update CHANGELOG.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update docs/sources/reference/stdlib/array.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update docs/sources/reference/stdlib/array.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update docs/sources/reference/stdlib/array.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* rename mapCombine to combineMaps

* document panic

* add equal test

* add more tests

* Update syntax/internal/stdlib/stdlib.go

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update syntax/internal/stdlib/stdlib.go

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* add examples in doc

* fix error propagation

* remove value nul on len function

* refactor code into a traversal function

* update doc to avoid modifying the experimental shared doc

---------

Co-authored-by: William Dumont <william.dumont@grafana.com>
Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Implement an initial version of the support bundle in Alloy (#2009)

* Implement an initial version of the support bundle in Alloy

* Add documentation for support bundle

* Update changelog

* Update docs/sources/troubleshoot/support_bundle.md

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>

* Update docs/sources/troubleshoot/support_bundle.md

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>

* Update docs/sources/troubleshoot/support_bundle.md

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>

* Update docs/sources/troubleshoot/support_bundle.md

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>

* Initial PR feedback

* Rewrite http service to use logging library internal to alloy

* Revert accidental commit of e2e test changes

* Fix comment on exported function

* Clean up added host variable that is no longer used

* Refactor usage of logger in http service

* Update internal/service/http/http.go

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* implement PR feedback

* Hide support bundle behind public preview stability level

* Update docs based on feedback

* Update docs/sources/troubleshoot/support_bundle.md

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>

* Update docs/sources/troubleshoot/support_bundle.md

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>

* Update docs/sources/troubleshoot/support_bundle.md

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>

* Update docs/sources/troubleshoot/support_bundle.md

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>

* Update docs/sources/troubleshoot/support_bundle.md

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>

* Update docs/sources/troubleshoot/support_bundle.md

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>

* More PR feedback in docs

* Fix race condition in logger

* Add a note about backward-compatibility exception

---------

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>
Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

---------

Co-authored-by: Paulin Todev <paulin.todev@gmail.com>
Co-authored-by: William Dumont <william.dumont@grafana.com>
Co-authored-by: Sam DeHaan <dehaansa@gmail.com>
Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>
vaxvms pushed a commit to vaxvms/alloy that referenced this pull request Nov 15, 2024
* Prototype map.inner_join function

* Rename to "targets.merge", remove unnecessary params

* Add failure tests

* Delete incorrect comment

* rename targets.merge to array.combine_maps, mark it as experimental, make is more permissive and add tests

* update changelog

* Update CHANGELOG.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update docs/sources/reference/stdlib/array.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update docs/sources/reference/stdlib/array.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update docs/sources/reference/stdlib/array.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* rename mapCombine to combineMaps

* document panic

* add equal test

* add more tests

* Update syntax/internal/stdlib/stdlib.go

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update syntax/internal/stdlib/stdlib.go

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* add examples in doc

* fix error propagation

* remove value nul on len function

* refactor code into a traversal function

* update doc to avoid modifying the experimental shared doc

---------

Co-authored-by: William Dumont <william.dumont@grafana.com>
Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>
vaxvms pushed a commit to vaxvms/alloy that referenced this pull request Nov 20, 2024
* Prototype map.inner_join function

* Rename to "targets.merge", remove unnecessary params

* Add failure tests

* Delete incorrect comment

* rename targets.merge to array.combine_maps, mark it as experimental, make is more permissive and add tests

* update changelog

* Update CHANGELOG.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update docs/sources/reference/stdlib/array.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update docs/sources/reference/stdlib/array.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update docs/sources/reference/stdlib/array.md

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* rename mapCombine to combineMaps

* document panic

* add equal test

* add more tests

* Update syntax/internal/stdlib/stdlib.go

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* Update syntax/internal/stdlib/stdlib.go

Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>

* add examples in doc

* fix error propagation

* remove value nul on len function

* refactor code into a traversal function

* update doc to avoid modifying the experimental shared doc

---------

Co-authored-by: William Dumont <william.dumont@grafana.com>
Co-authored-by: Piotr <17101802+thampiotr@users.noreply.github.com>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integrate prometheus.exporter and discovery components
4 participants