master
go 132 lines 2.76 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package traefik
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("traefik", 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:8082/metrics",
35 },
36 ClientConfig: web.ClientConfig{
37 Timeout: confopt.Duration(time.Second),
38 },
39 },
40 },
41
42 charts: &collectorapi.Charts{},
43 checkMetrics: true,
44 cache: &cache{
45 entrypoints: make(map[string]*cacheEntrypoint),
46 },
47 }
48 }
49
50 type Config struct {
51 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
52 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
53 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
54 web.HTTPConfig `yaml:",inline" json:""`
55 }
56
57 type (
58 Collector struct {
59 collectorapi.Base
60 Config `yaml:",inline" json:""`
61
62 charts *collectorapi.Charts
63
64 prom prometheus.Prometheus
65
66 checkMetrics bool
67 cache *cache
68 }
69 cache struct {
70 entrypoints map[string]*cacheEntrypoint
71 }
72 cacheEntrypoint struct {
73 name, proto string
74 requests *collectorapi.Chart
75 reqDur *collectorapi.Chart
76 reqDurData map[string]cacheEntrypointReqDur
77 openConn *collectorapi.Chart
78 openConnMethods map[string]bool
79 }
80 cacheEntrypointReqDur struct {
81 prev, cur struct{ reqs, secs float64 }
82 seen bool
83 }
84 )
85
86 func (c *Collector) Configuration() any {
87 return c.Config
88 }
89
90 func (c *Collector) Init(context.Context) error {
91 if err := c.validateConfig(); err != nil {
92 return fmt.Errorf("config validation: %v", err)
93 }
94
95 prom, err := c.initPrometheusClient()
96 if err != nil {
97 return fmt.Errorf("prometheus client initialization: %v", err)
98 }
99 c.prom = prom
100
101 return nil
102 }
103
104 func (c *Collector) Check(context.Context) error {
105 mx, err := c.collect()
106 if err != nil {
107 return err
108 }
109 if len(mx) == 0 {
110 return errors.New("no metrics collected")
111 }
112 return nil
113 }
114
115 func (c *Collector) Charts() *collectorapi.Charts {
116 return c.charts
117 }
118
119 func (c *Collector) Collect(context.Context) map[string]int64 {
120 mx, err := c.collect()
121 if err != nil {
122 c.Error(err)
123 return nil
124 }
125
126 if len(mx) == 0 {
127 return nil
128 }
129 return mx
130 }
131
132 func (c *Collector) Cleanup(context.Context) {}