Skip to content

Commit

Permalink
Add option to fetch timestamp from JSON
Browse files Browse the repository at this point in the history
This fixes prometheus-community#80

Signed-off-by: Jérôme LOYET <822436+fatpat@users.noreply.github.com>
  • Loading branch information
fatpat committed May 5, 2021
1 parent 28dde07 commit b7fe9c9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
17 changes: 11 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import (

// Metric contains values that define a metric
type Metric struct {
Name string
Path string
Labels map[string]string
Type MetricType
Help string
Values map[string]string
Name string
Path string
Timestamp string
Timezone string
Labels map[string]string
Type MetricType
Help string
Values map[string]string
}

type MetricType string
Expand Down Expand Up @@ -63,6 +65,9 @@ func LoadConfig(configPath string) (Config, error) {
if config.Metrics[i].Help == "" {
config.Metrics[i].Help = config.Metrics[i].Name
}
if config.Metrics[i].Timezone == "" {
config.Metrics[i].Timezone = "UTC"
}
}

return config, nil
Expand Down
40 changes: 38 additions & 2 deletions exporter/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package exporter
import (
"bytes"
"encoding/json"
"time"

"github.com/araddon/dateparse"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -34,6 +36,8 @@ type JsonMetric struct {
KeyJsonPath string
ValueJsonPath string
LabelsJsonPaths []string
TimestampPath string
Timezone string
}

func (mc JsonMetricCollector) Describe(ch chan<- *prometheus.Desc) {
Expand All @@ -44,6 +48,28 @@ func (mc JsonMetricCollector) Describe(ch chan<- *prometheus.Desc) {

func (mc JsonMetricCollector) Collect(ch chan<- prometheus.Metric) {
for _, m := range mc.JsonMetrics {
var timestamp time.Time
if m.TimestampPath != "" { // fetch timestamp
if m.Timezone != "" { // fetch timezone
loc, err := time.LoadLocation(m.Timezone)
if err != nil {
level.Error(mc.Logger).Log("msg", "Failed to extract timezone for metric", "path", m.KeyJsonPath, "err", err, "metric", m.Desc) //nolint:errcheck
continue
}
time.Local = loc
}
ts, err := extractValue(mc.Logger, mc.Data, m.TimestampPath, false)
if err != nil {
level.Error(mc.Logger).Log("msg", "Failed to extract timestamp for metric", "path", m.KeyJsonPath, "err", err, "metric", m.Desc) //nolint:errcheck
continue
}
timestamp, err = dateparse.ParseLocal(ts)
if err != nil {
level.Error(mc.Logger).Log("msg", "Failed to convert timestamp", "path", m.KeyJsonPath, "err", err, "metric", m.Desc) //nolint:errcheck
continue
}
}

if m.ValueJsonPath == "" { // ScrapeType is 'value'
value, err := extractValue(mc.Logger, mc.Data, m.KeyJsonPath, false)
if err != nil {
Expand All @@ -53,12 +79,17 @@ func (mc JsonMetricCollector) Collect(ch chan<- prometheus.Metric) {

if floatValue, err := SanitizeValue(value); err == nil {

ch <- prometheus.MustNewConstMetric(
metric := prometheus.MustNewConstMetric(
m.Desc,
prometheus.UntypedValue,
floatValue,
extractLabels(mc.Logger, mc.Data, m.LabelsJsonPaths)...,
)
if (timestamp.IsZero()) {
ch <- metric
} else {
ch <- prometheus.NewMetricWithTimestamp(timestamp, metric)
}
} else {
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.KeyJsonPath, "value", value, "err", err, "metric", m.Desc) //nolint:errcheck
continue
Expand All @@ -85,12 +116,17 @@ func (mc JsonMetricCollector) Collect(ch chan<- prometheus.Metric) {
}

if floatValue, err := SanitizeValue(value); err == nil {
ch <- prometheus.MustNewConstMetric(
metric := prometheus.MustNewConstMetric(
m.Desc,
prometheus.UntypedValue,
floatValue,
extractLabels(mc.Logger, jdata, m.LabelsJsonPaths)...,
)
if (timestamp.IsZero()) {
ch <- metric
} else {
ch <- prometheus.NewMetricWithTimestamp(timestamp, metric)
}
} else {
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.ValueJsonPath, "value", value, "err", err, "metric", m.Desc) //nolint:errcheck
continue
Expand Down
4 changes: 4 additions & 0 deletions exporter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func CreateMetricsList(c config.Config) ([]JsonMetric, error) {
),
KeyJsonPath: metric.Path,
LabelsJsonPaths: variableLabelsValues,
TimestampPath: metric.Timestamp,
Timezone: metric.Timezone,
}
metrics = append(metrics, jsonMetric)
case config.ObjectScrape:
Expand All @@ -100,6 +102,8 @@ func CreateMetricsList(c config.Config) ([]JsonMetric, error) {
KeyJsonPath: metric.Path,
ValueJsonPath: valuePath,
LabelsJsonPaths: variableLabelsValues,
TimestampPath: metric.Timestamp,
Timezone: metric.Timezone,
}
metrics = append(metrics, jsonMetric)
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/prometheus-community/json_exporter
go 1.14

require (
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/go-kit/kit v0.10.0
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/common v0.15.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2c
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de h1:FxWPpzIjnTlhPwqqXc4/vE0f7GvRjuAsbW+HOIe8KnA=
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoUhZrYW9p1lxo/cm8EmUOOzAPSEZNGF2DK1dJgw=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
Expand Down Expand Up @@ -251,6 +253,7 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
Expand Down Expand Up @@ -341,11 +344,13 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.2.0 h1:wH4vA7pcjKuZzjF7lM8awk4fnuJO6idemZXoKnULUx4=
github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4/go.mod h1:C1a7PQSMz9NShzorzCiG2fk9+xuCgLkPeCvMHYR2OWg=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand All @@ -371,6 +376,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
Expand Down

0 comments on commit b7fe9c9

Please sign in to comment.