| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package couchdb |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | _ "embed" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "net/http" |
| 11 | "strings" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 15 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 16 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 17 | ) |
| 18 | |
| 19 | //go:embed "config_schema.json" |
| 20 | var configSchema string |
| 21 | |
| 22 | func init() { |
| 23 | collectorapi.Register("couchdb", collectorapi.Creator{ |
| 24 | JobConfigSchema: configSchema, |
| 25 | Defaults: collectorapi.Defaults{ |
| 26 | UpdateEvery: 10, |
| 27 | }, |
| 28 | Create: func() collectorapi.CollectorV1 { return New() }, |
| 29 | Config: func() any { return &Config{} }, |
| 30 | }) |
| 31 | } |
| 32 | |
| 33 | func New() *Collector { |
| 34 | return &Collector{ |
| 35 | Config: Config{ |
| 36 | HTTPConfig: web.HTTPConfig{ |
| 37 | RequestConfig: web.RequestConfig{ |
| 38 | URL: "http://127.0.0.1:5984", |
| 39 | }, |
| 40 | ClientConfig: web.ClientConfig{ |
| 41 | Timeout: confopt.Duration(time.Second * 2), |
| 42 | }, |
| 43 | }, |
| 44 | Node: "_local", |
| 45 | }, |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | type Config struct { |
| 50 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 51 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 52 | AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"` |
| 53 | web.HTTPConfig `yaml:",inline" json:""` |
| 54 | Node string `yaml:"node,omitempty" json:"node"` |
| 55 | Databases string `yaml:"databases,omitempty" json:"databases"` |
| 56 | } |
| 57 | |
| 58 | type Collector struct { |
| 59 | collectorapi.Base |
| 60 | Config `yaml:",inline" json:""` |
| 61 | |
| 62 | charts *collectorapi.Charts |
| 63 | |
| 64 | httpClient *http.Client |
| 65 | |
| 66 | databases []string |
| 67 | } |
| 68 | |
| 69 | func (c *Collector) Configuration() any { |
| 70 | return c.Config |
| 71 | } |
| 72 | |
| 73 | func (c *Collector) Init(context.Context) error { |
| 74 | err := c.validateConfig() |
| 75 | if err != nil { |
| 76 | return fmt.Errorf("check configuration: %v", err) |
| 77 | } |
| 78 | |
| 79 | c.databases = strings.Fields(c.Config.Databases) |
| 80 | |
| 81 | httpClient, err := c.initHTTPClient() |
| 82 | if err != nil { |
| 83 | return fmt.Errorf("init HTTP client: %v", err) |
| 84 | } |
| 85 | c.httpClient = httpClient |
| 86 | |
| 87 | charts, err := c.initCharts() |
| 88 | if err != nil { |
| 89 | return fmt.Errorf("init charts: %v", err) |
| 90 | } |
| 91 | c.charts = charts |
| 92 | |
| 93 | return nil |
| 94 | } |
| 95 | |
| 96 | func (c *Collector) Check(context.Context) error { |
| 97 | if err := c.pingCouchDB(); err != nil { |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | mx, err := c.collect() |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | |
| 106 | if len(mx) == 0 { |
| 107 | return errors.New("no metrics collected") |
| 108 | } |
| 109 | |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | func (c *Collector) Charts() *Charts { |
| 114 | return c.charts |
| 115 | } |
| 116 | |
| 117 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 118 | mx, err := c.collect() |
| 119 | if err != nil { |
| 120 | c.Error(err) |
| 121 | } |
| 122 | |
| 123 | if len(mx) == 0 { |
| 124 | return nil |
| 125 | } |
| 126 | return mx |
| 127 | } |
| 128 | |
| 129 | func (c *Collector) Cleanup(context.Context) { |
| 130 | if c.httpClient == nil { |
| 131 | return |
| 132 | } |
| 133 | c.httpClient.CloseIdleConnections() |
| 134 | } |