@cryptotaxi247 / netdata-1 / commits / 09b11d221

improvement(go.d/nats): add server_id label (#19280)

Ilya Mashchenko committed Dec 23, 2024 at 21:42 UTC 09b11d2213a936a56a81f631f20c6b3c18a81196
4 files changed +69 -3
src/go/plugin/go.d/collector/nats/charts.go
+19
@@ -392,6 +392,8 @@ var (
392 )
393
394 func (c *Collector) updateCharts() {
395 + c.onceAddSrvCharts.Do(c.addServerCharts)
396 +
397 maps.DeleteFunc(c.cache.accounts, func(_ string, acc *accCacheEntry) bool {
398 if !acc.updated {
399 c.removeAccountCharts(acc)
@@ -444,12 +446,27 @@ func (c *Collector) updateCharts() {
446 })
447 }
448
449 +func (c *Collector) addServerCharts() {
450 + charts := serverCharts.Copy()
451 +
452 + for _, chart := range *charts {
453 + chart.Labels = []module.Label{
454 + {Key: "server_id", Value: c.srvMeta.id},
455 + }
456 + }
457 +
458 + if err := c.Charts().Add(*charts...); err != nil {
459 + c.Warningf("failed to add server charts: %v", err)
460 + }
461 +}
462 +
463 func (c *Collector) addAccountCharts(acc *accCacheEntry) {
464 charts := accountChartsTmpl.Copy()
465
466 for _, chart := range *charts {
467 chart.ID = fmt.Sprintf(chart.ID, acc.accName)
468 chart.Labels = []module.Label{
469 + {Key: "server_id", Value: c.srvMeta.id},
470 {Key: "account", Value: acc.accName},
471 }
472 for _, dim := range chart.Dims {
@@ -473,6 +490,7 @@ func (c *Collector) addRouteCharts(route *routeCacheEntry) {
490 for _, chart := range *charts {
491 chart.ID = fmt.Sprintf(chart.ID, route.rid)
492 chart.Labels = []module.Label{
493 + {Key: "server_id", Value: c.srvMeta.id},
494 {Key: "route_id", Value: strconv.FormatUint(route.rid, 10)},
495 {Key: "remote_id", Value: route.remoteId},
496 }
@@ -504,6 +522,7 @@ func (c *Collector) addGatewayConnCharts(gwConn *gwConnCacheEntry, isInbound boo
522 chart.Title = fmt.Sprintf(chart.Title, cases.Title(language.English, cases.Compact).String(direction))
523 chart.Ctx = fmt.Sprintf(chart.Ctx, direction)
524 chart.Labels = []module.Label{
525 + {Key: "server_id", Value: c.srvMeta.id},
526 {Key: "gateway", Value: gwConn.gwName},
527 {Key: "remote_gateway", Value: gwConn.rgwName},
528 }
src/go/plugin/go.d/collector/nats/collect.go
+27
@@ -14,6 +14,15 @@ import (
14 )
15
16 func (c *Collector) collect() (map[string]int64, error) {
17 + if c.srvMeta.id == "" {
18 + id, name, err := c.getServerMeta()
19 + if err != nil {
20 + return nil, err
21 + }
22 + c.srvMeta.id = id
23 + c.srvMeta.name = name
24 + }
25 +
26 mx := make(map[string]int64)
27
28 c.cache.resetUpdated()
@@ -39,6 +48,24 @@ func (c *Collector) collect() (map[string]int64, error) {
48 return mx, nil
49 }
50
51 +func (c *Collector) getServerMeta() (srvId, srvName string, err error) {
52 + req, err := web.NewHTTPRequestWithPath(c.RequestConfig, urlPathVarz)
53 + if err != nil {
54 + return "", "", err
55 + }
56 +
57 + var resp struct {
58 + ID string `json:"server_id"`
59 + Name string `json:"server_name"`
60 + }
61 +
62 + if err := web.DoHTTP(c.httpClient).RequestJSON(req, &resp); err != nil {
63 + return "", "", err
64 + }
65 +
66 + return resp.ID, resp.Name, nil
67 +}
68 +
69 func (c *Collector) collectHealthz(mx map[string]int64) error {
70 req, err := web.NewHTTPRequestWithPath(c.RequestConfig, urlPathHealthz)
71 if err != nil {
src/go/plugin/go.d/collector/nats/collector.go
+10 -2
@@ -8,6 +8,7 @@ import (
8 "errors"
9 "fmt"
10 "net/http"
11 + "sync"
12 "time"
13
14 "github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
@@ -39,8 +40,9 @@ func New() *Collector {
40 },
41 HealthzCheck: "default",
42 },
42 - charts: serverCharts.Copy(),
43 - cache: newCache(),
43 + charts: &module.Charts{},
44 + cache: newCache(),
45 + onceAddSrvCharts: &sync.Once{},
46 }
47 }
48
@@ -60,6 +62,12 @@ type Collector struct {
62 httpClient *http.Client
63
64 cache *cache
65 +
66 + srvMeta struct {
67 + id string
68 + name string
69 + }
70 + onceAddSrvCharts *sync.Once
71 }
72
73 func (c *Collector) Configuration() any {
src/go/plugin/go.d/collector/nats/metadata.yaml
+13 -1
@@ -185,7 +185,9 @@ modules:
185 scopes:
186 - name: server
187 description: These metrics refer to NATS servers.
188 - labels: []
188 + labels:
189 + - name: server_id
190 + description: "A unique identifier for a server within the NATS cluster."
191 metrics:
192 - name: nats.server_traffic
193 description: Server Traffic
@@ -241,6 +243,8 @@ modules:
243 - name: http endpoint
244 description: These metrics refer to HTTP endpoints.
245 labels:
246 + - name: server_id
247 + description: "A unique identifier for a server within the NATS cluster."
248 - name: http_endpoint
249 description: "HTTP endpoint path."
250 metrics:
@@ -253,6 +257,8 @@ modules:
257 - name: account
258 description: These metrics refer to [Accounts](https://docs.nats.io/running-a-nats-service/nats_admin/monitoring#account-statistics).
259 labels:
260 + - name: server_id
261 + description: "A unique identifier for a server within the NATS cluster."
262 - name: account
263 description: "Account name."
264 metrics:
@@ -303,6 +309,8 @@ modules:
309 - name: route
310 description: These metrics refer to [Routes](https://docs.nats.io/running-a-nats-service/nats_admin/monitoring#route-information).
311 labels:
312 + - name: server_id
313 + description: "A unique identifier for a server within the NATS cluster."
314 - name: route_id
315 description: "A unique identifier for a route within the NATS cluster."
316 - name: remote_id
@@ -331,6 +339,8 @@ modules:
339 - name: inbound gateway connection
340 description: These metrics refer to [Inbound Gateway Connections](https://docs.nats.io/running-a-nats-service/nats_admin/monitoring#gateway-information).
341 labels:
342 + - name: server_id
343 + description: "A unique identifier for a server within the NATS cluster."
344 - name: gateway
345 description: "The name of the local gateway."
346 - name: remote_gateway
@@ -367,6 +377,8 @@ modules:
377 - name: outbound gateway connection
378 description: These metrics refer to [Outbound Gateway Connections](https://docs.nats.io/running-a-nats-service/nats_admin/monitoring#gateway-information).
379 labels:
380 + - name: server_id
381 + description: "A unique identifier for a server within the NATS cluster."
382 - name: gateway
383 description: "The name of the local gateway."
384 - name: remote_gateway