Skip to content

Commit

Permalink
Add option to report input timestamp in prometheus output (influxdata…
Browse files Browse the repository at this point in the history
  • Loading branch information
bozaro authored and Max Eshleman committed Feb 13, 2019
1 parent d951d34 commit 13fd815
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
7 changes: 4 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@

[[constraint]]
name = "github.com/prometheus/client_golang"
version = "0.8.0"
version = "0.9.2"

[[constraint]]
name = "github.com/prometheus/client_model"
Expand Down
3 changes: 3 additions & 0 deletions plugins/outputs/prometheus_client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ This plugin starts a [Prometheus](https://prometheus.io/) Client, it exposes all
## If set, enable TLS with the given certificate.
# tls_cert = "/etc/ssl/telegraf.crt"
# tls_key = "/etc/ssl/telegraf.key"

## Export metric collection time.
# export_timestamp = false
```
16 changes: 14 additions & 2 deletions plugins/outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"net"
"net/http"
"os"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -38,6 +37,8 @@ type Sample struct {
// Histograms and Summaries need a count and a sum
Count uint64
Sum float64
// Metric timestamp
Timestamp time.Time
// Expiration is the deadline that this Sample is valid until.
Expiration time.Time
}
Expand Down Expand Up @@ -66,6 +67,7 @@ type PrometheusClient struct {
Path string `toml:"path"`
CollectorsExclude []string `toml:"collectors_exclude"`
StringAsLabel bool `toml:"string_as_label"`
ExportTimestamp bool `toml:"export_timestamp"`

server *http.Server

Expand Down Expand Up @@ -109,6 +111,9 @@ var sampleConfig = `
## Boolean value indicating whether or not to skip SSL verification
insecure_skip_verify = false
## Export metric collection time.
# export_timestamp = false
`

func (p *PrometheusClient) auth(h http.Handler) http.Handler {
Expand Down Expand Up @@ -165,7 +170,7 @@ func (p *PrometheusClient) Connect() error {
case "gocollector":
registry.Register(prometheus.NewGoCollector())
case "process":
registry.Register(prometheus.NewProcessCollector(os.Getpid(), ""))
registry.Register(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
default:
return fmt.Errorf("unrecognized collector %s", collector)
}
Expand Down Expand Up @@ -316,6 +321,9 @@ func (p *PrometheusClient) Collect(ch chan<- prometheus.Metric) {
name, labels, err.Error())
}

if p.ExportTimestamp {
metric = prometheus.NewMetricWithTimestamp(sample.Timestamp, metric)
}
ch <- metric
}
}
Expand Down Expand Up @@ -432,6 +440,7 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
SummaryValue: summaryvalue,
Count: count,
Sum: sum,
Timestamp: point.Time(),
Expiration: now.Add(p.ExpirationInterval.Duration),
}
mname = sanitize(point.Name())
Expand Down Expand Up @@ -473,6 +482,7 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
HistogramValue: histogramvalue,
Count: count,
Sum: sum,
Timestamp: point.Time(),
Expiration: now.Add(p.ExpirationInterval.Duration),
}
mname = sanitize(point.Name())
Expand All @@ -497,6 +507,7 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
sample := &Sample{
Labels: labels,
Value: value,
Timestamp: point.Time(),
Expiration: now.Add(p.ExpirationInterval.Duration),
}

Expand Down Expand Up @@ -534,6 +545,7 @@ func init() {
return &PrometheusClient{
ExpirationInterval: internal.Duration{Duration: time.Second * 60},
StringAsLabel: true,
ExportTimestamp: true,
fam: make(map[string]*MetricFamily),
now: time.Now,
}
Expand Down

0 comments on commit 13fd815

Please sign in to comment.