Skip to content

Commit

Permalink
feat: 采集器调整 prom 指标时间戳赋值行为 --story=117553852 (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx authored May 21, 2024
1 parent 86c7e04 commit 5b8efe0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/bkmonitorbeat/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v3.33.x
v3.34.x
2 changes: 1 addition & 1 deletion pkg/bkmonitorbeat/tasks/prom_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func NewPromEventV2(line string, ts int64, offsetTime time.Duration, handler Tim

var peTs int64
if timestamp == nil {
peTs = handler(ts, time.Now().UnixMilli(), offsetTime)
peTs = handler(ts, ts, offsetTime)
} else {
peTs = handler(ts, *timestamp, offsetTime)
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/bkmonitorbeat/tasks/prom_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ func TestPromEventExemplars(t *testing.T) {
assert.Equal(t, 0.15, event.Exemplar.Value)
}

func TestPromEventTs(t *testing.T) {
timeOffset := 24 * time.Hour * 365 * 200
tsHandler, _ := GetTimestampHandler("s")

t.Run("Without Timestamp", func(t *testing.T) {
line := `my_histogram_bucketx{le="0.25"} 205.5`
nowTs := int64(1637839803000) // 设定为当前时间
event, err := NewPromEvent(line, nowTs, timeOffset, tsHandler)
assert.NoError(t, err)
assert.Equal(t, nowTs/1000, event.GetTimestamp())
})

t.Run("With Timestamp", func(t *testing.T) {
line := `my_histogram_bucketx{le="0.25"} 205.5 1637839804000`
nowTs := int64(1637839803000)
event, err := NewPromEvent(line, nowTs, timeOffset, tsHandler)
assert.NoError(t, err)
assert.Equal(t, int64(1637839804), event.GetTimestamp())
})
}

func TestPromEvent(t *testing.T) {
line := `my_histogram_bucketx{le="0.25"} 205.5`
event, err := NewPromEvent(line, 1637839803, time.Second, func(_ int64, ts int64, _ time.Duration) int64 {
Expand Down

0 comments on commit 5b8efe0

Please sign in to comment.