optional ts in metrics

This commit is contained in:
alessandro negrin
2022-11-30 10:02:48 +01:00
parent 070e15fe91
commit a76297e47c
3 changed files with 15 additions and 3 deletions
+1
View File
@@ -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"`
+8 -2
View File
@@ -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)
}
}
}
+6 -1
View File
@@ -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
}