master
go 133 lines 2.79 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package pika
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "fmt"
10 "time"
11
12 "github.com/blang/semver/v4"
13 "github.com/redis/go-redis/v9"
14
15 "github.com/netdata/netdata/go/plugins/pkg/confopt"
16 "github.com/netdata/netdata/go/plugins/pkg/tlscfg"
17 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
18 )
19
20 //go:embed "config_schema.json"
21 var configSchema string
22
23 func init() {
24 collectorapi.Register("pika", collectorapi.Creator{
25 JobConfigSchema: configSchema,
26 Create: func() collectorapi.CollectorV1 { return New() },
27 Config: func() any { return &Config{} },
28 })
29 }
30
31 func New() *Collector {
32 return &Collector{
33 Config: Config{
34 Address: "redis://@localhost:9221",
35 Timeout: confopt.Duration(time.Second),
36 },
37
38 collectedCommands: make(map[string]bool),
39 collectedDbs: make(map[string]bool),
40 }
41 }
42
43 type Config struct {
44 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
45 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
46 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
47 Address string `yaml:"address" json:"address"`
48 Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"`
49 tlscfg.TLSConfig `yaml:",inline" json:""`
50 }
51
52 type (
53 Collector struct {
54 collectorapi.Base
55 Config `yaml:",inline" json:""`
56
57 charts *collectorapi.Charts
58
59 pdb redisClient
60
61 server string
62 version *semver.Version
63 collectedCommands map[string]bool
64 collectedDbs map[string]bool
65 }
66 redisClient interface {
67 Info(ctx context.Context, section ...string) *redis.StringCmd
68 Close() error
69 }
70 )
71
72 func (c *Collector) Configuration() any {
73 return c.Config
74 }
75
76 func (c *Collector) Init(context.Context) error {
77 err := c.validateConfig()
78 if err != nil {
79 return fmt.Errorf("config validation: %v", err)
80 }
81
82 pdb, err := c.initRedisClient()
83 if err != nil {
84 return fmt.Errorf("init redis client: %v", err)
85 }
86 c.pdb = pdb
87
88 charts, err := c.initCharts()
89 if err != nil {
90 return fmt.Errorf("init charts: %v", err)
91 }
92 c.charts = charts
93
94 return nil
95 }
96
97 func (c *Collector) Check(context.Context) error {
98 mx, err := c.collect()
99 if err != nil {
100 return err
101 }
102 if len(mx) == 0 {
103 return errors.New("no metrics collected")
104 }
105 return nil
106 }
107
108 func (c *Collector) Charts() *collectorapi.Charts {
109 return c.charts
110 }
111
112 func (c *Collector) Collect(context.Context) map[string]int64 {
113 ms, err := c.collect()
114 if err != nil {
115 c.Error(err)
116 }
117
118 if len(ms) == 0 {
119 return nil
120 }
121 return ms
122 }
123
124 func (c *Collector) Cleanup(context.Context) {
125 if c.pdb == nil {
126 return
127 }
128 err := c.pdb.Close()
129 if err != nil {
130 c.Warningf("cleanup: error on closing redis client [%s]: %v", c.Address, err)
131 }
132 c.pdb = nil
133 }