Skip to content

Commit

Permalink
Change date and time parsing to use strptime format
Browse files Browse the repository at this point in the history
  • Loading branch information
phibos committed Aug 29, 2024
1 parent 73e4c33 commit 80ec12c
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/gosnmp/gosnmp"
"github.com/itchyny/timefmt-go"
"github.com/prometheus/client_golang/prometheus"

"github.com/prometheus/snmp_exporter/config"
Expand Down Expand Up @@ -546,7 +547,7 @@ func parseDateAndTime(pdu *gosnmp.SnmpPDU) (float64, error) {

func parseDateAndTimeWithPattern(metric *config.Metric, pdu *gosnmp.SnmpPDU, metrics Metrics) (float64, error) {
pduValue := pduValueAsString(pdu, "DisplayString", metrics)
t, err := time.Parse(metric.DateTimePattern, pduValue)
t, err := timefmt.Parse(pduValue, metric.DateTimePattern)
if err != nil {
return 0, fmt.Errorf("error parsing date and time %q", err)
}
Expand Down
4 changes: 2 additions & 2 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,13 +829,13 @@ func TestParseDateAndTimeWithPattern(t *testing.T) {
}{
{
pdu: &gosnmp.SnmpPDU{Value: "Apr 01 2025"},
metric: config.Metric{DateTimePattern: "Jan 02 2006"},
metric: config.Metric{DateTimePattern: "%b %d %Y"},
result: 1.7434656e+09,
shouldErr: false,
},
{
pdu: &gosnmp.SnmpPDU{Value: "ABC"},
metric: config.Metric{DateTimePattern: "Jan 02 2006"},
metric: config.Metric{DateTimePattern: "%b %d %Y"},
result: 0,
shouldErr: true,
},
Expand Down
2 changes: 1 addition & 1 deletion generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ modules:
value: '1' # The first entry whose regex matches and whose value parses wins.
- regex: '.*'
value: '0'
datetime_pattern: # Used if type = ParseDateAndTime. Uses the go time.Parse() format https://pkg.go.dev/time#pkg-constants
datetime_pattern: # Used if type = ParseDateAndTime. Uses the strptime format (See: man 3 strptime)
offset: 1.0 # Add the value to the same. Applied after scale.
scale: 1.0 # Scale the value of the sample by this value.
type: DisplayString # Override the metric type, possible types are:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/itchyny/timefmt-go v0.1.6 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gosnmp/gosnmp v1.37.0 h1:/Tf8D3b9wrnNuf/SfbvO+44mPrjVphBhRtcGg22V07Y=
github.com/gosnmp/gosnmp v1.37.0/go.mod h1:GDH9vNqpsD7f2HvZhKs5dlqSEcAS6s6Qp099oZRCR+M=
github.com/itchyny/timefmt-go v0.1.6 h1:ia3s54iciXDdzWzwaVKXZPbiXzxxnv1SPGFfM/myJ5Q=
github.com/itchyny/timefmt-go v0.1.6/go.mod h1:RRDZYC5s9ErkjQvTvvU7keJjxUYzIISJGxm9/mAERQg=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down

0 comments on commit 80ec12c

Please sign in to comment.