master
go 116 lines 2.34 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package logstash
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "fmt"
10 "net/http"
11 "time"
12
13 "github.com/netdata/netdata/go/plugins/pkg/confopt"
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("logstash", 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://localhost:9600",
35 },
36 ClientConfig: web.ClientConfig{
37 Timeout: confopt.Duration(time.Second),
38 },
39 },
40 },
41 charts: charts.Copy(),
42 pipelines: make(map[string]bool),
43 }
44 }
45
46 type Config struct {
47 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
48 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
49 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
50 web.HTTPConfig `yaml:",inline" json:""`
51 }
52
53 type Collector struct {
54 collectorapi.Base
55 Config `yaml:",inline" json:""`
56
57 charts *collectorapi.Charts
58
59 httpClient *http.Client
60
61 pipelines map[string]bool
62 }
63
64 func (c *Collector) Configuration() any {
65 return c.Config
66 }
67
68 func (c *Collector) Init(context.Context) error {
69 if c.URL == "" {
70 return errors.New("config: 'url' cannot be empty")
71 }
72
73 httpClient, err := web.NewHTTPClient(c.ClientConfig)
74 if err != nil {
75 return fmt.Errorf("init HTTP client: %v", err)
76 }
77 c.httpClient = httpClient
78
79 c.Debugf("using URL %s", c.URL)
80 c.Debugf("using timeout: %s", c.Timeout.Duration())
81
82 return nil
83 }
84
85 func (c *Collector) Check(context.Context) error {
86 mx, err := c.collect()
87 if err != nil {
88 return err
89 }
90 if len(mx) == 0 {
91 return errors.New("no metrics collected")
92 }
93 return nil
94 }
95
96 func (c *Collector) Charts() *collectorapi.Charts {
97 return c.charts
98 }
99
100 func (c *Collector) Collect(context.Context) map[string]int64 {
101 mx, err := c.collect()
102 if err != nil {
103 c.Error(err)
104 }
105
106 if len(mx) == 0 {
107 return nil
108 }
109 return mx
110 }
111
112 func (c *Collector) Cleanup(context.Context) {
113 if c.httpClient != nil {
114 c.httpClient.CloseIdleConnections()
115 }
116 }