master
go 123 lines 2.63 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package fluentd
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/matcher"
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("fluentd", 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:24220",
35 },
36 ClientConfig: web.ClientConfig{
37 Timeout: confopt.Duration(time.Second),
38 },
39 }},
40 activePlugins: make(map[string]bool),
41 charts: charts.Copy(),
42 }
43 }
44
45 type Config struct {
46 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
47 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
48 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
49 web.HTTPConfig `yaml:",inline" json:""`
50 PermitPlugin string `yaml:"permit_plugin_id,omitempty" json:"permit_plugin_id"`
51 }
52
53 type Collector struct {
54 collectorapi.Base
55 Config `yaml:",inline" json:""`
56
57 charts *Charts
58
59 apiClient *apiClient
60
61 permitPlugin matcher.Matcher
62 activePlugins map[string]bool
63 }
64
65 func (c *Collector) Configuration() any {
66 return c.Config
67 }
68
69 func (c *Collector) Init(context.Context) error {
70 if err := c.validateConfig(); err != nil {
71 return fmt.Errorf("invalid config: %v", err)
72 }
73
74 pm, err := c.initPermitPluginMatcher()
75 if err != nil {
76 return fmt.Errorf("init permit_plugin_id: %v", err)
77 }
78 c.permitPlugin = pm
79
80 client, err := c.initApiClient()
81 if err != nil {
82 return fmt.Errorf("init api client: %v", err)
83 }
84 c.apiClient = client
85
86 c.Debugf("using URL %s", c.URL)
87 c.Debugf("using timeout: %s", c.Timeout.Duration())
88
89 return nil
90 }
91
92 func (c *Collector) Check(context.Context) error {
93 mx, err := c.collect()
94 if err != nil {
95 return err
96 }
97 if len(mx) == 0 {
98 return errors.New("no metrics collected")
99
100 }
101 return nil
102 }
103
104 func (c *Collector) Charts() *Charts {
105 return c.charts
106 }
107
108 func (c *Collector) Collect(context.Context) map[string]int64 {
109 mx, err := c.collect()
110
111 if err != nil {
112 c.Error(err)
113 return nil
114 }
115
116 return mx
117 }
118
119 func (c *Collector) Cleanup(context.Context) {
120 if c.apiClient != nil && c.apiClient.httpClient != nil {
121 c.apiClient.httpClient.CloseIdleConnections()
122 }
123 }