Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
module/prometheus: add bearer_token_file option (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 authored Sep 8, 2020
1 parent b2e4b5b commit b8cf89d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
6 changes: 6 additions & 0 deletions config/go.d/prometheus.conf
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@
# Syntax:
# max_time_series: 1000
#
#
# - bearer_token_file
# Path to bearer token file.
# Syntax:
# bearer_token_file: '/var/run/secrets/kubernetes.io/serviceaccount/token'
#
# - username
# Username for basic HTTP authentication.
# Syntax:
Expand Down
14 changes: 12 additions & 2 deletions modules/prometheus/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package prometheus
import (
"errors"
"fmt"
"io/ioutil"
"strings"

"github.com/netdata/go.d.plugin/pkg/prometheus"
Expand All @@ -25,15 +26,24 @@ func (p Prometheus) initPrometheusClient() (prometheus.Prometheus, error) {
return nil, fmt.Errorf("creating HTTP client: %v", err)
}

req := p.Request.Copy()
if p.BearerTokenFile != "" {
token, err := ioutil.ReadFile(p.BearerTokenFile)
if err != nil {
return nil, fmt.Errorf("reading bearer token file: %v", err)
}
req.Headers["Authorization"] = "Bearer " + string(token)
}

sr, err := p.Selector.Parse()
if err != nil {
return nil, fmt.Errorf("parsing selector: %v", err)
}

if sr != nil {
return prometheus.NewWithSelector(client, p.Request, sr), nil
return prometheus.NewWithSelector(client, req, sr), nil
}
return prometheus.New(client, p.Request), nil
return prometheus.New(client, req), nil
}

func (p Prometheus) initOptionalGrouping() ([]optionalGrouping, error) {
Expand Down
11 changes: 6 additions & 5 deletions modules/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ func New() *Prometheus {

type (
Config struct {
web.HTTP `yaml:",inline"`
MaxTS int `yaml:"max_time_series"`
MaxTSPerMetric int `yaml:"max_time_series_per_metric"`
Selector selector.Expr `yaml:"selector"`
Grouping []GroupOption `yaml:"group"`
web.HTTP `yaml:",inline"`
BearerTokenFile string `yaml:"bearer_token_file"` // TODO: part of web.Request?
MaxTS int `yaml:"max_time_series"`
MaxTSPerMetric int `yaml:"max_time_series_per_metric"`
Selector selector.Expr `yaml:"selector"`
Grouping []GroupOption `yaml:"group"`
}
GroupOption struct {
Selector string `yaml:"selector"`
Expand Down

0 comments on commit b8cf89d

Please sign in to comment.