Commit 80b92335 by r.khavronenko

allow setting basic auth headers for prometheus datasource

parent 3827c0a6
...@@ -22,6 +22,18 @@ type PrometheusExecutor struct { ...@@ -22,6 +22,18 @@ type PrometheusExecutor struct {
Transport *http.Transport Transport *http.Transport
} }
type basicAuthTransport struct {
*http.Transport
username string
password string
}
func (bat basicAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.SetBasicAuth(bat.username, bat.password)
return http.DefaultTransport.RoundTrip(req)
}
func NewPrometheusExecutor(dsInfo *models.DataSource) (tsdb.Executor, error) { func NewPrometheusExecutor(dsInfo *models.DataSource) (tsdb.Executor, error) {
transport, err := dsInfo.GetHttpTransport() transport, err := dsInfo.GetHttpTransport()
if err != nil { if err != nil {
...@@ -51,6 +63,14 @@ func (e *PrometheusExecutor) getClient() (prometheus.QueryAPI, error) { ...@@ -51,6 +63,14 @@ func (e *PrometheusExecutor) getClient() (prometheus.QueryAPI, error) {
Transport: e.Transport, Transport: e.Transport,
} }
if e.BasicAuth {
cfg.Transport = basicAuthTransport{
Transport: e.Transport,
username: e.BasicAuthUser,
password: e.BasicAuthPassword,
}
}
client, err := prometheus.New(cfg) client, err := prometheus.New(cfg)
if err != nil { if err != nil {
return nil, err return nil, err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment