master
go 100 lines 1.99 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package uwsgi
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("uwsgi", 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:1717",
30 Timeout: confopt.Duration(time.Second * 1),
31 },
32 charts: charts.Copy(),
33 seenWorkers: make(map[int]bool),
34 }
35 }
36
37 type Config struct {
38 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
39 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
40 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
41 Address string `yaml:"address" json:"address"`
42 Timeout confopt.Duration `yaml:"timeout" json:"timeout"`
43 }
44
45 type Collector struct {
46 collectorapi.Base
47 Config `yaml:",inline" json:""`
48
49 charts *collectorapi.Charts
50
51 conn uwsgiConn
52
53 seenWorkers map[int]bool
54 }
55
56 func (c *Collector) Configuration() any {
57 return c.Config
58 }
59
60 func (c *Collector) Init(context.Context) error {
61 if c.Address == "" {
62 return errors.New("config: 'address' not set")
63 }
64
65 c.conn = newUwsgiConn(c.Config)
66
67 return nil
68 }
69
70 func (c *Collector) Check(context.Context) error {
71 mx, err := c.collect()
72 if err != nil {
73 return err
74 }
75
76 if len(mx) == 0 {
77 return errors.New("no metrics collected")
78 }
79
80 return nil
81 }
82
83 func (c *Collector) Charts() *collectorapi.Charts {
84 return c.charts
85 }
86
87 func (c *Collector) Collect(context.Context) map[string]int64 {
88 mx, err := c.collect()
89 if err != nil {
90 c.Error(err)
91 }
92
93 if len(mx) == 0 {
94 return nil
95 }
96
97 return mx
98 }
99
100 func (c *Collector) Cleanup(context.Context) {}