| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package geth |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | _ "embed" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 13 | "github.com/netdata/netdata/go/plugins/pkg/prometheus" |
| 14 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 15 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 16 | ) |
| 17 | |
| 18 | //go:embed "config_schema.json" |
| 19 | var configSchema string |
| 20 | |
| 21 | func init() { |
| 22 | collectorapi.Register("geth", collectorapi.Creator{ |
| 23 | JobConfigSchema: configSchema, |
| 24 | Create: func() collectorapi.CollectorV1 { return New() }, |
| 25 | Config: func() any { return &Config{} }, |
| 26 | }) |
| 27 | } |
| 28 | |
| 29 | func New() *Collector { |
| 30 | return &Collector{ |
| 31 | Config: Config{ |
| 32 | HTTPConfig: web.HTTPConfig{ |
| 33 | RequestConfig: web.RequestConfig{ |
| 34 | URL: "http://127.0.0.1:6060/debug/metrics/prometheus", |
| 35 | }, |
| 36 | ClientConfig: web.ClientConfig{ |
| 37 | Timeout: confopt.Duration(time.Second), |
| 38 | }, |
| 39 | }, |
| 40 | }, |
| 41 | charts: charts.Copy(), |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | type Config struct { |
| 46 | web.HTTPConfig `yaml:",inline" json:""` |
| 47 | UpdateEvery int `yaml:"update_every" json:"update_every"` |
| 48 | } |
| 49 | |
| 50 | type Collector struct { |
| 51 | collectorapi.Base |
| 52 | Config `yaml:",inline" json:""` |
| 53 | |
| 54 | charts *Charts |
| 55 | |
| 56 | prom prometheus.Prometheus |
| 57 | } |
| 58 | |
| 59 | func (c *Collector) Configuration() any { |
| 60 | return c.Config |
| 61 | } |
| 62 | |
| 63 | func (c *Collector) Init(context.Context) error { |
| 64 | if err := c.validateConfig(); err != nil { |
| 65 | return fmt.Errorf("error on validating config: %v", err) |
| 66 | } |
| 67 | |
| 68 | prom, err := c.initPrometheusClient() |
| 69 | if err != nil { |
| 70 | return fmt.Errorf("error on initializing prometheus client: %v", err) |
| 71 | } |
| 72 | c.prom = prom |
| 73 | |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | func (c *Collector) Check(context.Context) error { |
| 78 | mx, err := c.collect() |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | if len(mx) == 0 { |
| 83 | return errors.New("no metrics collected") |
| 84 | } |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | func (c *Collector) Charts() *Charts { |
| 89 | return c.charts |
| 90 | } |
| 91 | |
| 92 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 93 | mx, err := c.collect() |
| 94 | if err != nil { |
| 95 | c.Error(err) |
| 96 | } |
| 97 | |
| 98 | if len(mx) == 0 { |
| 99 | return nil |
| 100 | } |
| 101 | return mx |
| 102 | } |
| 103 | |
| 104 | func (c *Collector) Cleanup(context.Context) { |
| 105 | if c.prom != nil && c.prom.HTTPClient() != nil { |
| 106 | c.prom.HTTPClient().CloseIdleConnections() |
| 107 | } |
| 108 | } |