@cryptotaxi247 / netdata-1 / commits / 0a9eb3c39

improvement(go.d/nats): add cluster_name label and jetstream status chart (#19303)

Ilya Mashchenko committed Dec 31, 2024 at 12:04 UTC 0a9eb3c39daa78345ce729c631885363b6a32f23
4 files changed +45 -12
src/go/plugin/go.d/collector/nats/charts.go
+24
@@ -25,6 +25,7 @@ const (
25 prioServerMemoryUsage
26 prioServerUptime
27
28 + prioJetStreamStatus
29 prioJetStreamStreams
30 prioJetStreamConsumers
31 prioJetStreamBytes
@@ -203,6 +204,7 @@ var httpEndpointRequestsChartTmpl = module.Chart{
204 }
205
206 var jetStreamCharts = module.Charts{
207 + jetStreamStatus.Copy(),
208 jetStreamStreams.Copy(),
209 jetStreamStreamsStorageBytes.Copy(),
210 jetStreamStreamsStorageMessages.Copy(),
@@ -215,6 +217,18 @@ var jetStreamCharts = module.Charts{
217 }
218
219 var (
220 + jetStreamStatus = module.Chart{
221 + ID: "jetstream_status",
222 + Title: "JetStream Status",
223 + Units: "status",
224 + Fam: "jstream streams",
225 + Ctx: "nats.jetstream_status",
226 + Priority: prioJetStreamStatus,
227 + Dims: module.Dims{
228 + {ID: "jsz_enabled", Name: "enabled"},
229 + {ID: "jsz_disabled", Name: "disabled"},
230 + },
231 + }
232 jetStreamStreams = module.Chart{
233 ID: "jetstream_streams",
234 Title: "JetStream Streams",
@@ -654,6 +668,8 @@ func (c *Collector) addServerCharts() {
668 for _, chart := range *charts {
669 chart.Labels = []module.Label{
670 {Key: "server_id", Value: c.srvMeta.id},
671 + {Key: "server_name", Value: c.srvMeta.name},
672 + {Key: "cluster_name", Value: c.srvMeta.clusterName},
673 }
674 }
675
@@ -669,6 +685,8 @@ func (c *Collector) addAccountCharts(acc *accCacheEntry) {
685 chart.ID = fmt.Sprintf(chart.ID, acc.accName)
686 chart.Labels = []module.Label{
687 {Key: "server_id", Value: c.srvMeta.id},
688 + {Key: "server_name", Value: c.srvMeta.name},
689 + {Key: "cluster_name", Value: c.srvMeta.clusterName},
690 {Key: "account", Value: acc.accName},
691 }
692 for _, dim := range chart.Dims {
@@ -693,6 +711,8 @@ func (c *Collector) addRouteCharts(route *routeCacheEntry) {
711 chart.ID = fmt.Sprintf(chart.ID, route.rid)
712 chart.Labels = []module.Label{
713 {Key: "server_id", Value: c.srvMeta.id},
714 + {Key: "server_name", Value: c.srvMeta.name},
715 + {Key: "cluster_name", Value: c.srvMeta.clusterName},
716 {Key: "route_id", Value: strconv.FormatUint(route.rid, 10)},
717 {Key: "remote_id", Value: route.remoteId},
718 }
@@ -725,6 +745,8 @@ func (c *Collector) addGatewayConnCharts(gwConn *gwConnCacheEntry, isInbound boo
745 chart.Ctx = fmt.Sprintf(chart.Ctx, direction)
746 chart.Labels = []module.Label{
747 {Key: "server_id", Value: c.srvMeta.id},
748 + {Key: "server_name", Value: c.srvMeta.name},
749 + {Key: "cluster_name", Value: c.srvMeta.clusterName},
750 {Key: "gateway", Value: gwConn.gwName},
751 {Key: "remote_gateway", Value: gwConn.rgwName},
752 }
@@ -755,6 +777,8 @@ func (c *Collector) addLeafCharts(leaf *leafCacheEntry) {
777 chart.ID = cleanChartID(chart.ID)
778 chart.Labels = []module.Label{
779 {Key: "server_id", Value: c.srvMeta.id},
780 + {Key: "server_name", Value: c.srvMeta.name},
781 + {Key: "cluster_name", Value: c.srvMeta.clusterName},
782 {Key: "remote_name", Value: leaf.leafName},
783 {Key: "account", Value: leaf.account},
784 {Key: "ip", Value: leaf.ip},
src/go/plugin/go.d/collector/nats/collect.go
+16 -10
@@ -15,12 +15,9 @@ import (
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 {
18 + if err := c.getServerMeta(); err != nil {
19 return nil, err
20 }
22 - c.srvMeta.id = id
23 - c.srvMeta.name = name
21 }
22
23 mx := make(map[string]int64)
@@ -54,22 +51,29 @@ func (c *Collector) collect() (map[string]int64, error) {
51 return mx, nil
52 }
53
57 -func (c *Collector) getServerMeta() (srvId, srvName string, err error) {
54 +func (c *Collector) getServerMeta() error {
55 req, err := web.NewHTTPRequestWithPath(c.RequestConfig, urlPathVarz)
56 if err != nil {
60 - return "", "", err
57 + return err
58 }
59
60 var resp struct {
64 - ID string `json:"server_id"`
65 - Name string `json:"server_name"`
61 + ID string `json:"server_id"`
62 + Name string `json:"server_name"`
63 + Cluster struct {
64 + Name string `json:"name"`
65 + } `json:"cluster"`
66 }
67
68 if err := web.DoHTTP(c.httpClient).RequestJSON(req, &resp); err != nil {
69 - return "", "", err
69 + return err
70 }
71
72 - return resp.ID, resp.Name, nil
72 + c.srvMeta.id = resp.ID
73 + c.srvMeta.name = resp.Name
74 + c.srvMeta.clusterName = resp.Cluster.Name
75 +
76 + return nil
77 }
78
79 func (c *Collector) collectHealthz(mx map[string]int64) error {
@@ -274,6 +278,8 @@ func (c *Collector) collectJsz(mx map[string]int64) error {
278 return err
279 }
280
281 + mx["jsz_disabled"] = metrix.Bool(resp.Disabled)
282 + mx["jsz_enabled"] = metrix.Bool(!resp.Disabled)
283 mx["jsz_streams"] = int64(resp.Streams)
284 mx["jsz_consumers"] = int64(resp.Consumers)
285 mx["jsz_bytes"] = int64(resp.Bytes)
src/go/plugin/go.d/collector/nats/collector.go
+3 -2
@@ -64,8 +64,9 @@ type Collector struct {
64 cache *cache
65
66 srvMeta struct {
67 - id string
68 - name string
67 + id string
68 + name string
69 + clusterName string
70 }
71 onceAddSrvCharts *sync.Once
72 }
src/go/plugin/go.d/collector/nats/collector_test.go
+2
@@ -203,6 +203,8 @@ func TestCollector_Collect(t *testing.T) {
203 "jsz_api_total": 936916,
204 "jsz_bytes": 114419224,
205 "jsz_consumers": 9,
206 + "jsz_disabled": 0,
207 + "jsz_enabled": 1,
208 "jsz_memory_used": 128,
209 "jsz_messages": 5670,
210 "jsz_store_used": 114419224,