master
go 124 lines 2.63 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package riakkv
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "fmt"
10 "net/http"
11 "sync"
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("riakkv", collectorapi.Creator{
24 Create: func() collectorapi.CollectorV1 { return New() },
25 // Riak updates the metrics on the /stats endpoint every 1 second.
26 // If we use 1 here, it means we might get weird jitter in the graph,
27 // so the default is set to 2 seconds to prevent that.
28 Defaults: collectorapi.Defaults{
29 UpdateEvery: 2,
30 },
31 JobConfigSchema: configSchema,
32 Config: func() any { return &Config{} },
33 })
34 }
35
36 func New() *Collector {
37 return &Collector{
38 Config: Config{
39 HTTPConfig: web.HTTPConfig{
40 RequestConfig: web.RequestConfig{
41 // https://docs.riak.com/riak/kv/2.2.3/developing/api/http/status.1.html
42 URL: "http://127.0.0.1:8098/stats",
43 },
44 ClientConfig: web.ClientConfig{
45 Timeout: confopt.Duration(time.Second),
46 },
47 },
48 },
49 once: &sync.Once{},
50 charts: charts.Copy(),
51 }
52 }
53
54 type Config struct {
55 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
56 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
57 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
58 web.HTTPConfig `yaml:",inline" json:""`
59 }
60
61 type Collector struct {
62 collectorapi.Base
63 Config `yaml:",inline" json:""`
64
65 once *sync.Once
66 charts *collectorapi.Charts
67
68 httpClient *http.Client
69 }
70
71 func (c *Collector) Configuration() any {
72 return c.Config
73 }
74
75 func (c *Collector) Init(context.Context) error {
76 if c.URL == "" {
77 return errors.New("config: url not set")
78 }
79
80 httpClient, err := web.NewHTTPClient(c.ClientConfig)
81 if err != nil {
82 return fmt.Errorf("init HTTP client: %v", err)
83 }
84 c.httpClient = httpClient
85
86 c.Debugf("using URL %s", c.URL)
87 c.Debugf("using timeout: %s", c.Timeout)
88
89 return nil
90 }
91
92 func (c *Collector) Check(context.Context) error {
93 mx, err := c.collect()
94 if err != nil {
95 return err
96 }
97 if len(mx) == 0 {
98 return errors.New("no metrics collected")
99
100 }
101 return nil
102 }
103
104 func (c *Collector) Charts() *collectorapi.Charts {
105 return c.charts
106 }
107
108 func (c *Collector) Collect(context.Context) map[string]int64 {
109 mx, err := c.collect()
110 if err != nil {
111 c.Error(err)
112 }
113
114 if len(mx) == 0 {
115 return nil
116 }
117 return mx
118 }
119
120 func (c *Collector) Cleanup(context.Context) {
121 if c.httpClient != nil {
122 c.httpClient.CloseIdleConnections()
123 }
124 }