From 40d9f16c44cd61c61610e5c88532e5c9a237c28c Mon Sep 17 00:00:00 2001 From: Argannor Date: Fri, 27 Sep 2024 15:39:16 +0200 Subject: [PATCH] [exporter/prometheus] close server on shutdown --- exporter/prometheusexporter/prometheus.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/exporter/prometheusexporter/prometheus.go b/exporter/prometheusexporter/prometheus.go index 893653d45326..9dca9f748c38 100644 --- a/exporter/prometheusexporter/prometheus.go +++ b/exporter/prometheusexporter/prometheus.go @@ -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 @@ -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{ @@ -63,11 +63,14 @@ 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 { + err := ln.Close() + err2 := srv.Shutdown(ctx) + return errors.Join(err, err2) + } if err != nil { return err } @@ -88,6 +91,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) }