Skip to content

Commit

Permalink
fix: level detection for warning level (#14444)
Browse files Browse the repository at this point in the history
(cherry picked from commit 242a852)
  • Loading branch information
trevorwhitney authored and grafana-delivery-bot[bot] committed Oct 10, 2024
1 parent 901525e commit 74e423e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/distributor/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ func extractLogLevelFromLogLine(log string) string {
return constants.LogLevelDebug
case bytes.EqualFold(v, []byte("info")), bytes.EqualFold(v, []byte("inf")):
return constants.LogLevelInfo
case bytes.EqualFold(v, []byte("warn")), bytes.EqualFold(v, []byte("wrn")):
case bytes.EqualFold(v, []byte("warn")), bytes.EqualFold(v, []byte("wrn")), bytes.EqualFold(v, []byte("warning")):
return constants.LogLevelWarn
case bytes.EqualFold(v, []byte("error")), bytes.EqualFold(v, []byte("err")):
return constants.LogLevelError
Expand Down
36 changes: 22 additions & 14 deletions pkg/distributor/distributor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ func makeWriteRequestWithLabelsWithLevel(lines, size int, labels []string, level

for j := 0; j < lines; j++ {
// Construct the log line, honoring the input size
line := "msg=" + strconv.Itoa(j) + strings.Repeat("0", size) + " severity=" + level
line := "msg=an error occured " + strconv.Itoa(j) + strings.Repeat("0", size) + " severity=" + level

stream.Entries = append(stream.Entries, logproto.Entry{
Timestamp: time.Now().Add(time.Duration(j) * time.Millisecond),
Expand Down Expand Up @@ -1644,20 +1644,28 @@ func Test_DetectLogLevels(t *testing.T) {
})

t.Run("log level detection enabled and warn logs", func(t *testing.T) {
limits, ingester := setup(true)
distributors, _ := prepare(t, 1, 5, limits, func(_ string) (ring_client.PoolClient, error) { return ingester, nil })
for _, level := range []string{"warn", "Wrn", "WARNING"} {
limits, ingester := setup(true)
distributors, _ := prepare(
t,
1,
5,
limits,
func(_ string) (ring_client.PoolClient, error) { return ingester, nil },
)

writeReq := makeWriteRequestWithLabelsWithLevel(1, 10, []string{`{foo="bar"}`}, "warn")
_, err := distributors[0].Push(ctx, writeReq)
require.NoError(t, err)
topVal := ingester.Peek()
require.Equal(t, `{foo="bar"}`, topVal.Streams[0].Labels)
require.Equal(t, push.LabelsAdapter{
{
Name: constants.LevelLabel,
Value: constants.LogLevelWarn,
},
}, topVal.Streams[0].Entries[0].StructuredMetadata)
writeReq := makeWriteRequestWithLabelsWithLevel(1, 10, []string{`{foo="bar"}`}, level)
_, err := distributors[0].Push(ctx, writeReq)
require.NoError(t, err)
topVal := ingester.Peek()
require.Equal(t, `{foo="bar"}`, topVal.Streams[0].Labels)
require.Equal(t, push.LabelsAdapter{
{
Name: constants.LevelLabel,
Value: constants.LogLevelWarn,
},
}, topVal.Streams[0].Entries[0].StructuredMetadata, fmt.Sprintf("level: %s", level))
}
})

t.Run("log level detection enabled but log level already present in stream", func(t *testing.T) {
Expand Down

0 comments on commit 74e423e

Please sign in to comment.