From cf55942eb49a31fc65235257ad7c09b0fe578dbe Mon Sep 17 00:00:00 2001 From: vmiszczak Date: Thu, 18 Feb 2021 22:58:49 +0100 Subject: [PATCH] Expose timestamps option --- cmd/graphite_exporter/main.go | 20 +++++++++++--------- collector/collector.go | 12 +++++++++++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/cmd/graphite_exporter/main.go b/cmd/graphite_exporter/main.go index 77e01fc..3ec691e 100644 --- a/cmd/graphite_exporter/main.go +++ b/cmd/graphite_exporter/main.go @@ -36,15 +36,16 @@ import ( ) var ( - listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics.").Default(":9108").String() - metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose Prometheus metrics.").Default("/metrics").String() - graphiteAddress = kingpin.Flag("graphite.listen-address", "TCP and UDP address on which to accept samples.").Default(":9109").String() - mappingConfig = kingpin.Flag("graphite.mapping-config", "Metric mapping configuration file name.").Default("").String() - sampleExpiry = kingpin.Flag("graphite.sample-expiry", "How long a sample is valid for.").Default("5m").Duration() - strictMatch = kingpin.Flag("graphite.mapping-strict-match", "Only store metrics that match the mapping configuration.").Bool() - cacheSize = kingpin.Flag("graphite.cache-size", "Maximum size of your metric mapping cache. Relies on least recently used replacement policy if max size is reached.").Default("1000").Int() - cacheType = kingpin.Flag("graphite.cache-type", "Metric mapping cache type. Valid options are \"lru\" and \"random\"").Default("lru").Enum("lru", "random") - dumpFSMPath = kingpin.Flag("debug.dump-fsm", "The path to dump internal FSM generated for glob matching as Dot file.").Default("").String() + listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics.").Default(":9108").String() + metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose Prometheus metrics.").Default("/metrics").String() + exposeTimestamps = kingpin.Flag("web.expose-timestamps", "Expose timestamps from Graphite samples").Default("false").Bool() + graphiteAddress = kingpin.Flag("graphite.listen-address", "TCP and UDP address on which to accept samples.").Default(":9109").String() + mappingConfig = kingpin.Flag("graphite.mapping-config", "Metric mapping configuration file name.").Default("").String() + sampleExpiry = kingpin.Flag("graphite.sample-expiry", "How long a sample is valid for.").Default("5m").Duration() + strictMatch = kingpin.Flag("graphite.mapping-strict-match", "Only store metrics that match the mapping configuration.").Bool() + cacheSize = kingpin.Flag("graphite.cache-size", "Maximum size of your metric mapping cache. Relies on least recently used replacement policy if max size is reached.").Default("1000").Int() + cacheType = kingpin.Flag("graphite.cache-type", "Metric mapping cache type. Valid options are \"lru\" and \"random\"").Default("lru").Enum("lru", "random") + dumpFSMPath = kingpin.Flag("debug.dump-fsm", "The path to dump internal FSM generated for glob matching as Dot file.").Default("").String() ) func init() { @@ -105,6 +106,7 @@ func main() { } c.SetMapper(metricMapper) + c.ExposeTimestamps(*exposeTimestamps) tcpSock, err := net.Listen("tcp", *graphiteAddress) if err != nil { diff --git a/collector/collector.go b/collector/collector.go index 221ab82..fb00e3b 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -45,6 +45,7 @@ type graphiteCollector struct { lastProcessed prometheus.Gauge sampleExpiryMetric prometheus.Gauge sampleExpiry time.Duration + exposeTimestamps bool } func NewGraphiteCollector(logger log.Logger, strictMatch bool, sampleExpiry time.Duration) *graphiteCollector { @@ -80,6 +81,10 @@ func NewGraphiteCollector(logger log.Logger, strictMatch bool, sampleExpiry time return c } +func (c *graphiteCollector) ExposeTimestamps(exporeTimestamps bool) { + c.exposeTimestamps = exporeTimestamps +} + func (c *graphiteCollector) ProcessReader(reader io.Reader) { lineScanner := bufio.NewScanner(reader) for { @@ -229,11 +234,16 @@ func (c graphiteCollector) Collect(ch chan<- prometheus.Metric) { if ageLimit.After(sample.Timestamp) { continue } - ch <- prometheus.MustNewConstMetric( + var metric prometheus.Metric + metric = prometheus.MustNewConstMetric( prometheus.NewDesc(sample.Name, sample.Help, []string{}, sample.Labels), sample.Type, sample.Value, ) + if c.exposeTimestamps { + metric = prometheus.NewMetricWithTimestamp(sample.Timestamp, metric) + } + ch <- metric } }