From 6d528f29223554a207cfbad1b43ab887df5d148f Mon Sep 17 00:00:00 2001 From: Stephen Muth Date: Mon, 3 Mar 2025 10:04:10 -0500 Subject: [PATCH] feat(outputs.prometheus_client): Allow adding custom headers (#16566) Co-authored-by: Stephen Muth --- plugins/outputs/prometheus_client/README.md | 3 +++ .../prometheus_client/prometheus_client.go | 17 +++++++++++++++-- plugins/outputs/prometheus_client/sample.conf | 3 +++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/plugins/outputs/prometheus_client/README.md b/plugins/outputs/prometheus_client/README.md index 0a9c75a22..013940df0 100644 --- a/plugins/outputs/prometheus_client/README.md +++ b/plugins/outputs/prometheus_client/README.md @@ -81,6 +81,9 @@ to use them. ## Export metric collection time. # export_timestamp = false + ## Set custom headers for HTTP responses. + # http_headers = {"X-Special-Header" = "Special-Value"} + ## Specify the metric type explicitly. ## This overrides the metric-type of the Telegraf metric. Globbing is allowed. # [outputs.prometheus_client.metric_types] diff --git a/plugins/outputs/prometheus_client/prometheus_client.go b/plugins/outputs/prometheus_client/prometheus_client.go index fd9fe3421..8c4069c24 100644 --- a/plugins/outputs/prometheus_client/prometheus_client.go +++ b/plugins/outputs/prometheus_client/prometheus_client.go @@ -60,6 +60,7 @@ type PrometheusClient struct { StringAsLabel bool `toml:"string_as_label"` ExportTimestamp bool `toml:"export_timestamp"` TypeMappings serializers_prometheus.MetricTypes `toml:"metric_types"` + HTTPHeaders map[string]*config.Secret `toml:"http_headers"` Log telegraf.Logger `toml:"-"` common_tls.ServerConfig @@ -164,8 +165,8 @@ func (p *PrometheusClient) Init() error { if p.Path == "" { p.Path = "/metrics" } - mux.Handle(p.Path, authHandler(rangeHandler(promHandler))) - mux.Handle("/", authHandler(rangeHandler(landingPageHandler))) + mux.Handle(p.Path, p.headerHandler(authHandler(rangeHandler(promHandler)))) + mux.Handle("/", p.headerHandler(authHandler(rangeHandler(landingPageHandler)))) tlsConfig, err := p.TLSConfig() if err != nil { @@ -256,6 +257,18 @@ func (p *PrometheusClient) Connect() error { return nil } +func (p *PrometheusClient) headerHandler(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + for key, secret := range p.HTTPHeaders { + value, err := secret.Get() + if err == nil { + w.Header().Set(key, value.String()) + } + } + next.ServeHTTP(w, r) + }) +} + func onAuthError(_ http.ResponseWriter) { } diff --git a/plugins/outputs/prometheus_client/sample.conf b/plugins/outputs/prometheus_client/sample.conf index 7055f1bac..5e1a7e2a6 100644 --- a/plugins/outputs/prometheus_client/sample.conf +++ b/plugins/outputs/prometheus_client/sample.conf @@ -49,6 +49,9 @@ ## Export metric collection time. # export_timestamp = false + ## Set custom headers for HTTP responses. + # http_headers = {"X-Special-Header" = "Special-Value"} + ## Specify the metric type explicitly. ## This overrides the metric-type of the Telegraf metric. Globbing is allowed. # [outputs.prometheus_client.metric_types]