Skip to content

flags: respect explicit --listen when metrics-endpoint is set#332

Open
gecube wants to merge 1 commit into
coroot:mainfrom
gecube:fix/respect-listen-with-metrics-endpoint
Open

flags: respect explicit --listen when metrics-endpoint is set#332
gecube wants to merge 1 commit into
coroot:mainfrom
gecube:fix/respect-listen-with-metrics-endpoint

Conversation

@gecube

@gecube gecube commented Jul 6, 2026

Copy link
Copy Markdown

Problem

Fixes #258.

When a metrics endpoint is configured — either directly via --metrics-endpoint / METRICS_ENDPOINT, or derived from --collector-endpointflags/init() unconditionally overwrites the listen address:

if *MetricsEndpoint != nil {
    *ListenAddress = "127.0.0.1:10300"
}

This binds the built-in HTTP server to loopback and silently discards any user-provided --listen / LISTEN. As a result you cannot scrape /metrics directly from the agent while it is also remote-writing, even if you explicitly ask for a routable listen address.

Change

Only apply the loopback default when the listen address is still at its compiled-in default:

if *MetricsEndpoint != nil && *ListenAddress == defaultListenAddress {
    *ListenAddress = "127.0.0.1:10300"
}
  • No --listen set → unchanged behavior: loopback (127.0.0.1:10300) in remote-write mode.
  • --listen / LISTEN set explicitly → honored, so /metrics can be scraped alongside remote write.

Why this is safe

*flags.ListenAddress is consumed only by the HTTP server (main.go, and the Windows agent). The remote-write path gathers metrics in-process via registry.Gather() (prom/remote_writer.go) — it does not scrape its own socket — so honoring --listen cannot affect the remote-write pipeline. On Windows defaultListenAddress is already 127.0.0.1:10300, so the default-mode behavior there is unchanged too, and an explicit --listen is now honored on Windows as well.

Builds cleanly for GOOS=linux and GOOS=windows; go vet ./flags/ is clean.

When a metrics endpoint is configured (directly or derived from
--collector-endpoint), the listen address was unconditionally
overwritten to 127.0.0.1:10300, which made it impossible to scrape
/metrics from the agent while it also remote-writes metrics.

Only apply the loopback default when the listen address is still at its
compiled-in default, so an explicitly configured --listen / LISTEN is
honored. This keeps the previous behavior (loopback in remote-write
mode) for everyone who does not set --listen.

Fixes coroot#258

Signed-off-by: Gaál György <gb12335@gmail.com>
@gecube

gecube commented Jul 6, 2026

Copy link
Copy Markdown
Author

Local build & test report

Built and tested in a Debian bullseye container (matching the project Dockerfile) on an arm64 Linux VM (OrbStack), Go 1.24.9, CGO_ENABLED=1, -mod=readonly.

Build

$ go build .
BUILD_OK

Vet

$ go vet ./...
(clean, exit 0)

Tests

$ go test ./...
?   github.com/coroot/coroot-node-agent               [no test files]
ok  github.com/coroot/coroot-node-agent/cgroup        0.013s
ok  github.com/coroot/coroot-node-agent/common        0.002s
ok  github.com/coroot/coroot-node-agent/ebpftracer/l7 0.003s
ok  github.com/coroot/coroot-node-agent/logs          2.416s
ok  github.com/coroot/coroot-node-agent/node          0.003s
ok  github.com/coroot/coroot-node-agent/proc          0.003s
(remaining packages have no test files)
exit 0

All packages build and pass; go vet is clean. Cross-compiles cleanly for GOOS=windows as well.

Note: the flags package has no unit tests, and flags.init() early-returns under go test (the strings.HasSuffix(os.Args[0], ".test") guard), so the resolution logic isn't exercised by the suite. Happy to extract the listen-address resolution into a small testable helper and add a focused unit test if you'd prefer that over the minimal in-place change.

@def def added the AI slop label Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: allow to scrape metrics from node-agent

2 participants