master
go 147 lines 3.49 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package rethinkdb
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "time"
10
11 "github.com/netdata/netdata/go/plugins/pkg/confopt"
12 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
13 )
14
15 //go:embed "config_schema.json"
16 var configSchema string
17
18 func init() {
19 collectorapi.Register("rethinkdb", collectorapi.Creator{
20 JobConfigSchema: configSchema,
21 Create: func() collectorapi.CollectorV1 { return New() },
22 Config: func() any { return &Config{} },
23 Methods: rethinkdbMethods,
24 MethodHandler: rethinkdbFunctionHandler,
25 })
26 }
27
28 func New() *Collector {
29 c := &Collector{
30 Config: Config{
31 Address: "127.0.0.1:28015",
32 Timeout: confopt.Duration(time.Second * 1),
33 Functions: FunctionsConfig{
34 RunningQueries: RunningQueriesConfig{
35 Limit: 500,
36 },
37 },
38 },
39
40 charts: clusterCharts.Copy(),
41 newConn: newRethinkdbConn,
42 seenServers: make(map[string]bool),
43 }
44
45 c.funcRouter = newFuncRouter(c)
46
47 return c
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 Address string `yaml:"address" json:"address"`
55 Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"`
56 Username string `yaml:"username,omitempty" json:"username"`
57 Password string `yaml:"password,omitempty" json:"password"`
58 Functions FunctionsConfig `yaml:"functions,omitempty" json:"functions"`
59 }
60
61 type FunctionsConfig struct {
62 RunningQueries RunningQueriesConfig `yaml:"running_queries,omitempty" json:"running_queries"`
63 }
64
65 type RunningQueriesConfig struct {
66 Disabled bool `yaml:"disabled" json:"disabled"`
67 Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"`
68 Limit int `yaml:"limit,omitempty" json:"limit"`
69 }
70
71 func (c Config) runningQueriesTimeout() time.Duration {
72 if c.Functions.RunningQueries.Timeout == 0 {
73 return c.Timeout.Duration()
74 }
75 return c.Functions.RunningQueries.Timeout.Duration()
76 }
77
78 func (c Config) runningQueriesLimit() int {
79 if c.Functions.RunningQueries.Limit <= 0 {
80 return 500
81 }
82 return c.Functions.RunningQueries.Limit
83 }
84
85 type Collector struct {
86 collectorapi.Base
87 Config `yaml:",inline" json:""`
88
89 charts *collectorapi.Charts
90
91 newConn func(cfg Config) (rdbConn, error)
92 rdb rdbConn
93
94 funcRouter *funcRouter // function router for method handlers
95
96 seenServers map[string]bool
97 }
98
99 func (c *Collector) Configuration() any {
100 return c.Config
101 }
102
103 func (c *Collector) Init(context.Context) error {
104 if c.Address == "" {
105 return errors.New("config: address is not set")
106 }
107 return nil
108 }
109
110 func (c *Collector) Check(context.Context) error {
111 mx, err := c.collect()
112 if err != nil {
113 return err
114 }
115 if len(mx) == 0 {
116 return errors.New("no metrics collected")
117 }
118 return nil
119 }
120
121 func (c *Collector) Charts() *collectorapi.Charts {
122 return c.charts
123 }
124
125 func (c *Collector) Collect(context.Context) map[string]int64 {
126 ms, err := c.collect()
127 if err != nil {
128 c.Error(err)
129 }
130
131 if len(ms) == 0 {
132 return nil
133 }
134 return ms
135 }
136
137 func (c *Collector) Cleanup(ctx context.Context) {
138 if c.funcRouter != nil {
139 c.funcRouter.Cleanup(ctx)
140 }
141 if c.rdb != nil {
142 if err := c.rdb.close(); err != nil {
143 c.Warningf("cleanup: error on closing client [%s]: %v", c.Address, err)
144 }
145 c.rdb = nil
146 }
147 }