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

chore: make error labels injectable via 'Add' #12251

Merged
merged 2 commits into from
Mar 20, 2024
Merged
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
11 changes: 11 additions & 0 deletions pkg/logql/log/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,17 @@ func (b *LabelsBuilder) Add(category LabelCategory, labels ...labels.Label) *Lab
if b.BaseHas(name) {
name = fmt.Sprintf("%s%s", name, duplicateSuffix)
}

if name == logqlmodel.ErrorLabel {
b.err = l.Value
continue
}

if name == logqlmodel.ErrorDetailsLabel {
b.errDetails = l.Value
continue
}

b.Set(category, name, l.Value)
}
return b
Expand Down
25 changes: 25 additions & 0 deletions pkg/logql/log/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,31 @@ func TestLabelsBuilder_LabelsError(t *testing.T) {
require.Equal(t, labels.FromStrings("already", "in"), lbs)
}

func TestLabelsBuilder_LabelsErrorFromAdd(t *testing.T) {
lbs := labels.FromStrings("already", "in")
b := NewBaseLabelsBuilder().ForLabels(lbs, lbs.Hash())
b.Reset()

// This works for any category
b.Add(StructuredMetadataLabel, labels.FromStrings(logqlmodel.ErrorLabel, "test error", logqlmodel.ErrorDetailsLabel, "test details")...)
lbsWithErr := b.LabelsResult()

expectedLbs := labels.FromStrings(
logqlmodel.ErrorLabel, "test error",
logqlmodel.ErrorDetailsLabel, "test details",
"already", "in",
)
require.Equal(t, expectedLbs, lbsWithErr.Labels())
require.Equal(t, expectedLbs.String(), lbsWithErr.String())
require.Equal(t, expectedLbs.Hash(), lbsWithErr.Hash())
require.Equal(t, labels.FromStrings("already", "in"), lbsWithErr.Stream())
require.Nil(t, lbsWithErr.StructuredMetadata())
require.Equal(t, labels.FromStrings(logqlmodel.ErrorLabel, "test error", logqlmodel.ErrorDetailsLabel, "test details"), lbsWithErr.Parsed())

// make sure the original labels is unchanged.
require.Equal(t, labels.FromStrings("already", "in"), lbs)
}

func TestLabelsBuilder_IntoMap(t *testing.T) {
strs := []string{
"namespace", "loki",
Expand Down
Loading