enhancement(prometheus_scrape source): Add scraping of Prometheus metrics over Unix sockets#25095
enhancement(prometheus_scrape source): Add scraping of Prometheus metrics over Unix sockets#25095weriomat wants to merge 4 commits intovectordotdev:masterfrom
Conversation
…r HTTP. To scrape Unix sockets, I adapted the approach taken with the `HttpClient`. It should support HTTPS, but I have not actively tested this. Furthermore, the ProxyConnector is supported on paper, but its usefulness is in doubt and the function is not tested. On the other hand, scraping via HTTP is tested and working. Sources using the `HttpClient` could be adapted to support scraping from Unix sockets as well. This is done for the Prometheus scrape source in this PR. This implements the Unix part of this [issue](vectordotdev#4554).
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3d1eb070e3
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| .map(|t| match t { | ||
| PrometheusEndpoint::Url(s) => s.parse::<Uri>().context(sources::UriParseSnafu), | ||
| #[cfg(unix)] | ||
| PrometheusEndpoint::Unix(u) => Ok(UnixUri::new(&u.path, &u.endpoint).into()).into(), |
There was a problem hiding this comment.
Avoid panicking when building Unix scrape URI
Constructing the Unix endpoint with UnixUri::new(&u.path, &u.endpoint) can panic for malformed or non-absolute endpoint values, which turns a user config error into a process crash during source build instead of returning a normal BuildError. This is reachable on Unix when endpoints = [{ path = "...", endpoint = "metrics" }] (missing leading slash), so startup robustness regresses for invalid configs.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
This is a limitation used by the library in this case. This can be circumvented by reimplementing this part of the library. Can be implemented if requested.
67185e2 to
c640bed
Compare
…rics over Unix sockets
To accommodate scraping over Unix sockets, I had to update how endpoints
are configured.
The implementation I propose is backward compatible and therefore not a
breaking change.
To scrape a Unix socket, you must configure the path to the Unix socket.
Additionally, you can configure the endpoint you scrape; the default
value is `/metrics`.
Arguably, I could implement parsing of the URI for `unix://` for the
`PrometheusEndpoint::Url` as well, but I have decided to separate Unix
sockets to provide a clearer distinction from the "normal" HttpClient.
The following is an example configuration.
```toml
[sources.unix]
endpoints = [{ path = "/var/run/forgejo/forgejo.sock" }, { path =
"/var/run/myservice/myservice.sock", endpoint = "/api/v1/metrics"}]
instance_tag = "instance_endpoint"
type = "prometheus_scrape"
```
The proposed implementation could be a solution for vectordotdev#17050 and is not in
line with [this
comment](vectordotdev#19012 (comment))
or with vectordotdev#19012. This is not a blocker, as it can be refactored later.
This commit implements a solution to vectordotdev#5794.
c640bed to
b2369d3
Compare
|
Do I have to push changes to |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b2369d3aab
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Summary
In this PR I plan to introduce the ability to scrape Prometheus metrics over Unix sockets. For this I have added a
UnixHttpClientcompatible with theHttpClientin its interface. Furthermore, I have adjusted the handling of endpoint URI's. Lastly the http_client is adjusted to handle URI's with theunixscheme via the newUnixHttpClient.For further information, check the descriptions of the commits themselves.
There is some debate on how the URI should be handled. The current solution is an untagged union with special case for the Unix socket. This is not in line with #19012 and should be refactored further in the future. Another solution for the meantime could be that the path to the socket is passed as a query parameter (like Postgres does it see 31.1.1.2. Connection URIs. See the overview I made here.
Furthermore, the
UnixHttpClientcould be used by other sources wanting to scrape Unix sockets as well.Vector configuration
How did you test this PR?
I have manually created dummy Prometheus metrics server running over a Unix socket and successfully scraped metrics. There were then written to a log file.
Change Type
Is this a breaking change?
Does this PR include user facing changes?
no-changeloglabel to this PR.References
Closes: #5794
Related: #19012
Related: #17050
Related: #4554
Notes
@vectordotdev/vectorto reach out to us regarding this PR.pre-pushhook, please see this template.make fmtmake check-clippy(if there are failures it's possible some of them can be fixed withmake clippy-fix)make testgit merge origin masterandgit push.Cargo.lock), pleaserun
make build-licensesto regenerate the license inventory and commit the changes (if any). More details on the dd-rust-license-tool.