github.com/netdata/go.d.plugin@v0.58.1/modules/prometheus/init.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package prometheus 4 5 import ( 6 "errors" 7 "fmt" 8 "os" 9 10 "github.com/netdata/go.d.plugin/pkg/matcher" 11 "github.com/netdata/go.d.plugin/pkg/prometheus" 12 "github.com/netdata/go.d.plugin/pkg/web" 13 ) 14 15 func (p *Prometheus) validateConfig() error { 16 if p.URL == "" { 17 return errors.New("'url' can not be empty") 18 } 19 return nil 20 } 21 22 func (p *Prometheus) initPrometheusClient() (prometheus.Prometheus, error) { 23 httpClient, err := web.NewHTTPClient(p.Client) 24 if err != nil { 25 return nil, fmt.Errorf("init HTTP client: %v", err) 26 } 27 28 req := p.Request.Copy() 29 if p.BearerTokenFile != "" { 30 token, err := os.ReadFile(p.BearerTokenFile) 31 if err != nil { 32 return nil, fmt.Errorf("bearer token file: %v", err) 33 } 34 req.Headers["Authorization"] = "Bearer " + string(token) 35 } 36 37 sr, err := p.Selector.Parse() 38 if err != nil { 39 return nil, fmt.Errorf("parsing selector: %v", err) 40 } 41 42 if sr != nil { 43 return prometheus.NewWithSelector(httpClient, req, sr), nil 44 } 45 return prometheus.New(httpClient, req), nil 46 } 47 48 func (p *Prometheus) initFallbackTypeMatcher(expr []string) (matcher.Matcher, error) { 49 if len(expr) == 0 { 50 return nil, nil 51 } 52 53 m := matcher.FALSE() 54 55 for _, pattern := range expr { 56 v, err := matcher.NewGlobMatcher(pattern) 57 if err != nil { 58 return nil, fmt.Errorf("error on parsing pattern '%s': %v", pattern, err) 59 } 60 m = matcher.Or(m, v) 61 } 62 63 return m, nil 64 }