master
go 128 lines 2.84 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package envoy
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "fmt"
10 "time"
11
12 "github.com/netdata/netdata/go/plugins/pkg/confopt"
13 "github.com/netdata/netdata/go/plugins/pkg/prometheus"
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("envoy", 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://127.0.0.1:9091/stats/prometheus",
35 },
36 ClientConfig: web.ClientConfig{
37 Timeout: confopt.Duration(time.Second),
38 },
39 },
40 },
41
42 charts: &collectorapi.Charts{},
43
44 servers: make(map[string]bool),
45 clusterMgrs: make(map[string]bool),
46 clusterUpstream: make(map[string]bool),
47 listenerMgrs: make(map[string]bool),
48 listenerAdminDownstream: make(map[string]bool),
49 listenerDownstream: make(map[string]bool),
50 }
51 }
52
53 type Config struct {
54 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
55 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
56 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
57 web.HTTPConfig `yaml:",inline" json:""`
58 }
59
60 type Collector struct {
61 collectorapi.Base
62 Config `yaml:",inline" json:""`
63
64 charts *collectorapi.Charts
65
66 prom prometheus.Prometheus
67
68 servers map[string]bool
69 clusterMgrs map[string]bool
70 clusterUpstream map[string]bool
71 listenerMgrs map[string]bool
72 listenerAdminDownstream map[string]bool
73 listenerDownstream map[string]bool
74 }
75
76 func (c *Collector) Configuration() any {
77 return c.Config
78 }
79
80 func (c *Collector) Init(context.Context) error {
81 if err := c.validateConfig(); err != nil {
82 return fmt.Errorf("config validation: %v", err)
83 }
84
85 prom, err := c.initPrometheusClient()
86 if err != nil {
87 return fmt.Errorf("init Prometheus client: %v", err)
88 }
89 c.prom = prom
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 }
103 return nil
104 }
105
106 func (c *Collector) Charts() *collectorapi.Charts {
107 return c.charts
108 }
109
110 func (c *Collector) Collect(context.Context) map[string]int64 {
111 mx, err := c.collect()
112 if err != nil {
113 c.Error(err)
114 }
115
116 if len(mx) == 0 {
117 return nil
118 }
119 return mx
120 }
121
122 func (c *Collector) Cleanup(context.Context) {
123 if c.prom == nil || c.prom.HTTPClient() == nil {
124 return
125 }
126
127 c.prom.HTTPClient().CloseIdleConnections()
128 }