Skip to content
This repository has been archived by the owner on Mar 30, 2024. It is now read-only.

Commit

Permalink
Parse values of type timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
alxrem committed Aug 14, 2019
1 parent bc4b1be commit cc287e9
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (e *Exporter) collectMetrics(stats *Stats, ch chan<- prometheus.Metric) {
}

func (e *Exporter) collectTree(name string, data interface{}, labels prometheus.Labels, ch chan<- prometheus.Metric) {
if v, ok := data.(float64); ok {
if v, ok := parseData(data); ok {
if len(labels) == 0 {
metric := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Expand Down Expand Up @@ -184,6 +184,20 @@ func (e *Exporter) collectPlugins(name string, section string, data interface{},
}
}

func parseData(data interface{}) (float64, bool) {
if value, ok := data.(float64); ok {
return value, ok
}

if v, ok := data.(string); ok {
if timestamp, err := time.Parse(time.RFC3339, v); err == nil {
return float64(timestamp.Unix()), true
}
}

return 0, false
}

func (e *Exporter) fetchStats() (*Stats, error) {
body, err := e.fetch(e.nodeStatsUri)
if err != nil {
Expand Down

0 comments on commit cc287e9

Please sign in to comment.