Skip to content

Commit

Permalink
feat(inputs.influxdb): Collect uptime statistics (#12493)
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj authored Jan 12, 2023
1 parent 5174fb1 commit 40aa899
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
24 changes: 24 additions & 0 deletions plugins/inputs/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ type memstats struct {
GCCPUFraction float64 `json:"GCCPUFraction"`
}

type system struct {
CurrentTime string `json:"currentTime"`
Started string `json:"started"`
Uptime uint64 `json:"uptime"`
}

// Gathers data from a particular URL
// Parameters:
//
Expand Down Expand Up @@ -189,6 +195,24 @@ func (i *InfluxDB) gatherURL(
}

if keyStr, ok := key.(string); ok {
if keyStr == "system" {
var p system
if err := dec.Decode(&p); err != nil {
continue
}

acc.AddFields("influxdb_system",
map[string]interface{}{
"current_time": p.CurrentTime,
"started": p.Started,
"uptime": p.Uptime,
},
map[string]string{
"url": url,
},
)
}

if keyStr == "memstats" {
var m memstats
if err := dec.Decode(&m); err != nil {
Expand Down
21 changes: 19 additions & 2 deletions plugins/inputs/influxdb/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestInfluxDB(t *testing.T) {
var acc testutil.Accumulator
require.NoError(t, acc.GatherError(plugin.Gather))

require.Len(t, acc.Metrics, 34)
require.Len(t, acc.Metrics, 35)

fields := map[string]interface{}{
"heap_inuse": int64(18046976),
Expand Down Expand Up @@ -119,6 +119,13 @@ func TestInfluxDB(t *testing.T) {
}
acc.AssertContainsTaggedFields(t, "influxdb_memstats", fields, tags)

fields = map[string]interface{}{
"current_time": "2023-01-11T16:51:52.723166944Z",
"started": "2023-01-11T16:51:23.355766023Z",
"uptime": uint64(29),
}
acc.AssertContainsTaggedFields(t, "influxdb_system", fields, tags)

acc.AssertContainsTaggedFields(t, "influxdb",
map[string]interface{}{
"n_shards": 1,
Expand Down Expand Up @@ -147,12 +154,22 @@ func TestInfluxDB2(t *testing.T) {
var acc testutil.Accumulator
require.NoError(t, acc.GatherError(plugin.Gather))

require.Len(t, acc.Metrics, 34)
require.Len(t, acc.Metrics, 35)

acc.AssertContainsTaggedFields(t, "influxdb",
map[string]interface{}{
"n_shards": 1,
}, map[string]string{})

fields := map[string]interface{}{
"current_time": "2023-01-11T17:04:59.928454705Z",
"started": "2023-01-11T16:51:23.355766023Z",
"uptime": uint64(816),
}
tags := map[string]string{
"url": fakeInfluxServer.URL + "/endpoint",
}
acc.AssertContainsTaggedFields(t, "influxdb_system", fields, tags)
}

func TestErrorHandling(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions plugins/inputs/influxdb/testdata/influx_return.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@
"numSeries": 1
}
},
"system": {
"currentTime": "2023-01-11T16:51:52.723166944Z",
"started": "2023-01-11T16:51:23.355766023Z",
"uptime": 29
},
"memstats": {
"Alloc": 17034016,
"TotalAlloc": 201739016,
Expand Down
5 changes: 5 additions & 0 deletions plugins/inputs/influxdb/testdata/influx_return2.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@
"numSeries": 1
}
},
"system": {
"currentTime": "2023-01-11T17:04:59.928454705Z",
"started": "2023-01-11T16:51:23.355766023Z",
"uptime": 816
},
"memstats": {
"Alloc": 17034016,
"TotalAlloc": 201739016,
Expand Down

0 comments on commit 40aa899

Please sign in to comment.