Skip to content

Commit

Permalink
[exporter/prometheus] close server on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Argannor authored and Soeren-S committed Sep 27, 2024
1 parent 507ec47 commit 40d9f16
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 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,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
}
Expand All @@ -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)
}

0 comments on commit 40d9f16

Please sign in to comment.