Repository navigation
[FUNK-2635] Expose reusable prometheus integration - #242
Ashwithabg wants to merge 3 commits into
Conversation
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Review SummaryAutomated review (bug-hunter, language-pattern-reviewer, test-analyzer). No bugs found: the Recommendation: COMMENT (this line is a reading aid; no review status was set) 🟠 High Findings (4)Caller-supplied
|
Ashwithabg
left a comment
There was a problem hiding this comment.
Inline comments from review-toolkit agents.
Add Done and Err so a server that dies in the background is observable, default WriteTimeout and IdleTimeout, close open connections when Shutdown's context expires, and document that the engine writes are not synchronized. Tests cover a caller-supplied Handler, MetricTimeout, the default engine, repeated calls, Done/Err, a serve failure and a forced close. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
5 open issues
Shutdowncurrently returns the error fromhttp.Server.Shutdown(ctx)whenever it’s non-nil,… · New Tests usehttp.Getwithout a client timeout; if something goes wrong (server not responding,… · New Theio.ReadAllerror is ignored. If a read fails (e.g., truncated response), the test may… · New Tests usehttp.Getwithout a client timeout; if something goes wrong (server not responding,… · New This fixed sleep is used to ensure the in-flight request is active, which can be flaky on… · New
What changed in this PR
Adds a reusable prometheus.StartExporter helper to expose /metrics on a dedicated listener while tagging the stats engine with an owning-service label, reducing duplicated setup across services.
Changes:
- Introduces
ExporterConfig/ExporterandStartExporterto bind a listener, register a handler on a stats engine, and serve/metricswith graceful shutdown. - Adds Go tests covering tagging behavior, error cases, handler interplay, shutdown semantics, and connection closing on shutdown timeout.
- Updates README and HISTORY with usage and release notes.
| File | Description |
|---|---|
| prometheus/exporter.go | Implements StartExporter, Exporter, and config-driven HTTP server setup + shutdown semantics |
| prometheus/exporter_test.go | Adds test coverage for exporter behavior (tags, handlers, timeouts, failures, shutdown) |
| README.md | Documents StartExporter usage and ordering constraints with derived engines |
| HISTORY.md | Adds Unreleased entry describing the new API and behavior |
Review effort: Lite
| err := e.server.Shutdown(ctx) | ||
| if err != nil { | ||
| e.server.Close() // ctx expired with scrapes in flight: do not leave them open | ||
| } | ||
| <-e.done | ||
| if err == nil { | ||
| err = e.err | ||
| } | ||
| return err |
| engine.Add("requests", 3) | ||
| engine.Flush() | ||
|
|
||
| res, err := http.Get("http://" + e.Addr().String() + "/metrics") |
| t.Fatal(err) | ||
| } | ||
| defer res.Body.Close() | ||
| body, _ := io.ReadAll(res.Body) |
| func scrapeBody(t *testing.T, e *Exporter) string { | ||
| t.Helper() | ||
|
|
||
| res, err := http.Get("http://" + e.Addr().String() + "/metrics") |
| // Hold a request open: send the headers of a request but never finish it, so it stays active. | ||
| conn, err := net.Dial("tcp", e.Addr().String()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer conn.Close() | ||
| if _, err := conn.Write([]byte("GET /metrics HTTP/1.1\r\nHost: x\r\n")); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| time.Sleep(50 * time.Millisecond) | ||
|
|


Description:
What
Adds
prometheus.StartExporter, which wires a stats engine to a Prometheus/metricsendpoint in one call: