Skip to content

Commit

Permalink
Fix/prometheusexporter Shutdown HTTP server (#35465)
Browse files Browse the repository at this point in the history
**Description:** Shutdown the http.Server instance on exporter shutdown

**Link to tracking Issue:** #35464 

**Testing:** Manual testing. I included this version of the exporter in
our internal distribution and deployed it to verify the metrics no
longer go stale after receiving a `NOHUP` signal.

**Documentation:** n/a

---------

Co-authored-by: David Ashpole <[email protected]>
  • Loading branch information
Argannor and dashpole authored Oct 30, 2024
1 parent c4e91e1 commit a30db62
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
27 changes: 27 additions & 0 deletions .chloggen/fix_prometheusexporter-shutdown-server.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: prometheusexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Fixes an issue where the prometheus exporter would not shut down the server when the collector was stopped."

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [35464]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
18 changes: 12 additions & 6 deletions exporter/prometheusexporter/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type prometheusExporter struct {
config Config
name string
endpoint string
shutdownFunc func() error
shutdownFunc func(ctx context.Context) error
handler http.Handler
collector *collector
registry *prometheus.Registry
Expand All @@ -44,7 +44,7 @@ func newPrometheusExporter(config *Config, set exporter.Settings) (*prometheusEx
endpoint: addr,
collector: collector,
registry: registry,
shutdownFunc: func() error { return nil },
shutdownFunc: func(_ context.Context) error { return nil },
handler: promhttp.HandlerFor(
registry,
promhttp.HandlerOpts{
Expand All @@ -63,11 +63,17 @@ func (pe *prometheusExporter) Start(ctx context.Context, host component.Host) er
return err
}

pe.shutdownFunc = ln.Close

mux := http.NewServeMux()
mux.Handle("/metrics", pe.handler)
srv, err := pe.config.ToServer(ctx, host, pe.settings, mux)
pe.shutdownFunc = func(ctx context.Context) error {
errLn := ln.Close()
if srv == nil {
return errLn
}
errSrv := srv.Shutdown(ctx)
return errors.Join(errLn, errSrv)
}
if err != nil {
return err
}
Expand All @@ -88,6 +94,6 @@ func (pe *prometheusExporter) ConsumeMetrics(_ context.Context, md pmetric.Metri
return nil
}

func (pe *prometheusExporter) Shutdown(context.Context) error {
return pe.shutdownFunc()
func (pe *prometheusExporter) Shutdown(ctx context.Context) error {
return pe.shutdownFunc(ctx)
}
25 changes: 0 additions & 25 deletions exporter/prometheusexporter/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,6 @@ func TestPrometheusExporter_WithTLS(t *testing.T) {

t.Cleanup(func() {
require.NoError(t, exp.Shutdown(context.Background()))
// trigger a get so that the server cleans up our keepalive socket
var resp *http.Response
resp, err = httpClient.Get("https://localhost:7777/metrics")
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
})

assert.NotNil(t, exp)
Expand Down Expand Up @@ -196,11 +191,6 @@ func TestPrometheusExporter_endToEndMultipleTargets(t *testing.T) {

t.Cleanup(func() {
require.NoError(t, exp.Shutdown(context.Background()))
// trigger a get so that the server cleans up our keepalive socket
var resp *http.Response
resp, err = http.Get("http://localhost:7777/metrics")
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
})

assert.NotNil(t, exp)
Expand Down Expand Up @@ -280,11 +270,6 @@ func TestPrometheusExporter_endToEnd(t *testing.T) {

t.Cleanup(func() {
require.NoError(t, exp.Shutdown(context.Background()))
// trigger a get so that the server cleans up our keepalive socket
var resp *http.Response
resp, err = http.Get("http://localhost:7777/metrics")
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
})

assert.NotNil(t, exp)
Expand Down Expand Up @@ -359,11 +344,6 @@ func TestPrometheusExporter_endToEndWithTimestamps(t *testing.T) {

t.Cleanup(func() {
require.NoError(t, exp.Shutdown(context.Background()))
// trigger a get so that the server cleans up our keepalive socket
var resp *http.Response
resp, err = http.Get("http://localhost:7777/metrics")
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
})

assert.NotNil(t, exp)
Expand Down Expand Up @@ -441,11 +421,6 @@ func TestPrometheusExporter_endToEndWithResource(t *testing.T) {

t.Cleanup(func() {
require.NoError(t, exp.Shutdown(context.Background()))
// trigger a get so that the server cleans up our keepalive socket
var resp *http.Response
resp, err = http.Get("http://localhost:7777/metrics")
require.NoError(t, err, "Failed to perform a scrape")
require.NoError(t, resp.Body.Close())
})

assert.NotNil(t, exp)
Expand Down

0 comments on commit a30db62

Please sign in to comment.