Skip to content

Commit 8a6c751

Browse files
committed
event_to_metric: use millisecond precision
1 parent 3a7ee12 commit 8a6c751

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

plugin/action/event_to_metrics/event_to_metrics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {
139139

140140
object.AddField("name").MutateToBytes([]byte(metric.Name))
141141
object.AddField("type").MutateToBytes([]byte(metric.Type))
142-
object.AddField("ttl").MutateToInt64(metric.TTL_.Nanoseconds())
143-
object.AddField("timestamp").MutateToInt64(ts.UnixNano())
142+
object.AddField("ttl").MutateToInt64(metric.TTL_.Milliseconds())
143+
object.AddField("timestamp").MutateToInt64(ts.UnixMilli())
144144

145145
if len(metric.Value) == 0 {
146146
object.AddField("value").MutateToInt(1)

plugin/output/prometheus/metric_collector.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func newCollector(timeout time.Duration, sender storageSender, logger *zap.Logge
5050
func (p *metricCollector) handleMetric(labels []promwrite.Label, value float64, timestamp int64, metricType string, ttl int64) []promwrite.TimeSeries {
5151
labelsKey := labelsToKey(labels)
5252
now := time.Now()
53-
currentTimestampSec := timestamp / 1_000_000_000
53+
currentTimestampSec := timestamp / 1_000
5454

5555
var values []promwrite.TimeSeries
5656
var shouldSend bool
@@ -59,7 +59,7 @@ func (p *metricCollector) handleMetric(labels []promwrite.Label, value float64,
5959

6060
if prev, ok := p.collector.Load(labelsKey); ok {
6161
prevMetric = prev.(metricValue)
62-
prevTimestampSec := prevMetric.timestamp / 1_000_000_000
62+
prevTimestampSec := prevMetric.timestamp / 1_000
6363

6464
// For counters, accumulate values
6565
currentValue = value
@@ -71,7 +71,7 @@ func (p *metricCollector) handleMetric(labels []promwrite.Label, value float64,
7171
shouldSend = prevTimestampSec < currentTimestampSec
7272

7373
if shouldSend {
74-
prevMetric.timestamp = max(prevMetric.timestamp, timestamp-1_000_000_000)
74+
prevMetric.timestamp = max(prevMetric.timestamp, timestamp-1_000)
7575
values = append(values, createTimeSeries(labels, prevMetric))
7676
}
7777
} else {
@@ -84,7 +84,7 @@ func (p *metricCollector) handleMetric(labels []promwrite.Label, value float64,
8484
value: currentValue,
8585
timestamp: timestamp,
8686
lastUpdateTime: now,
87-
expiredAt: now.Add(time.Duration(ttl)),
87+
expiredAt: now.Add(time.Duration(ttl) * time.Millisecond),
8888
lastValueIsSended: shouldSend,
8989
})
9090

@@ -132,7 +132,7 @@ func (p *metricCollector) flushOldMetrics() {
132132
func (p *metricCollector) repeatOldMetrics() {
133133
var toSend []promwrite.TimeSeries
134134
now := time.Now()
135-
nowUnixtime := now.UnixNano()
135+
nowUnixtime := now.UnixMilli()
136136

137137
p.collector.Range(func(key, value interface{}) bool {
138138
metric := value.(metricValue)
@@ -174,7 +174,7 @@ func createTimeSeries(labels []promwrite.Label, metric metricValue) promwrite.Ti
174174
return promwrite.TimeSeries{
175175
Labels: labels,
176176
Sample: promwrite.Sample{
177-
Time: time.Unix(0, metric.timestamp),
177+
Time: time.Unix(0, metric.timestamp*int64(time.Millisecond)),
178178
Value: metric.value,
179179
},
180180
}

0 commit comments

Comments
 (0)