From 9e66e05e688d9a78daedd61363722a8e868eeed0 Mon Sep 17 00:00:00 2001 From: Travis Patterson Date: Wed, 20 Mar 2024 08:34:47 -0600 Subject: [PATCH] chore: make error labels injectable via 'Add' (#12251) --- pkg/logql/log/labels.go | 11 +++++++++++ pkg/logql/log/labels_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/pkg/logql/log/labels.go b/pkg/logql/log/labels.go index 10414a8ed3b88..d93af8a845e83 100644 --- a/pkg/logql/log/labels.go +++ b/pkg/logql/log/labels.go @@ -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 diff --git a/pkg/logql/log/labels_test.go b/pkg/logql/log/labels_test.go index 0f859c8125420..e42a330dcbc3a 100644 --- a/pkg/logql/log/labels_test.go +++ b/pkg/logql/log/labels_test.go @@ -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",