Skip to content

Commit

Permalink
[ADDED] Time metrics to the exporter (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonberrutti authored Nov 8, 2023
1 parent 6ec93ee commit f6cb9e6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
43 changes: 32 additions & 11 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type CollectedServer struct {
type metric struct {
path []string
metric interface{}
time bool
}

// NATSCollector collects NATS metrics
Expand Down Expand Up @@ -240,8 +241,12 @@ func (nc *NATSCollector) collectStatsFromRequests(
case float64: // json only has floats
m.WithLabelValues(id).Set(v)
case string:
m.Reset()
m.With(prometheus.Labels{"server_id": id, "value": v}).Set(1)
if stat.time {
m.WithLabelValues(id).Set(parseDateString(v))
} else {
m.Reset()
m.With(prometheus.Labels{"server_id": id, "value": v}).Set(1)
}
default:
Debugf("value %s no longer a float", key, id, v)
}
Expand Down Expand Up @@ -332,14 +337,18 @@ func (nc *NATSCollector) objectToMetrics(response map[string]interface{}, namesp
"gateway_tls_timeout": {},
"gateway_connect_retries": {},
}

labelKeys := map[string]struct{}{
"server_id": {},
"server_name": {},
"version": {},
"domain": {},
"leader": {},
"name": {},
"server_id": {},
"server_name": {},
"version": {},
"domain": {},
"leader": {},
"name": {},
"start": {},
"config_load_time": {},
}

for k := range response {
fqn, path := fqName(k, prefix...)
if _, ok := skipFQN[fqn]; ok {
Expand All @@ -360,9 +369,21 @@ func (nc *NATSCollector) objectToMetrics(response map[string]interface{}, namesp
if _, ok := labelKeys[k]; !ok {
break
}
nc.Stats[fqn] = metric{
path: path,
metric: newLabelGauge(nc.system, nc.endpoint, fqn, "", namespace, "value"),

// Check if the value is a valid time string.
// Go JSONMarshal time.Time in RFC3339Nano format.
_, err := time.Parse(time.RFC3339Nano, v)
if err == nil {
nc.Stats[fqn] = metric{
path: path,
metric: newPrometheusGaugeVec(nc.system, nc.endpoint, fqn, "", namespace),
time: true,
}
} else {
nc.Stats[fqn] = metric{
path: path,
metric: newLabelGauge(nc.system, nc.endpoint, fqn, "", namespace, "value"),
}
}
case map[string]interface{}:
// recurse and flatten
Expand Down
25 changes: 25 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,31 @@ func TestVarz(t *testing.T) {
verifyCollector(CoreSystem, url, "varz", cases, t)
}

func TestStartAndConfigLoadTimeVarz(t *testing.T) {
s := pet.RunServer()
defer s.Shutdown()

varz, err := s.Varz(nil)
if err != nil {
t.Fatal(err)
}

url := fmt.Sprintf("http://localhost:%d/", pet.MonitorPort)

nc := pet.CreateClientConnSubscribeAndPublish(t)
defer nc.Close()

// see if we get the same stats as the original monitor testing code.
// just for our monitoring_port

cases := map[string]float64{
"gnatsd_varz_start": float64(varz.Start.UnixMilli()),
"gnatsd_varz_config_load_time": float64(varz.ConfigLoadTime.UnixMilli()),
}

verifyCollector(CoreSystem, url, "varz", cases, t)
}

func TestConnz(t *testing.T) {
s := pet.RunServer()
defer s.Shutdown()
Expand Down

0 comments on commit f6cb9e6

Please sign in to comment.