| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package couchbase |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | _ "embed" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "net/http" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 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("couchbase", 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 | Methods: couchbaseMethods, |
| 30 | MethodHandler: couchbaseFunctionHandler, |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | func New() *Collector { |
| 35 | return &Collector{ |
| 36 | Config: Config{ |
| 37 | HTTPConfig: web.HTTPConfig{ |
| 38 | RequestConfig: web.RequestConfig{ |
| 39 | URL: "http://127.0.0.1:8091", |
| 40 | }, |
| 41 | ClientConfig: web.ClientConfig{ |
| 42 | Timeout: confopt.Duration(time.Second), |
| 43 | }, |
| 44 | }, |
| 45 | Functions: FunctionsConfig{ |
| 46 | TopQueries: TopQueriesConfig{ |
| 47 | Limit: 500, |
| 48 | }, |
| 49 | }, |
| 50 | }, |
| 51 | collectedBuckets: make(map[string]bool), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | type Config struct { |
| 56 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 57 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 58 | AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"` |
| 59 | web.HTTPConfig `yaml:",inline" json:""` |
| 60 | QueryURL string `yaml:"query_url,omitempty" json:"query_url,omitempty"` |
| 61 | Functions FunctionsConfig `yaml:"functions,omitempty" json:"functions"` |
| 62 | } |
| 63 | |
| 64 | type FunctionsConfig struct { |
| 65 | TopQueries TopQueriesConfig `yaml:"top_queries,omitempty" json:"top_queries"` |
| 66 | } |
| 67 | |
| 68 | type TopQueriesConfig struct { |
| 69 | Disabled bool `yaml:"disabled" json:"disabled"` |
| 70 | Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"` |
| 71 | Limit int `yaml:"limit,omitempty" json:"limit"` |
| 72 | } |
| 73 | |
| 74 | func (c Config) topQueriesTimeout() time.Duration { |
| 75 | if c.Functions.TopQueries.Timeout == 0 { |
| 76 | return c.Timeout.Duration() |
| 77 | } |
| 78 | return c.Functions.TopQueries.Timeout.Duration() |
| 79 | } |
| 80 | |
| 81 | func (c Config) topQueriesLimit() int { |
| 82 | if c.Functions.TopQueries.Limit <= 0 { |
| 83 | return 500 |
| 84 | } |
| 85 | return c.Functions.TopQueries.Limit |
| 86 | } |
| 87 | |
| 88 | type Collector struct { |
| 89 | collectorapi.Base |
| 90 | Config `yaml:",inline" json:""` |
| 91 | |
| 92 | httpClient *http.Client |
| 93 | charts *collectorapi.Charts |
| 94 | |
| 95 | collectedBuckets map[string]bool |
| 96 | |
| 97 | funcRouter *funcRouter |
| 98 | } |
| 99 | |
| 100 | func (c *Collector) Configuration() any { |
| 101 | return c.Config |
| 102 | } |
| 103 | |
| 104 | func (c *Collector) Init(context.Context) error { |
| 105 | err := c.validateConfig() |
| 106 | if err != nil { |
| 107 | return fmt.Errorf("check configuration: %v", err) |
| 108 | } |
| 109 | |
| 110 | httpClient, err := c.initHTTPClient() |
| 111 | if err != nil { |
| 112 | return fmt.Errorf("init HTTP client: %v", err) |
| 113 | } |
| 114 | c.httpClient = httpClient |
| 115 | |
| 116 | charts, err := c.initCharts() |
| 117 | if err != nil { |
| 118 | return fmt.Errorf("init charts: %v", err) |
| 119 | } |
| 120 | c.charts = charts |
| 121 | |
| 122 | c.funcRouter = newFuncRouter(c) |
| 123 | |
| 124 | return nil |
| 125 | } |
| 126 | |
| 127 | func (c *Collector) Check(context.Context) error { |
| 128 | mx, err := c.collect() |
| 129 | if err != nil { |
| 130 | return err |
| 131 | } |
| 132 | if len(mx) == 0 { |
| 133 | return errors.New("no metrics collected") |
| 134 | |
| 135 | } |
| 136 | return nil |
| 137 | } |
| 138 | |
| 139 | func (c *Collector) Charts() *Charts { |
| 140 | return c.charts |
| 141 | } |
| 142 | |
| 143 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 144 | mx, err := c.collect() |
| 145 | if err != nil { |
| 146 | c.Error(err) |
| 147 | } |
| 148 | |
| 149 | if len(mx) == 0 { |
| 150 | return nil |
| 151 | } |
| 152 | return mx |
| 153 | } |
| 154 | |
| 155 | func (c *Collector) Cleanup(ctx context.Context) { |
| 156 | if c.funcRouter != nil { |
| 157 | c.funcRouter.Cleanup(ctx) |
| 158 | } |
| 159 | if c.httpClient == nil { |
| 160 | return |
| 161 | } |
| 162 | c.httpClient.CloseIdleConnections() |
| 163 | } |