master
go 107 lines 2.21 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package gearman
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "time"
10
11 "github.com/netdata/netdata/go/plugins/pkg/confopt"
12 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
13 )
14
15 //go:embed "config_schema.json"
16 var configSchema string
17
18 func init() {
19 collectorapi.Register("gearman", collectorapi.Creator{
20 JobConfigSchema: configSchema,
21 Create: func() collectorapi.CollectorV1 { return New() },
22 Config: func() any { return &Config{} },
23 })
24 }
25
26 func New() *Collector {
27 return &Collector{
28 Config: Config{
29 Address: "127.0.0.1:4730",
30 Timeout: confopt.Duration(time.Second * 1),
31 },
32 newConn: newGearmanConn,
33 charts: summaryCharts.Copy(),
34 seenTasks: make(map[string]bool),
35 seenPriorityTasks: make(map[string]bool),
36 }
37 }
38
39 type Config struct {
40 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
41 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
42 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
43 Address string `yaml:"address" json:"address"`
44 Timeout confopt.Duration `yaml:"timeout" json:"timeout"`
45 }
46
47 type Collector struct {
48 collectorapi.Base
49 Config `yaml:",inline" json:""`
50
51 charts *collectorapi.Charts
52
53 newConn func(Config) gearmanConn
54 conn gearmanConn
55
56 seenTasks map[string]bool
57 seenPriorityTasks map[string]bool
58 }
59
60 func (c *Collector) Configuration() any {
61 return c.Config
62 }
63
64 func (c *Collector) Init(context.Context) error {
65 if c.Address == "" {
66 return errors.New("config: 'address' not set")
67 }
68
69 return nil
70 }
71
72 func (c *Collector) Check(context.Context) error {
73 mx, err := c.collect()
74 if err != nil {
75 return err
76 }
77
78 if len(mx) == 0 {
79 return errors.New("no metrics collected")
80 }
81
82 return nil
83 }
84
85 func (c *Collector) Charts() *collectorapi.Charts {
86 return c.charts
87 }
88
89 func (c *Collector) Collect(context.Context) map[string]int64 {
90 mx, err := c.collect()
91 if err != nil {
92 c.Error(err)
93 }
94
95 if len(mx) == 0 {
96 return nil
97 }
98
99 return mx
100 }
101
102 func (c *Collector) Cleanup(context.Context) {
103 if c.conn != nil {
104 c.conn.disconnect()
105 c.conn = nil
106 }
107 }