diff options
author | Austin S. Hemmelgarn <austin@netdata.cloud> | 2024-02-13 06:56:20 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-13 06:56:20 -0500 |
commit | 3a29b66132f561c910d827e8c7ae82997f7c1f30 (patch) | |
tree | a9306156631b6b188de8877f7c1dbdbe8b067804 /src/go/collectors/go.d.plugin/modules/prometheus/prometheus.go | |
parent | 57eec3da0e51baa400037ccc4b547cb839ab6ffa (diff) |
Include Go plugin sources in main repository. (#16997)
* Include Go plugin sources in main repository.
* Fix CI issues.
* Rename source tree.
Diffstat (limited to 'src/go/collectors/go.d.plugin/modules/prometheus/prometheus.go')
-rw-r--r-- | src/go/collectors/go.d.plugin/modules/prometheus/prometheus.go | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/modules/prometheus/prometheus.go b/src/go/collectors/go.d.plugin/modules/prometheus/prometheus.go new file mode 100644 index 0000000000..32a91e5c25 --- /dev/null +++ b/src/go/collectors/go.d.plugin/modules/prometheus/prometheus.go @@ -0,0 +1,127 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package prometheus + +import ( + _ "embed" + "time" + + "github.com/netdata/go.d.plugin/agent/module" + "github.com/netdata/go.d.plugin/pkg/matcher" + "github.com/netdata/go.d.plugin/pkg/prometheus" + "github.com/netdata/go.d.plugin/pkg/prometheus/selector" + "github.com/netdata/go.d.plugin/pkg/web" +) + +//go:embed "config_schema.json" +var configSchema string + +func init() { + module.Register("prometheus", module.Creator{ + JobConfigSchema: configSchema, + Defaults: module.Defaults{ + UpdateEvery: 10, + }, + Create: func() module.Module { return New() }, + }) +} + +func New() *Prometheus { + return &Prometheus{ + Config: Config{ + HTTP: web.HTTP{ + Client: web.Client{ + Timeout: web.Duration{Duration: time.Second * 10}, + }, + }, + MaxTS: 2000, + MaxTSPerMetric: 200, + }, + charts: &module.Charts{}, + cache: newCache(), + } +} + +type Config struct { + web.HTTP `yaml:",inline"` + Name string `yaml:"name"` + Application string `yaml:"app"` + BearerTokenFile string `yaml:"bearer_token_file"` + + Selector selector.Expr `yaml:"selector"` + + ExpectedPrefix string `yaml:"expected_prefix"` + MaxTS int `yaml:"max_time_series"` + MaxTSPerMetric int `yaml:"max_time_series_per_metric"` + FallbackType struct { + Counter []string `yaml:"counter"` + Gauge []string `yaml:"gauge"` + } `yaml:"fallback_type"` +} + +type Prometheus struct { + module.Base + Config `yaml:",inline"` + + charts *module.Charts + + prom prometheus.Prometheus + cache *cache + + fallbackType struct { + counter matcher.Matcher + gauge matcher.Matcher + } +} + +func (p *Prometheus) Init() bool { + if err := p.validateConfig(); err != nil { + p.Errorf("validating config: %v", err) + return false + } + + prom, err := p.initPrometheusClient() + if err != nil { + p.Errorf("init prometheus client: %v", err) + return false + } + p.prom = prom + + m, err := p.initFallbackTypeMatcher(p.FallbackType.Counter) + if err != nil { + p.Errorf("init counter fallback type matcher: %v", err) + return false + } + p.fallbackType.counter = m + + m, err = p.initFallbackTypeMatcher(p.FallbackType.Gauge) + if err != nil { + p.Errorf("init counter fallback type matcher: %v", err) + return false + } + p.fallbackType.gauge = m + + return true +} + +func (p *Prometheus) Check() bool { + return len(p.Collect()) > 0 +} + +func (p *Prometheus) Charts() *module.Charts { + return p.charts +} + +func (p *Prometheus) Collect() map[string]int64 { + mx, err := p.collect() + if err != nil { + p.Error(err) + } + + if len(mx) == 0 { + return nil + } + return mx +} + +func (p *Prometheus) Cleanup() {} |