Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: detected_level in aggregated metrics #14463

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/pattern/aggregation/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"

"github.com/grafana/loki/v3/pkg/loghttp/push"
"github.com/grafana/loki/v3/pkg/logproto"
"github.com/grafana/loki/v3/pkg/logql/syntax"
"github.com/grafana/loki/v3/pkg/util"
"github.com/grafana/loki/v3/pkg/util/build"
"github.com/grafana/loki/v3/pkg/util/constants"

"github.com/grafana/dskit/backoff"

Expand Down Expand Up @@ -309,7 +309,7 @@ func (p *Push) send(ctx context.Context, payload []byte) (int, error) {
func AggregatedMetricEntry(
ts model.Time,
totalBytes, totalCount uint64,
service string,
level string,
lbls labels.Labels,
) string {
byteString := util.HumanizeBytes(totalBytes)
Expand All @@ -318,7 +318,7 @@ func AggregatedMetricEntry(
ts.UnixNano(),
byteString,
totalCount,
push.LabelServiceName, service,
constants.LevelLabel, level,
)

for _, l := range lbls {
Expand Down
107 changes: 76 additions & 31 deletions pkg/pattern/ingester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pattern

import (
"context"
"fmt"
"math"
"testing"
"time"
Expand All @@ -15,8 +16,8 @@ import (
"github.com/grafana/dskit/ring"

"github.com/grafana/loki/v3/pkg/logproto"
"github.com/grafana/loki/v3/pkg/pattern/aggregation"
"github.com/grafana/loki/v3/pkg/pattern/iter"
"github.com/grafana/loki/v3/pkg/util"
"github.com/grafana/loki/v3/pkg/util/constants"

"github.com/grafana/loki/v3/pkg/pattern/drain"
Expand Down Expand Up @@ -149,6 +150,7 @@ func TestInstancePushAggregateMetrics(t *testing.T) {

err = inst.Push(context.Background(), &push.PushRequest{
Streams: []push.Stream{
// info level
{
Labels: lbs.String(),
Entries: []push.Entry{
Expand All @@ -164,21 +166,17 @@ func TestInstancePushAggregateMetrics(t *testing.T) {
},
},
},
// unknown level
{
Labels: lbs2.String(),
Entries: []push.Entry{
{
Timestamp: time.Unix(20, 0),
Line: "ts=1 msg=hello",
StructuredMetadata: push.LabelsAdapter{
push.LabelAdapter{
Name: constants.LevelLabel,
Value: "error",
},
},
},
},
},
// error level
{
Labels: lbs3.String(),
Entries: []push.Entry{
Expand All @@ -199,6 +197,7 @@ func TestInstancePushAggregateMetrics(t *testing.T) {
for i := 0; i < 30; i++ {
err = inst.Push(context.Background(), &push.PushRequest{
Streams: []push.Stream{
// info
{
Labels: lbs.String(),
Entries: []push.Entry{
Expand All @@ -214,6 +213,7 @@ func TestInstancePushAggregateMetrics(t *testing.T) {
},
},
},
// error
{
Labels: lbs2.String(),
Entries: []push.Entry{
Expand Down Expand Up @@ -243,9 +243,26 @@ func TestInstancePushAggregateMetrics(t *testing.T) {

require.Len(t, inst.aggMetricsByStreamAndLevel, 3)

require.Equal(t, uint64(14+(15*30)), inst.aggMetricsByStreamAndLevel[lbs.String()]["info"].bytes)
require.Equal(t, uint64(14+(15*30)), inst.aggMetricsByStreamAndLevel[lbs2.String()]["error"].bytes)
require.Equal(t, uint64(17), inst.aggMetricsByStreamAndLevel[lbs3.String()]["error"].bytes)
require.Equal(
t,
uint64(14+(15*30)),
inst.aggMetricsByStreamAndLevel[lbs.String()]["info"].bytes,
)
require.Equal(
t,
uint64(14),
inst.aggMetricsByStreamAndLevel[lbs2.String()]["unknown"].bytes,
)
require.Equal(
t,
uint64(15*30),
inst.aggMetricsByStreamAndLevel[lbs2.String()]["error"].bytes,
)
require.Equal(
t,
uint64(17),
inst.aggMetricsByStreamAndLevel[lbs3.String()]["error"].bytes,
)

require.Equal(
t,
Expand All @@ -254,7 +271,12 @@ func TestInstancePushAggregateMetrics(t *testing.T) {
)
require.Equal(
t,
uint64(31),
uint64(1),
inst.aggMetricsByStreamAndLevel[lbs2.String()]["unknown"].count,
)
require.Equal(
t,
uint64(30),
inst.aggMetricsByStreamAndLevel[lbs2.String()]["error"].count,
)
require.Equal(
Expand All @@ -270,51 +292,74 @@ func TestInstancePushAggregateMetrics(t *testing.T) {
now := model.Now()
inst.Downsample(now)

expectedTestLine := fmt.Sprintf(
`ts=%d bytes=%s count=%d detected_level="info" %s="test_service" test="test"`,
now.UnixNano(),
util.HumanizeBytes(uint64(14+(15*30))),
uint64(31),
loghttp_push.LabelServiceName,
)

mockWriter.AssertCalled(
t,
"WriteEntry",
now.Time(),
aggregation.AggregatedMetricEntry(
now,
uint64(14+(15*30)),
uint64(31),
"test_service",
lbs,
),
expectedTestLine,
labels.New(
labels.Label{Name: loghttp_push.AggregatedMetricLabel, Value: "test_service"},
labels.Label{Name: "level", Value: "info"},
),
)

expectedFooLine1 := fmt.Sprintf(
`ts=%d bytes=%s count=%d detected_level="unknown" foo="bar" %s="foo_service"`,
now.UnixNano(),
util.HumanizeBytes(uint64(14)),
uint64(1),
loghttp_push.LabelServiceName,
)
expectedFooLine2 := fmt.Sprintf(
`ts=%d bytes=%s count=%d detected_level="error" foo="bar" %s="foo_service"`,
now.UnixNano(),
util.HumanizeBytes(uint64(15*30)),
uint64(30),
loghttp_push.LabelServiceName,
)

mockWriter.AssertCalled(
t,
"WriteEntry",
now.Time(),
aggregation.AggregatedMetricEntry(
now,
uint64(14+(15*30)),
uint64(31),
"foo_service",
lbs2,
expectedFooLine1,
labels.New(
labels.Label{Name: loghttp_push.AggregatedMetricLabel, Value: "foo_service"},
labels.Label{Name: "level", Value: "unknown"},
),
)
mockWriter.AssertCalled(
t,
"WriteEntry",
now.Time(),
expectedFooLine2,
labels.New(
labels.Label{Name: loghttp_push.AggregatedMetricLabel, Value: "foo_service"},
labels.Label{Name: "level", Value: "error"},
),
)

expectedBazLine := fmt.Sprintf(
`ts=%d bytes=%s count=%d detected_level="error" foo="baz" %s="baz_service"`,
now.UnixNano(),
util.HumanizeBytes(uint64(17)),
uint64(1),
loghttp_push.LabelServiceName,
)

mockWriter.AssertCalled(
t,
"WriteEntry",
now.Time(),
aggregation.AggregatedMetricEntry(
now,
uint64(17),
uint64(1),
"baz_service",
lbs3,
),
expectedBazLine,
labels.New(
labels.Label{Name: loghttp_push.AggregatedMetricLabel, Value: "baz_service"},
labels.Label{Name: "level", Value: "error"},
Expand Down
3 changes: 2 additions & 1 deletion pkg/pattern/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ func (i *instance) writeAggregatedMetrics(
service := streamLbls.Get(push.LabelServiceName)
if service == "" {
service = push.ServiceUnknown
streamLbls = append(streamLbls, labels.Label{Name: push.LabelServiceName, Value: service})
}

newLbls := labels.Labels{
Expand All @@ -318,7 +319,7 @@ func (i *instance) writeAggregatedMetrics(
if i.writer != nil {
i.writer.WriteEntry(
now.Time(),
aggregation.AggregatedMetricEntry(now, totalBytes, totalCount, service, streamLbls),
aggregation.AggregatedMetricEntry(now, totalBytes, totalCount, level, streamLbls),
newLbls,
)

Expand Down
Loading