From be014397f3b9646878b89c98de335d6e348a1031 Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Tue, 19 Jan 2021 17:30:15 +0100 Subject: [PATCH] Fixes unwrap expressions from last optimization. Signed-off-by: Cyril Tovena --- pkg/logql/log/metrics_extraction.go | 1 + pkg/logql/log/metrics_extraction_test.go | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/logql/log/metrics_extraction.go b/pkg/logql/log/metrics_extraction.go index f710b47a1824a..1a5b446fdfbcd 100644 --- a/pkg/logql/log/metrics_extraction.go +++ b/pkg/logql/log/metrics_extraction.go @@ -144,6 +144,7 @@ func LabelExtractorWithStages( expectedLabels = append(expectedLabels, preStage.RequiredLabelNames()...) expectedLabels = append(expectedLabels, groups...) expectedLabels = append(expectedLabels, postFilter.RequiredLabelNames()...) + expectedLabels = append(expectedLabels, labelName) expectedLabels = uniqueString(expectedLabels) } return &labelSampleExtractor{ diff --git a/pkg/logql/log/metrics_extraction_test.go b/pkg/logql/log/metrics_extraction_test.go index 1318c157b3218..87e7970eb333c 100644 --- a/pkg/logql/log/metrics_extraction_test.go +++ b/pkg/logql/log/metrics_extraction_test.go @@ -3,6 +3,7 @@ package log import ( "sort" "testing" + "time" "github.com/prometheus/prometheus/pkg/labels" "github.com/stretchr/testify/require" @@ -126,6 +127,15 @@ func Test_labelSampleExtractor_Extract(t *testing.T) { } } +func Test_Extract_ExpectedLabels(t *testing.T) { + ex := mustSampleExtractor(LabelExtractorWithStages("duration", ConvertDuration, []string{"foo"}, false, false, []Stage{NewJSONParser()}, NoopStage)) + + f, lbs, ok := ex.ForStream(labels.Labels{{Name: "bar", Value: "foo"}}).ProcessString(`{"duration":"20ms","foo":"json"}`) + require.True(t, ok) + require.Equal(t, (20 * time.Millisecond).Seconds(), f) + require.Equal(t, labels.Labels{{Name: "foo", Value: "json"}}, lbs.Labels()) +} + func mustSampleExtractor(ex SampleExtractor, err error) SampleExtractor { if err != nil { panic(err) @@ -134,7 +144,6 @@ func mustSampleExtractor(ex SampleExtractor, err error) SampleExtractor { } func TestNewLineSampleExtractor(t *testing.T) { - se, err := NewLineSampleExtractor(CountExtractor, nil, nil, false, false) require.NoError(t, err) lbs := labels.Labels{