diff --git a/surveyor/collector_statz.go b/surveyor/collector_statz.go index c44655d..e54ca7d 100644 --- a/surveyor/collector_statz.go +++ b/surveyor/collector_statz.go @@ -18,7 +18,6 @@ import ( "encoding/json" "fmt" "io" - "slices" "sort" "strconv" "strings" @@ -30,7 +29,6 @@ import ( "github.com/nats-io/nats.go" "github.com/prometheus/client_golang/prometheus" "github.com/sirupsen/logrus" - "golang.org/x/exp/maps" "golang.org/x/sync/singleflight" ) @@ -147,8 +145,6 @@ type StatzCollector struct { collectAccounts bool accStatZeroConn map[string]int natsUp *prometheus.Desc - routeIDRemap map[string]map[uint64]int - gatewayIDRemap map[string]map[uint64]int serverLabels []string serverInfoLabels []string @@ -224,25 +220,11 @@ func (sc *StatzCollector) serverInfoLabelValues(sm *server.ServerInfo) []string } func (sc *StatzCollector) routeLabelValues(sm *server.ServerInfo, rStat *server.RouteStat) []string { - idxS := strconv.FormatUint(rStat.ID, 10) - if byName, ok := sc.routeIDRemap[rStat.Name]; ok { - if idx, ok := byName[rStat.ID]; ok { - idxS = strconv.Itoa(idx) - } - } - - return []string{sm.Cluster, serverName(sm), sm.ID, rStat.Name, idxS} + return []string{sm.Cluster, serverName(sm), sm.ID, rStat.Name, strconv.FormatUint(rStat.ID, 10)} } func (sc *StatzCollector) gatewayLabelValues(sm *server.ServerInfo, gStat *server.GatewayStat) []string { - idxS := strconv.FormatUint(gStat.ID, 10) - if byName, ok := sc.gatewayIDRemap[gStat.Name]; ok { - if idx, ok := byName[gStat.ID]; ok { - idxS = strconv.Itoa(idx) - } - } - - return []string{sm.Cluster, serverName(sm), sm.ID, gStat.Name, idxS} + return []string{sm.Cluster, serverName(sm), sm.ID, gStat.Name, strconv.FormatUint(gStat.ID, 10)} } // Up/Down on servers - look at discovery mechanisms in Prometheus - aging out, how does it work? @@ -405,14 +387,12 @@ func NewStatzCollector(nc *nats.Conn, logger *logrus.Logger, numServers int, ser doneCh: make(chan struct{}, 1), collectAccounts: accounts, accStatZeroConn: make(map[string]int), - routeIDRemap: make(map[string]map[uint64]int), - gatewayIDRemap: make(map[string]map[uint64]int), // TODO - normalize these if possible. Jetstream varies from the other server labels serverLabels: []string{"server_cluster", "server_name", "server_id"}, serverInfoLabels: []string{"server_cluster", "server_name", "server_id", "server_version"}, - routeLabels: []string{"server_cluster", "server_name", "server_id", "server_route_name", "server_route_name_idx"}, - gatewayLabels: []string{"server_cluster", "server_name", "server_id", "server_gateway_name", "server_gateway_name_idx"}, + routeLabels: []string{"server_cluster", "server_name", "server_id", "server_route_name", "server_route_name_id"}, + gatewayLabels: []string{"server_cluster", "server_name", "server_id", "server_gateway_name", "server_gateway_name_id"}, jsServerLabels: []string{"server_id", "server_name", "cluster_name"}, jsServerInfoLabels: []string{"server_name", "server_host", "server_id", "server_cluster", "server_domain", "server_version", "server_jetstream"}, constLabels: constLabels, @@ -1046,13 +1026,6 @@ func (sc *StatzCollector) Collect(ch chan<- prometheus.Metric) { } } - pairs := make([]nameIDPair, len(sm.Stats.Routes)) - for i, rs := range sm.Stats.Routes { - pairs[i].id = rs.ID - pairs[i].name = rs.Name - } - sc.routeIDRemap = remapIDToIdx(pairs, sc.routeIDRemap) - for _, rs := range sm.Stats.Routes { labels = sc.routeLabelValues(&sm.Server, rs) metrics.newCounterMetric(sc.descs.RouteSentMsgs, float64(rs.Sent.Msgs), labels) @@ -1062,13 +1035,6 @@ func (sc *StatzCollector) Collect(ch chan<- prometheus.Metric) { metrics.newGaugeMetric(sc.descs.RoutePending, float64(rs.Pending), labels) } - pairs = make([]nameIDPair, len(sm.Stats.Gateways)) - for i, rs := range sm.Stats.Gateways { - pairs[i].id = rs.ID - pairs[i].name = rs.Name - } - sc.gatewayIDRemap = remapIDToIdx(pairs, sc.gatewayIDRemap) - for _, gw := range sm.Stats.Gateways { labels = sc.gatewayLabelValues(&sm.Server, gw) metrics.newCounterMetric(sc.descs.GatewaySentMsgs, float64(gw.Sent.Msgs), labels) @@ -1234,52 +1200,3 @@ func unmarshalMsg(msg *nats.Msg, v any) error { return json.Unmarshal(data, v) } - -type nameIDPair struct { - name string - id uint64 -} - -func remapIDToIdx(pairs []nameIDPair, existingMapping map[string]map[uint64]int) map[string]map[uint64]int { - newMapping := make(map[string]map[uint64]int) - - // give existing the same idx - for _, rs := range pairs { - newByName, ok := newMapping[rs.name] - if !ok { - newByName = make(map[uint64]int) - newMapping[rs.name] = newByName - } - - existingByName, ok := existingMapping[rs.name] - if !ok { - continue - } - - idx, ok := existingByName[rs.id] - if !ok { - continue - } - - newByName[rs.id] = idx - } - - // assign new ones new idx - for _, path := range pairs { - newByName := newMapping[path.name] - _, ok := newByName[path.id] - if ok { - continue - } - - vals := maps.Values(newByName) - for i := 0; i <= len(vals); i++ { - if !slices.Contains(vals, i) { - newByName[path.id] = i - break - } - } - } - - return newMapping -} diff --git a/surveyor/collector_statz_test.go b/surveyor/collector_statz_test.go deleted file mode 100644 index d55097b..0000000 --- a/surveyor/collector_statz_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package surveyor - -import ( - "reflect" - "testing" -) - -func TestRemapIdToIdx(t *testing.T) { - existingMapping := map[string]map[uint64]int{ - "a": { - 100: 0, - 200: 2, - }, - "b": { - 100: 0, - }, - } - - pairs := []nameIDPair{ - {name: "a", id: 200}, - {name: "a", id: 100}, - {name: "a", id: 300}, - {name: "a", id: 400}, - {name: "b", id: 200}, - {name: "c", id: 200}, - {name: "c", id: 100}, - } - - newMapping := remapIDToIdx(pairs, existingMapping) - expected := map[string]map[uint64]int{ - "a": { - 100: 0, - 200: 2, - 300: 1, - 400: 3, - }, - "b": { - 200: 0, - }, - "c": { - 200: 0, - 100: 1, - }, - } - - if !reflect.DeepEqual(expected, newMapping) { - t.Fatalf("Invalid mapping config; want: %v; got: %v", expected, newMapping) - } -} diff --git a/surveyor/surveyor_test.go b/surveyor/surveyor_test.go index 3235172..87f9426 100644 --- a/surveyor/surveyor_test.go +++ b/surveyor/surveyor_test.go @@ -150,14 +150,14 @@ func TestSurveyor_Basic(t *testing.T) { if !strings.Contains(output, "server_gateway_name") { t.Fatalf("invalid output, missing 'server_gateway_name': %v\n", output) } - if !strings.Contains(output, "server_gateway_name_idx") { - t.Fatalf("invalid output, missing 'server_gateway_name_idx': %v\n", output) + if !strings.Contains(output, "server_gateway_name_id") { + t.Fatalf("invalid output, missing 'server_gateway_name_id': %v\n", output) } if !strings.Contains(output, "server_route_name") { t.Fatalf("invalid output, missing 'server_route_name': %v\n", output) } - if !strings.Contains(output, "server_route_name_idx") { - t.Fatalf("invalid output, missing 'server_route_name_idx': %v\n", output) + if !strings.Contains(output, "server_route_name_id") { + t.Fatalf("invalid output, missing 'server_route_name_id': %v\n", output) } if !strings.Contains(output, "nats_survey_surveyed_count 3") { t.Fatalf("invalid output, missing 'nats_survey_surveyed_count 3': %v\n", output)