Skip to content

Commit

Permalink
chore: make error labels injectable via 'Add' (#12251)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasslessParticle authored Mar 20, 2024
1 parent fcd3e33 commit 9e66e05
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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

0 comments on commit 9e66e05

Please sign in to comment.