master
go 123 lines 2.64 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package dcgm
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("dcgm", collectorapi.Creator{
23 JobConfigSchema: configSchema,
24 Defaults: collectorapi.Defaults{
25 UpdateEvery: 30,
26 },
27 Create: func() collectorapi.CollectorV1 { return New() },
28 Config: func() any { return &Config{} },
29 })
30 }
31
32 func New() *Collector {
33 return &Collector{
34 Config: Config{
35 HTTPConfig: web.HTTPConfig{
36 RequestConfig: web.RequestConfig{
37 URL: "http://127.0.0.1:9400/metrics",
38 },
39 ClientConfig: web.ClientConfig{
40 Timeout: confopt.Duration(time.Second * 10),
41 },
42 },
43 MaxTS: 2000,
44 MaxTSPerMetric: 200,
45 },
46 charts: &collectorapi.Charts{},
47 cache: newCache(),
48 checkMetrics: true,
49 }
50 }
51
52 type Config struct {
53 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
54 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
55 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
56 web.HTTPConfig `yaml:",inline" json:""`
57 MaxTS int `yaml:"max_time_series" json:"max_time_series"`
58 MaxTSPerMetric int `yaml:"max_time_series_per_metric" json:"max_time_series_per_metric"`
59 }
60
61 type Collector struct {
62 collectorapi.Base
63 Config `yaml:",inline" json:""`
64
65 charts *collectorapi.Charts
66 prom prometheus.Prometheus
67
68 cache *cache
69 checkMetrics bool
70 }
71
72 func (c *Collector) Configuration() any {
73 return c.Config
74 }
75
76 func (c *Collector) Init(context.Context) error {
77 if err := c.validateConfig(); err != nil {
78 return fmt.Errorf("config validation: %v", err)
79 }
80
81 prom, err := c.initPrometheusClient()
82 if err != nil {
83 return fmt.Errorf("init prometheus client: %v", err)
84 }
85 c.prom = prom
86
87 return nil
88 }
89
90 func (c *Collector) Check(context.Context) error {
91 mx, err := c.collect()
92 if err != nil {
93 return err
94 }
95 if len(mx) == 0 {
96 return errors.New("no metrics collected")
97 }
98 return nil
99 }
100
101 func (c *Collector) Charts() *collectorapi.Charts {
102 return c.charts
103 }
104
105 func (c *Collector) Collect(context.Context) map[string]int64 {
106 mx, err := c.collect()
107 if err != nil {
108 c.Error(err)
109 return nil
110 }
111
112 if len(mx) == 0 {
113 return nil
114 }
115
116 return mx
117 }
118
119 func (c *Collector) Cleanup(context.Context) {
120 if c.prom != nil && c.prom.HTTPClient() != nil {
121 c.prom.HTTPClient().CloseIdleConnections()
122 }
123 }