github.com/netdata/go.d.plugin@v0.58.1/modules/prometheus/prometheus.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package prometheus 4 5 import ( 6 _ "embed" 7 "time" 8 9 "github.com/netdata/go.d.plugin/agent/module" 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/prometheus/selector" 13 "github.com/netdata/go.d.plugin/pkg/web" 14 ) 15 16 //go:embed "config_schema.json" 17 var configSchema string 18 19 func init() { 20 module.Register("prometheus", module.Creator{ 21 JobConfigSchema: configSchema, 22 Defaults: module.Defaults{ 23 UpdateEvery: 10, 24 }, 25 Create: func() module.Module { return New() }, 26 }) 27 } 28 29 func New() *Prometheus { 30 return &Prometheus{ 31 Config: Config{ 32 HTTP: web.HTTP{ 33 Client: web.Client{ 34 Timeout: web.Duration{Duration: time.Second * 10}, 35 }, 36 }, 37 MaxTS: 2000, 38 MaxTSPerMetric: 200, 39 }, 40 charts: &module.Charts{}, 41 cache: newCache(), 42 } 43 } 44 45 type Config struct { 46 web.HTTP `yaml:",inline"` 47 Name string `yaml:"name"` 48 Application string `yaml:"app"` 49 BearerTokenFile string `yaml:"bearer_token_file"` 50 51 Selector selector.Expr `yaml:"selector"` 52 53 ExpectedPrefix string `yaml:"expected_prefix"` 54 MaxTS int `yaml:"max_time_series"` 55 MaxTSPerMetric int `yaml:"max_time_series_per_metric"` 56 FallbackType struct { 57 Counter []string `yaml:"counter"` 58 Gauge []string `yaml:"gauge"` 59 } `yaml:"fallback_type"` 60 } 61 62 type Prometheus struct { 63 module.Base 64 Config `yaml:",inline"` 65 66 charts *module.Charts 67 68 prom prometheus.Prometheus 69 cache *cache 70 71 fallbackType struct { 72 counter matcher.Matcher 73 gauge matcher.Matcher 74 } 75 } 76 77 func (p *Prometheus) Init() bool { 78 if err := p.validateConfig(); err != nil { 79 p.Errorf("validating config: %v", err) 80 return false 81 } 82 83 prom, err := p.initPrometheusClient() 84 if err != nil { 85 p.Errorf("init prometheus client: %v", err) 86 return false 87 } 88 p.prom = prom 89 90 m, err := p.initFallbackTypeMatcher(p.FallbackType.Counter) 91 if err != nil { 92 p.Errorf("init counter fallback type matcher: %v", err) 93 return false 94 } 95 p.fallbackType.counter = m 96 97 m, err = p.initFallbackTypeMatcher(p.FallbackType.Gauge) 98 if err != nil { 99 p.Errorf("init counter fallback type matcher: %v", err) 100 return false 101 } 102 p.fallbackType.gauge = m 103 104 return true 105 } 106 107 func (p *Prometheus) Check() bool { 108 return len(p.Collect()) > 0 109 } 110 111 func (p *Prometheus) Charts() *module.Charts { 112 return p.charts 113 } 114 115 func (p *Prometheus) Collect() map[string]int64 { 116 mx, err := p.collect() 117 if err != nil { 118 p.Error(err) 119 } 120 121 if len(mx) == 0 { 122 return nil 123 } 124 return mx 125 } 126 127 func (p *Prometheus) Cleanup() {}