Skip to content

Commit

Permalink
improvement(go.d/nats): add cluster_name label and jetstream status c…
Browse files Browse the repository at this point in the history
…hart (netdata#19303)
  • Loading branch information
ilyam8 authored Dec 31, 2024
1 parent e8e9f76 commit 0a9eb3c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
24 changes: 24 additions & 0 deletions src/go/plugin/go.d/collector/nats/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
prioServerMemoryUsage
prioServerUptime

prioJetStreamStatus
prioJetStreamStreams
prioJetStreamConsumers
prioJetStreamBytes
Expand Down Expand Up @@ -203,6 +204,7 @@ var httpEndpointRequestsChartTmpl = module.Chart{
}

var jetStreamCharts = module.Charts{
jetStreamStatus.Copy(),
jetStreamStreams.Copy(),
jetStreamStreamsStorageBytes.Copy(),
jetStreamStreamsStorageMessages.Copy(),
Expand All @@ -215,6 +217,18 @@ var jetStreamCharts = module.Charts{
}

var (
jetStreamStatus = module.Chart{
ID: "jetstream_status",
Title: "JetStream Status",
Units: "status",
Fam: "jstream streams",
Ctx: "nats.jetstream_status",
Priority: prioJetStreamStatus,
Dims: module.Dims{
{ID: "jsz_enabled", Name: "enabled"},
{ID: "jsz_disabled", Name: "disabled"},
},
}
jetStreamStreams = module.Chart{
ID: "jetstream_streams",
Title: "JetStream Streams",
Expand Down Expand Up @@ -654,6 +668,8 @@ func (c *Collector) addServerCharts() {
for _, chart := range *charts {
chart.Labels = []module.Label{
{Key: "server_id", Value: c.srvMeta.id},
{Key: "server_name", Value: c.srvMeta.name},
{Key: "cluster_name", Value: c.srvMeta.clusterName},
}
}

Expand All @@ -669,6 +685,8 @@ func (c *Collector) addAccountCharts(acc *accCacheEntry) {
chart.ID = fmt.Sprintf(chart.ID, acc.accName)
chart.Labels = []module.Label{
{Key: "server_id", Value: c.srvMeta.id},
{Key: "server_name", Value: c.srvMeta.name},
{Key: "cluster_name", Value: c.srvMeta.clusterName},
{Key: "account", Value: acc.accName},
}
for _, dim := range chart.Dims {
Expand All @@ -693,6 +711,8 @@ func (c *Collector) addRouteCharts(route *routeCacheEntry) {
chart.ID = fmt.Sprintf(chart.ID, route.rid)
chart.Labels = []module.Label{
{Key: "server_id", Value: c.srvMeta.id},
{Key: "server_name", Value: c.srvMeta.name},
{Key: "cluster_name", Value: c.srvMeta.clusterName},
{Key: "route_id", Value: strconv.FormatUint(route.rid, 10)},
{Key: "remote_id", Value: route.remoteId},
}
Expand Down Expand Up @@ -725,6 +745,8 @@ func (c *Collector) addGatewayConnCharts(gwConn *gwConnCacheEntry, isInbound boo
chart.Ctx = fmt.Sprintf(chart.Ctx, direction)
chart.Labels = []module.Label{
{Key: "server_id", Value: c.srvMeta.id},
{Key: "server_name", Value: c.srvMeta.name},
{Key: "cluster_name", Value: c.srvMeta.clusterName},
{Key: "gateway", Value: gwConn.gwName},
{Key: "remote_gateway", Value: gwConn.rgwName},
}
Expand Down Expand Up @@ -755,6 +777,8 @@ func (c *Collector) addLeafCharts(leaf *leafCacheEntry) {
chart.ID = cleanChartID(chart.ID)
chart.Labels = []module.Label{
{Key: "server_id", Value: c.srvMeta.id},
{Key: "server_name", Value: c.srvMeta.name},
{Key: "cluster_name", Value: c.srvMeta.clusterName},
{Key: "remote_name", Value: leaf.leafName},
{Key: "account", Value: leaf.account},
{Key: "ip", Value: leaf.ip},
Expand Down
26 changes: 16 additions & 10 deletions src/go/plugin/go.d/collector/nats/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ import (

func (c *Collector) collect() (map[string]int64, error) {
if c.srvMeta.id == "" {
id, name, err := c.getServerMeta()
if err != nil {
if err := c.getServerMeta(); err != nil {
return nil, err
}
c.srvMeta.id = id
c.srvMeta.name = name
}

mx := make(map[string]int64)
Expand Down Expand Up @@ -54,22 +51,29 @@ func (c *Collector) collect() (map[string]int64, error) {
return mx, nil
}

func (c *Collector) getServerMeta() (srvId, srvName string, err error) {
func (c *Collector) getServerMeta() error {
req, err := web.NewHTTPRequestWithPath(c.RequestConfig, urlPathVarz)
if err != nil {
return "", "", err
return err
}

var resp struct {
ID string `json:"server_id"`
Name string `json:"server_name"`
ID string `json:"server_id"`
Name string `json:"server_name"`
Cluster struct {
Name string `json:"name"`
} `json:"cluster"`
}

if err := web.DoHTTP(c.httpClient).RequestJSON(req, &resp); err != nil {
return "", "", err
return err
}

return resp.ID, resp.Name, nil
c.srvMeta.id = resp.ID
c.srvMeta.name = resp.Name
c.srvMeta.clusterName = resp.Cluster.Name

return nil
}

func (c *Collector) collectHealthz(mx map[string]int64) error {
Expand Down Expand Up @@ -274,6 +278,8 @@ func (c *Collector) collectJsz(mx map[string]int64) error {
return err
}

mx["jsz_disabled"] = metrix.Bool(resp.Disabled)
mx["jsz_enabled"] = metrix.Bool(!resp.Disabled)
mx["jsz_streams"] = int64(resp.Streams)
mx["jsz_consumers"] = int64(resp.Consumers)
mx["jsz_bytes"] = int64(resp.Bytes)
Expand Down
5 changes: 3 additions & 2 deletions src/go/plugin/go.d/collector/nats/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ type Collector struct {
cache *cache

srvMeta struct {
id string
name string
id string
name string
clusterName string
}
onceAddSrvCharts *sync.Once
}
Expand Down
2 changes: 2 additions & 0 deletions src/go/plugin/go.d/collector/nats/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ func TestCollector_Collect(t *testing.T) {
"jsz_api_total": 936916,
"jsz_bytes": 114419224,
"jsz_consumers": 9,
"jsz_disabled": 0,
"jsz_enabled": 1,
"jsz_memory_used": 128,
"jsz_messages": 5670,
"jsz_store_used": 114419224,
Expand Down

0 comments on commit 0a9eb3c

Please sign in to comment.