diff --git a/pkg/config/config.go b/pkg/config/config.go index 07a27a2..e20f80d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -133,6 +133,7 @@ type MetricConfig struct { SensorNameFilter Regexp `yaml:"sensor_name_filter"` Help string `yaml:"help"` ValueType string `yaml:"type"` + OmitTimestamp bool `yaml:"omit_timestamp"` ConstantLabels map[string]string `yaml:"const_labels"` StringValueMapping *StringValueMappingConfig `yaml:"string_value_mapping"` MQTTValueScale float64 `yaml:"mqtt_value_scale"` diff --git a/pkg/metrics/collector.go b/pkg/metrics/collector.go index 0016839..188957a 100644 --- a/pkg/metrics/collector.go +++ b/pkg/metrics/collector.go @@ -2,11 +2,12 @@ package metrics import ( "fmt" + "time" + "github.com/hikhvar/mqtt2prometheus/pkg/config" gocache "github.com/patrickmn/go-cache" "github.com/prometheus/client_golang/prometheus" "go.uber.org/zap" - "time" ) type Collector interface { @@ -77,7 +78,12 @@ func (c *MemoryCachedCollector) Collect(mc chan<- prometheus.Metric) { device, metric.Topic, ) - mc <- prometheus.NewMetricWithTimestamp(metric.IngestTime, m) + + if metric.IngestTime.IsZero() { + mc <- m + } else { + mc <- prometheus.NewMetricWithTimestamp(metric.IngestTime, m) + } } } diff --git a/pkg/metrics/parser.go b/pkg/metrics/parser.go index c08da3b..e1f13a2 100644 --- a/pkg/metrics/parser.go +++ b/pkg/metrics/parser.go @@ -99,10 +99,15 @@ func (p *Parser) parseMetric(metricPath string, deviceID string, value interface metricValue = metricValue * cfg.MQTTValueScale } + var ingestTime time.Time + if !cfg.OmitTimestamp { + ingestTime = now() + } + return Metric{ Description: cfg.PrometheusDescription(), Value: metricValue, ValueType: cfg.PrometheusValueType(), - IngestTime: now(), + IngestTime: ingestTime, }, nil }