feat(outputs.prometheus_client): Allow adding custom headers (#16566)

Co-authored-by: Stephen Muth <smuth@ariasystems.com>
This commit is contained in:
Stephen Muth 2025-03-03 10:04:10 -05:00 committed by GitHub
parent 17298066b2
commit 6d528f2922
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 2 deletions

View File

@ -81,6 +81,9 @@ to use them.
## Export metric collection time. ## Export metric collection time.
# export_timestamp = false # export_timestamp = false
## Set custom headers for HTTP responses.
# http_headers = {"X-Special-Header" = "Special-Value"}
## Specify the metric type explicitly. ## Specify the metric type explicitly.
## This overrides the metric-type of the Telegraf metric. Globbing is allowed. ## This overrides the metric-type of the Telegraf metric. Globbing is allowed.
# [outputs.prometheus_client.metric_types] # [outputs.prometheus_client.metric_types]

View File

@ -60,6 +60,7 @@ type PrometheusClient struct {
StringAsLabel bool `toml:"string_as_label"` StringAsLabel bool `toml:"string_as_label"`
ExportTimestamp bool `toml:"export_timestamp"` ExportTimestamp bool `toml:"export_timestamp"`
TypeMappings serializers_prometheus.MetricTypes `toml:"metric_types"` TypeMappings serializers_prometheus.MetricTypes `toml:"metric_types"`
HTTPHeaders map[string]*config.Secret `toml:"http_headers"`
Log telegraf.Logger `toml:"-"` Log telegraf.Logger `toml:"-"`
common_tls.ServerConfig common_tls.ServerConfig
@ -164,8 +165,8 @@ func (p *PrometheusClient) Init() error {
if p.Path == "" { if p.Path == "" {
p.Path = "/metrics" p.Path = "/metrics"
} }
mux.Handle(p.Path, authHandler(rangeHandler(promHandler))) mux.Handle(p.Path, p.headerHandler(authHandler(rangeHandler(promHandler))))
mux.Handle("/", authHandler(rangeHandler(landingPageHandler))) mux.Handle("/", p.headerHandler(authHandler(rangeHandler(landingPageHandler))))
tlsConfig, err := p.TLSConfig() tlsConfig, err := p.TLSConfig()
if err != nil { if err != nil {
@ -256,6 +257,18 @@ func (p *PrometheusClient) Connect() error {
return nil 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) { func onAuthError(_ http.ResponseWriter) {
} }

View File

@ -49,6 +49,9 @@
## Export metric collection time. ## Export metric collection time.
# export_timestamp = false # export_timestamp = false
## Set custom headers for HTTP responses.
# http_headers = {"X-Special-Header" = "Special-Value"}
## Specify the metric type explicitly. ## Specify the metric type explicitly.
## This overrides the metric-type of the Telegraf metric. Globbing is allowed. ## This overrides the metric-type of the Telegraf metric. Globbing is allowed.
# [outputs.prometheus_client.metric_types] # [outputs.prometheus_client.metric_types]