Skip to content

enhancement(prometheus_scrape source): Add scraping of Prometheus metrics over Unix sockets#25095

Open
weriomat wants to merge 4 commits intovectordotdev:masterfrom
weriomat:prometheus_unix
Open

enhancement(prometheus_scrape source): Add scraping of Prometheus metrics over Unix sockets#25095
weriomat wants to merge 4 commits intovectordotdev:masterfrom
weriomat:prometheus_unix

Conversation

@weriomat
Copy link
Copy Markdown
Contributor

@weriomat weriomat commented Apr 1, 2026

Summary

In this PR I plan to introduce the ability to scrape Prometheus metrics over Unix sockets. For this I have added a UnixHttpClient compatible with the HttpClient in its interface. Furthermore, I have adjusted the handling of endpoint URI's. Lastly the http_client is adjusted to handle URI's with the unix scheme via the new UnixHttpClient.
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 UnixHttpClient could be used by other sources wanting to scrape Unix sockets as well.

Vector configuration

[sources.unix]
endpoints = [{ path = "./test.sock" }] # this is a full path but irrelevant here
instance_tag = "instance_endpoint"
type = "prometheus_scrape"

[sources.tcp]
endpoints = ["http://127.0.0.1:9898"]
instance_tag = "instance_endpoint"
type = "prometheus_scrape"

[api]
enabled = true

[sinks.file]
type = "file"
inputs = ["unix", "tcp"]
path = "./out.log"
encoding.codec = "json"

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

  • Bug fix
  • New feature
  • Dependencies
  • Non-functional (chore, refactoring, docs)
  • Performance

Is this a breaking change?

  • Yes
  • No

Does this PR include user facing changes?

  • Yes. Please add a changelog fragment based on our guidelines.
  • No. A maintainer will apply the no-changelog label to this PR.

References

Closes: #5794

Related: #19012
Related: #17050
Related: #4554

Notes

  • Please read our Vector contributor resources.
  • Do not hesitate to use @vectordotdev/vector to reach out to us regarding this PR.
  • Some CI checks run only after we manually approve them.
    • We recommend adding a pre-push hook, please see this template.
    • Alternatively, we recommend running the following locally before pushing to the remote branch:
      • make fmt
      • make check-clippy (if there are failures it's possible some of them can be fixed with make clippy-fix)
      • make test
  • After a review is requested, please avoid force pushes to help us review incrementally.
    • Feel free to push as many commits as you want. They will be squashed into one before merging.
    • For example, you can run git merge origin master and git push.
  • If this PR introduces changes Vector dependencies (modifies Cargo.lock), please
    run make build-licenses to regenerate the license inventory and commit the changes (if any). More details on the dd-rust-license-tool.

…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).
@weriomat weriomat requested a review from a team as a code owner April 1, 2026 16:27
@github-actions github-actions bot added the domain: sources Anything related to the Vector's sources label Apr 1, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 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(),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@weriomat weriomat changed the title enhancement(prometheus/scrape): Add scraping of Prometheus metrics over Unix sockets enhancement(prometheus_scrape source): Add scraping of Prometheus metrics over Unix sockets Apr 1, 2026
weriomat added 3 commits April 1, 2026 22:26
…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.
@weriomat
Copy link
Copy Markdown
Contributor Author

weriomat commented Apr 1, 2026

Do I have to push changes to website/cue/references/components/sources/generated/prometheus_scrape.cue?

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

domain: sources Anything related to the Vector's sources

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support unix socket for prometheus_scrape

1 participant