summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/prometheus/init.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-05 12:08:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-05 12:08:18 +0000
commit5da14042f70711ea5cf66e034699730335462f66 (patch)
tree0f6354ccac934ed87a2d555f45be4c831cf92f4a /src/go/collectors/go.d.plugin/modules/prometheus/init.go
parentReleasing debian version 1.44.3-2. (diff)
downloadnetdata-5da14042f70711ea5cf66e034699730335462f66.tar.xz
netdata-5da14042f70711ea5cf66e034699730335462f66.zip
Merging upstream version 1.45.3+dfsg.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--src/go/collectors/go.d.plugin/modules/prometheus/init.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/modules/prometheus/init.go b/src/go/collectors/go.d.plugin/modules/prometheus/init.go
new file mode 100644
index 000000000..f5cc8bca9
--- /dev/null
+++ b/src/go/collectors/go.d.plugin/modules/prometheus/init.go
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package prometheus
+
+import (
+ "errors"
+ "fmt"
+ "os"
+
+ "github.com/netdata/netdata/go/go.d.plugin/pkg/matcher"
+ "github.com/netdata/netdata/go/go.d.plugin/pkg/prometheus"
+ "github.com/netdata/netdata/go/go.d.plugin/pkg/web"
+)
+
+func (p *Prometheus) validateConfig() error {
+ if p.URL == "" {
+ return errors.New("'url' can not be empty")
+ }
+ return nil
+}
+
+func (p *Prometheus) initPrometheusClient() (prometheus.Prometheus, error) {
+ httpClient, err := web.NewHTTPClient(p.Client)
+ if err != nil {
+ return nil, fmt.Errorf("init HTTP client: %v", err)
+ }
+
+ req := p.Request.Copy()
+ if p.BearerTokenFile != "" {
+ token, err := os.ReadFile(p.BearerTokenFile)
+ if err != nil {
+ return nil, fmt.Errorf("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(httpClient, req, sr), nil
+ }
+ return prometheus.New(httpClient, req), nil
+}
+
+func (p *Prometheus) initFallbackTypeMatcher(expr []string) (matcher.Matcher, error) {
+ if len(expr) == 0 {
+ return nil, nil
+ }
+
+ m := matcher.FALSE()
+
+ for _, pattern := range expr {
+ v, err := matcher.NewGlobMatcher(pattern)
+ if err != nil {
+ return nil, fmt.Errorf("error on parsing pattern '%s': %v", pattern, err)
+ }
+ m = matcher.Or(m, v)
+ }
+
+ return m, nil
+}