master
go 126 lines 2.58 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package rabbitmq
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("rabbitmq", collectorapi.Creator{
23 JobConfigSchema: configSchema,
24 Create: func() collectorapi.CollectorV1 { return New() },
25 Config: func() any { return &Config{} },
26 })
27 }
28
29 func New() *Collector {
30 return &Collector{
31 Config: Config{
32 HTTPConfig: web.HTTPConfig{
33 RequestConfig: web.RequestConfig{
34 URL: "http://localhost:15672",
35 Username: "guest",
36 Password: "guest",
37 },
38 ClientConfig: web.ClientConfig{
39 Timeout: confopt.Duration(time.Second),
40 },
41 },
42 CollectQueues: false,
43 },
44
45 charts: &collectorapi.Charts{},
46 cache: newCache(),
47 queryClusterMeta: true,
48 }
49 }
50
51 type Config struct {
52 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
53 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
54 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
55 web.HTTPConfig `yaml:",inline" json:""`
56 CollectQueues bool `yaml:"collect_queues_metrics" json:"collect_queues_metrics"`
57 }
58
59 type Collector struct {
60 collectorapi.Base
61 Config `yaml:",inline" json:""`
62
63 charts *collectorapi.Charts
64
65 httpClient *http.Client
66
67 queryClusterMeta bool
68 clusterName string
69 clusterId string
70 cache *cache
71 }
72
73 func (c *Collector) Configuration() any {
74 return c.Config
75 }
76
77 func (c *Collector) Init(context.Context) error {
78 if c.URL == "" {
79 return errors.New("config: url not set")
80 }
81
82 client, err := web.NewHTTPClient(c.ClientConfig)
83 if err != nil {
84 return fmt.Errorf("init HTTP client: %v", err)
85 }
86 c.httpClient = client
87
88 c.Debugf("using URL %s", c.URL)
89 c.Debugf("using timeout: %s", c.Timeout)
90
91 return nil
92 }
93
94 func (c *Collector) Check(context.Context) error {
95 mx, err := c.collect()
96 if err != nil {
97 return err
98 }
99 if len(mx) == 0 {
100 return errors.New("no metrics collected")
101 }
102 return nil
103 }
104
105 func (c *Collector) Charts() *collectorapi.Charts {
106 return c.charts
107 }
108
109 func (c *Collector) Collect(context.Context) map[string]int64 {
110 mx, err := c.collect()
111 if err != nil {
112 c.Error(err)
113 }
114
115 if len(mx) == 0 {
116 return nil
117 }
118
119 return mx
120 }
121
122 func (c *Collector) Cleanup(context.Context) {
123 if c.httpClient != nil {
124 c.httpClient.CloseIdleConnections()
125 }
126 }