Skip to content

Commit

Permalink
Merge pull request #12 from pacmansyu/main
Browse files Browse the repository at this point in the history
Create new metric with last connection timestamp
  • Loading branch information
f100024 authored Jun 2, 2021
2 parents 2d8f82a + 0868a13 commit 6106b56
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
30 changes: 28 additions & 2 deletions collector/rest_stats_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
Expand Down Expand Up @@ -58,7 +59,17 @@ func NewStatsDeviceReport(logger log.Logger, client *http.Client, url *url.URL,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "last_connection_duration"),
"Duration of last connection with remote device in seconds.",
[]string{"deviceID", "lastSeen"}, nil),
[]string{"deviceID"}, nil),
Value: func(v float64) float64 {
return v
},
},
"last_connection_timestamp": {
Type: prometheus.GaugeValue,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "last_connection_timestamp"),
"Timestamp since last connection with remote device expressed in Unix epoch",
[]string{"deviceID"}, nil),
Value: func(v float64) float64 {
return v
},
Expand Down Expand Up @@ -147,7 +158,22 @@ func (c *StatsDeviceResponse) Collect(ch chan<- prometheus.Metric) {
c.numericalMetrics["last_connection_duration"].Desc,
c.numericalMetrics["last_connection_duration"].Type,
c.numericalMetrics["last_connection_duration"].Value(deviceDataAssertion["lastConnectionDurationS"].(float64)),
deviceID, deviceDataAssertion["lastSeen"].(string),
deviceID,
)
thetime, err := time.Parse(time.RFC3339, deviceDataAssertion["lastSeen"].(string))
if err != nil {
_ = level.Warn(*c.logger).Log(
"msg", "failed to parse timestamp",
"err", err,
)
return
}
epoch := float64(thetime.Unix())
ch <- prometheus.MustNewConstMetric(
c.numericalMetrics["last_connection_timestamp"].Desc,
c.numericalMetrics["last_connection_timestamp"].Type,
c.numericalMetrics["last_connection_timestamp"].Value(epoch),
deviceID,
)
}

Expand Down
11 changes: 8 additions & 3 deletions collector/rest_stats_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ func TestNewStatsDeviceReport(t *testing.T) {
syncthing_rest_stats_device_json_parse_failures 0
# HELP syncthing_rest_stats_device_last_connection_duration Duration of last connection with remote device in seconds.
# TYPE syncthing_rest_stats_device_last_connection_duration gauge
syncthing_rest_stats_device_last_connection_duration{deviceID="AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA",lastSeen="2077-12-18T00:00:50.3810375-08:00"} 0
syncthing_rest_stats_device_last_connection_duration{deviceID="AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-BBBBBBB",lastSeen="2077-04-13T04:50:25-07:00"} 819990.3432191
syncthing_rest_stats_device_last_connection_duration{deviceID="AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-CCCCCCC",lastSeen="1984-12-31T16:00:00-08:00"} 0
syncthing_rest_stats_device_last_connection_duration{deviceID="AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA"} 0
syncthing_rest_stats_device_last_connection_duration{deviceID="AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-BBBBBBB"} 819990.3432191
syncthing_rest_stats_device_last_connection_duration{deviceID="AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-CCCCCCC"} 0
# HELP syncthing_rest_stats_device_last_connection_timestamp Timestamp since last connection with remote device expressed in Unix epoch
# TYPE syncthing_rest_stats_device_last_connection_timestamp gauge
syncthing_rest_stats_device_last_connection_timestamp{deviceID="AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA"} 3407040050
syncthing_rest_stats_device_last_connection_timestamp{deviceID="AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-BBBBBBB"} 3385540225
syncthing_rest_stats_device_last_connection_timestamp{deviceID="AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA-CCCCCCC"} 473385600
# HELP syncthing_rest_stats_device_total_scrapes Current total Syncthings scrapes.
# TYPE syncthing_rest_stats_device_total_scrapes counter
syncthing_rest_stats_device_total_scrapes 1
Expand Down

0 comments on commit 6106b56

Please sign in to comment.