Skip to content

Commit

Permalink
fix: compatible with stream entry id metric for redis version 6.x/5.x. (
Browse files Browse the repository at this point in the history
  • Loading branch information
gitsang authored Jul 15, 2024
1 parent e9871c1 commit 9be2665
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
21 changes: 18 additions & 3 deletions exporter/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,18 @@ func getStreamInfo(c redis.Conn, key string) (*streamInfo, error) {
}

// Extract first and last id from slice
stream.FirstEntryId = getStreamEntryId(values, 17)
stream.LastEntryId = getStreamEntryId(values, 19)
for idx, v := range values {
vbytes, ok := v.([]byte)
if !ok {
continue
}
if string(vbytes) == "first-entry" {
stream.FirstEntryId = getStreamEntryId(values, idx+1)
}
if string(vbytes) == "last-entry" {
stream.LastEntryId = getStreamEntryId(values, idx+1)
}
}

stream.StreamGroupsInfo, err = scanStreamGroups(c, key)
if err != nil {
Expand All @@ -65,7 +75,12 @@ func getStreamInfo(c redis.Conn, key string) (*streamInfo, error) {
}

func getStreamEntryId(redisValue []interface{}, index int) string {
if len(redisValue) < index || redisValue[index] == nil || len(redisValue[index].([]interface{})) < 2 {
if values, ok := redisValue[index].([]interface{}); !ok || len(values) < 2 {
log.Debugf("Failed to parse StreamEntryId")
return ""
}

if len(redisValue) < index || redisValue[index] == nil {
log.Debugf("Failed to parse StreamEntryId")
return ""
}
Expand Down
8 changes: 4 additions & 4 deletions exporter/streams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ func TestStreamsGetStreamInfo(t *testing.T) {
if isNotTestTimestamp(info.LastGeneratedId) {
t.Errorf("Stream LastGeneratedId mismatch.\nActual: %#v;\nExpected any of: %#v", info.LastGeneratedId, TestStreamTimestamps)
}
if info.FirstEntryId != "" {
t.Errorf("Stream FirstEntryId mismatch.\nActual: %#v; - Expected empty", info.FirstEntryId)
if info.FirstEntryId != TestStreamTimestamps[0] {
t.Errorf("Stream FirstEntryId mismatch.\nActual: %#v;\nExpected any of: %#v", info.FirstEntryId, TestStreamTimestamps)
}
if info.LastEntryId != "" {
t.Errorf("Stream LastEntryId mismatch.\nActual: %#v;\nExpected empty", info.LastEntryId)
if info.LastEntryId != TestStreamTimestamps[len(TestStreamTimestamps)-1] {
t.Errorf("Stream LastEntryId mismatch.\nActual: %#v;\nExpected any of: %#v", info.LastEntryId, TestStreamTimestamps)
}
if info.MaxDeletedEntryId != "" {
t.Errorf("Stream MaxDeletedEntryId mismatch.\nActual: %#v;\nExpected: %#v", info.MaxDeletedEntryId, "0-0")
Expand Down

0 comments on commit 9be2665

Please sign in to comment.