master
go 138 lines 3.04 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package pulsar
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "fmt"
10 "sync"
11 "time"
12
13 "github.com/netdata/netdata/go/plugins/pkg/confopt"
14 "github.com/netdata/netdata/go/plugins/pkg/matcher"
15 "github.com/netdata/netdata/go/plugins/pkg/prometheus"
16 "github.com/netdata/netdata/go/plugins/pkg/web"
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("pulsar", collectorapi.Creator{
25 JobConfigSchema: configSchema,
26 Defaults: collectorapi.Defaults{
27 UpdateEvery: 60,
28 },
29 Create: func() collectorapi.CollectorV1 { return New() },
30 Config: func() any { return &Config{} },
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:8080/metrics",
40 },
41 ClientConfig: web.ClientConfig{
42 Timeout: confopt.Duration(time.Second * 5),
43 },
44 },
45 TopicFilter: matcher.SimpleExpr{
46 Includes: nil,
47 Excludes: []string{"*"},
48 },
49 },
50 once: &sync.Once{},
51 charts: summaryCharts.Copy(),
52 nsCharts: namespaceCharts.Copy(),
53 topicChartsMapping: topicChartsMapping(),
54 cache: newCache(),
55 curCache: newCache(),
56 }
57 }
58
59 type Config struct {
60 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
61 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
62 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
63 web.HTTPConfig `yaml:",inline" json:""`
64 TopicFilter matcher.SimpleExpr `yaml:"topic_filter,omitempty" json:"topic_filter"`
65 }
66
67 type Collector struct {
68 collectorapi.Base
69 Config `yaml:",inline" json:""`
70
71 charts *Charts
72 nsCharts *Charts
73
74 prom prometheus.Prometheus
75
76 topicFilter matcher.Matcher
77 cache *cache
78 curCache *cache
79 once *sync.Once
80 topicChartsMapping map[string]string
81 }
82
83 func (c *Collector) Configuration() any {
84 return c.Config
85 }
86
87 func (c *Collector) Init(context.Context) error {
88 if err := c.validateConfig(); err != nil {
89 return fmt.Errorf("config validation: %v", err)
90 }
91
92 prom, err := c.initPrometheusClient()
93 if err != nil {
94 return fmt.Errorf("init prometheus client: %v", err)
95 }
96 c.prom = prom
97
98 m, err := c.initTopicFilerMatcher()
99 if err != nil {
100 return fmt.Errorf("init topic filer: %v", err)
101 }
102 c.topicFilter = m
103
104 return nil
105 }
106
107 func (c *Collector) Check(context.Context) error {
108 mx, err := c.collect()
109 if err != nil {
110 return err
111 }
112 if len(mx) == 0 {
113 return errors.New("no metrics collected")
114 }
115 return nil
116 }
117
118 func (c *Collector) Charts() *Charts {
119 return c.charts
120 }
121
122 func (c *Collector) Collect(context.Context) map[string]int64 {
123 mx, err := c.collect()
124 if err != nil {
125 c.Error(err)
126 }
127
128 if len(mx) == 0 {
129 return nil
130 }
131 return mx
132 }
133
134 func (c *Collector) Cleanup(context.Context) {
135 if c.prom != nil && c.prom.HTTPClient() != nil {
136 c.prom.HTTPClient().CloseIdleConnections()
137 }
138 }