Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(inputs.influxdb): collect uptime statistics #12493

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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