Skip to content

Commit

Permalink
Expose timestamps option
Browse files Browse the repository at this point in the history
  • Loading branch information
vmiszczak committed Feb 18, 2021
1 parent f20b87a commit cf55942
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
20 changes: 11 additions & 9 deletions cmd/graphite_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -105,6 +106,7 @@ func main() {
}

c.SetMapper(metricMapper)
c.ExposeTimestamps(*exposeTimestamps)

tcpSock, err := net.Listen("tcp", *graphiteAddress)
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit cf55942

Please sign in to comment.