mirror of
https://github.com/hikhvar/mqtt2prometheus.git
synced 2026-08-23 21:16:15 +00:00
optional ts in metrics
This commit is contained in:
@@ -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"`
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user