| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package cassandra |
| 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("cassandra", collectorapi.Creator{ |
| 23 | JobConfigSchema: configSchema, |
| 24 | Defaults: collectorapi.Defaults{ |
| 25 | UpdateEvery: 5, |
| 26 | }, |
| 27 | Create: func() collectorapi.CollectorV1 { return New() }, |
| 28 | Config: func() any { return &Config{} }, |
| 29 | }) |
| 30 | } |
| 31 | |
| 32 | func New() *Collector { |
| 33 | return &Collector{ |
| 34 | Config: Config{ |
| 35 | HTTPConfig: web.HTTPConfig{ |
| 36 | RequestConfig: web.RequestConfig{ |
| 37 | URL: "http://127.0.0.1:7072/metrics", |
| 38 | }, |
| 39 | ClientConfig: web.ClientConfig{ |
| 40 | Timeout: confopt.Duration(time.Second * 5), |
| 41 | }, |
| 42 | }, |
| 43 | }, |
| 44 | charts: baseCharts.Copy(), |
| 45 | validateMetrics: true, |
| 46 | mx: newCassandraMetrics(), |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | type Config struct { |
| 51 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 52 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 53 | AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"` |
| 54 | web.HTTPConfig `yaml:",inline" json:""` |
| 55 | } |
| 56 | |
| 57 | type Collector struct { |
| 58 | collectorapi.Base |
| 59 | Config `yaml:",inline" json:""` |
| 60 | |
| 61 | charts *collectorapi.Charts |
| 62 | |
| 63 | prom prometheus.Prometheus |
| 64 | |
| 65 | validateMetrics bool |
| 66 | |
| 67 | mx *cassandraMetrics |
| 68 | } |
| 69 | |
| 70 | func (c *Collector) Configuration() any { |
| 71 | return c.Config |
| 72 | } |
| 73 | |
| 74 | func (c *Collector) Init(context.Context) error { |
| 75 | if err := c.validateConfig(); err != nil { |
| 76 | return fmt.Errorf("error on validating config: %v", err) |
| 77 | } |
| 78 | |
| 79 | prom, err := c.initPrometheusClient() |
| 80 | if err != nil { |
| 81 | return fmt.Errorf("error on init prometheus client: %v", err) |
| 82 | } |
| 83 | c.prom = prom |
| 84 | |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | func (c *Collector) Check(context.Context) error { |
| 89 | mx, err := c.collect() |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | if len(mx) == 0 { |
| 94 | return errors.New("no metrics collected") |
| 95 | |
| 96 | } |
| 97 | return nil |
| 98 | } |
| 99 | |
| 100 | func (c *Collector) Charts() *collectorapi.Charts { |
| 101 | return c.charts |
| 102 | } |
| 103 | |
| 104 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 105 | mx, err := c.collect() |
| 106 | if err != nil { |
| 107 | c.Error(err) |
| 108 | } |
| 109 | |
| 110 | if len(mx) == 0 { |
| 111 | return nil |
| 112 | } |
| 113 | return mx |
| 114 | } |
| 115 | |
| 116 | func (c *Collector) Cleanup(context.Context) { |
| 117 | if c.prom != nil && c.prom.HTTPClient() != nil { |
| 118 | c.prom.HTTPClient().CloseIdleConnections() |
| 119 | } |
| 120 | } |