mirror of
https://github.com/prometheus/prometheus
synced 2025-04-10 19:42:04 +00:00
Merge pull request #14760 from bboreham/simplify-otel-time-conversion
[REFACTOR] OTLP translator: simplify time conversion
This commit is contained in:
commit
ee2b7fdf9d
@ -24,7 +24,6 @@ import (
|
|||||||
"slices"
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/cespare/xxhash/v2"
|
"github.com/cespare/xxhash/v2"
|
||||||
@ -594,5 +593,5 @@ func addResourceTargetInfo(resource pcommon.Resource, settings Settings, timesta
|
|||||||
|
|
||||||
// convertTimeStamp converts OTLP timestamp in ns to timestamp in ms
|
// convertTimeStamp converts OTLP timestamp in ns to timestamp in ms
|
||||||
func convertTimeStamp(timestamp pcommon.Timestamp) int64 {
|
func convertTimeStamp(timestamp pcommon.Timestamp) int64 {
|
||||||
return timestamp.AsTime().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
|
return int64(timestamp) / 1_000_000
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ package prometheusremotewrite
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"go.opentelemetry.io/collector/pdata/pcommon"
|
"go.opentelemetry.io/collector/pdata/pcommon"
|
||||||
@ -159,3 +160,21 @@ func TestCreateAttributes(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_convertTimeStamp(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
arg pcommon.Timestamp
|
||||||
|
want int64
|
||||||
|
}{
|
||||||
|
{"zero", 0, 0},
|
||||||
|
{"1ms", 1_000_000, 1},
|
||||||
|
{"1s", pcommon.Timestamp(time.Unix(1, 0).UnixNano()), 1000},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := convertTimeStamp(tt.arg)
|
||||||
|
assert.Equal(t, tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user