Add option to report input timestamp in prometheus output (#5292)

This commit is contained in:
Artem V. Navrotskiy 2019-01-17 21:51:18 +03:00 committed by Daniel Nelson
parent 452b13a4e3
commit 3380fdf69d
4 changed files with 22 additions and 6 deletions

7
Gopkg.lock generated
View File

@ -892,15 +892,16 @@
version = "v1.0.0" version = "v1.0.0"
[[projects]] [[projects]]
digest = "1:4142d94383572e74b42352273652c62afec5b23f325222ed09198f46009022d1" digest = "1:6f218995d6a74636cfcab45ce03005371e682b4b9bee0e5eb0ccfd83ef85364f"
name = "github.com/prometheus/client_golang" name = "github.com/prometheus/client_golang"
packages = [ packages = [
"prometheus", "prometheus",
"prometheus/internal",
"prometheus/promhttp", "prometheus/promhttp",
] ]
pruneopts = "" pruneopts = ""
revision = "c5b7fccd204277076155f10851dad72b76a49317" revision = "505eaef017263e299324067d40ca2c48f6a2cf50"
version = "v0.8.0" version = "v0.9.2"
[[projects]] [[projects]]
branch = "master" branch = "master"

View File

@ -132,7 +132,7 @@
[[constraint]] [[constraint]]
name = "github.com/prometheus/client_golang" name = "github.com/prometheus/client_golang"
version = "0.8.0" version = "0.9.2"
[[constraint]] [[constraint]]
name = "github.com/prometheus/client_model" name = "github.com/prometheus/client_model"

View File

@ -35,4 +35,7 @@ This plugin starts a [Prometheus](https://prometheus.io/) Client, it exposes all
## If set, enable TLS with the given certificate. ## If set, enable TLS with the given certificate.
# tls_cert = "/etc/ssl/telegraf.crt" # tls_cert = "/etc/ssl/telegraf.crt"
# tls_key = "/etc/ssl/telegraf.key" # tls_key = "/etc/ssl/telegraf.key"
## Export metric collection time.
# export_timestamp = false
``` ```

View File

@ -7,7 +7,6 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"os"
"regexp" "regexp"
"sort" "sort"
"strconv" "strconv"
@ -38,6 +37,8 @@ type Sample struct {
// Histograms and Summaries need a count and a sum // Histograms and Summaries need a count and a sum
Count uint64 Count uint64
Sum float64 Sum float64
// Metric timestamp
Timestamp time.Time
// Expiration is the deadline that this Sample is valid until. // Expiration is the deadline that this Sample is valid until.
Expiration time.Time Expiration time.Time
} }
@ -64,6 +65,7 @@ type PrometheusClient struct {
Path string `toml:"path"` Path string `toml:"path"`
CollectorsExclude []string `toml:"collectors_exclude"` CollectorsExclude []string `toml:"collectors_exclude"`
StringAsLabel bool `toml:"string_as_label"` StringAsLabel bool `toml:"string_as_label"`
ExportTimestamp bool `toml:"export_timestamp"`
server *http.Server server *http.Server
@ -103,6 +105,9 @@ var sampleConfig = `
## If set, enable TLS with the given certificate. ## If set, enable TLS with the given certificate.
# tls_cert = "/etc/ssl/telegraf.crt" # tls_cert = "/etc/ssl/telegraf.crt"
# tls_key = "/etc/ssl/telegraf.key" # tls_key = "/etc/ssl/telegraf.key"
## Export metric collection time.
# export_timestamp = false
` `
func (p *PrometheusClient) auth(h http.Handler) http.Handler { func (p *PrometheusClient) auth(h http.Handler) http.Handler {
@ -159,7 +164,7 @@ func (p *PrometheusClient) Connect() error {
case "gocollector": case "gocollector":
registry.Register(prometheus.NewGoCollector()) registry.Register(prometheus.NewGoCollector())
case "process": case "process":
registry.Register(prometheus.NewProcessCollector(os.Getpid(), "")) registry.Register(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
default: default:
return fmt.Errorf("unrecognized collector %s", collector) return fmt.Errorf("unrecognized collector %s", collector)
} }
@ -282,6 +287,9 @@ func (p *PrometheusClient) Collect(ch chan<- prometheus.Metric) {
name, labels, err.Error()) name, labels, err.Error())
} }
if p.ExportTimestamp {
metric = prometheus.NewMetricWithTimestamp(sample.Timestamp, metric)
}
ch <- metric ch <- metric
} }
} }
@ -398,6 +406,7 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
SummaryValue: summaryvalue, SummaryValue: summaryvalue,
Count: count, Count: count,
Sum: sum, Sum: sum,
Timestamp: point.Time(),
Expiration: now.Add(p.ExpirationInterval.Duration), Expiration: now.Add(p.ExpirationInterval.Duration),
} }
mname = sanitize(point.Name()) mname = sanitize(point.Name())
@ -439,6 +448,7 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
HistogramValue: histogramvalue, HistogramValue: histogramvalue,
Count: count, Count: count,
Sum: sum, Sum: sum,
Timestamp: point.Time(),
Expiration: now.Add(p.ExpirationInterval.Duration), Expiration: now.Add(p.ExpirationInterval.Duration),
} }
mname = sanitize(point.Name()) mname = sanitize(point.Name())
@ -463,6 +473,7 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
sample := &Sample{ sample := &Sample{
Labels: labels, Labels: labels,
Value: value, Value: value,
Timestamp: point.Time(),
Expiration: now.Add(p.ExpirationInterval.Duration), Expiration: now.Add(p.ExpirationInterval.Duration),
} }
@ -500,6 +511,7 @@ func init() {
return &PrometheusClient{ return &PrometheusClient{
ExpirationInterval: internal.Duration{Duration: time.Second * 60}, ExpirationInterval: internal.Duration{Duration: time.Second * 60},
StringAsLabel: true, StringAsLabel: true,
ExportTimestamp: true,
fam: make(map[string]*MetricFamily), fam: make(map[string]*MetricFamily),
now: time.Now, now: time.Now,
} }