From a623350b45e8c14a293f123da025dfe5a59352aa Mon Sep 17 00:00:00 2001 From: Mark Motliuk Date: Mon, 11 Mar 2024 13:23:31 +0200 Subject: [PATCH] loki.source.api: fix a bug where structured metadata is not passed downstream (#6508) --- CHANGELOG.md | 2 ++ .../api/internal/lokipush/push_api_server.go | 4 +++- .../api/internal/lokipush/push_api_server_test.go | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb3e060cd38e..223d3eae7bd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,8 @@ Main (unreleased) - Fix an issue where JSON string array elements were not parsed correctly in `loki.source.cloudflare`. (@thampiotr) +- Fix a bug where structured metadata and parsed field are not passed further in `loki.source.api` (@marchellodev) + ### Other changes - Clustering for Grafana Agent in Flow mode has graduated from beta to stable. diff --git a/internal/component/loki/source/api/internal/lokipush/push_api_server.go b/internal/component/loki/source/api/internal/lokipush/push_api_server.go index 1b73950cc664..1842522dfae3 100644 --- a/internal/component/loki/source/api/internal/lokipush/push_api_server.go +++ b/internal/component/loki/source/api/internal/lokipush/push_api_server.go @@ -185,7 +185,9 @@ func (s *PushAPIServer) handleLoki(w http.ResponseWriter, r *http.Request) { e := loki.Entry{ Labels: filtered.Clone(), Entry: logproto.Entry{ - Line: entry.Line, + Line: entry.Line, + StructuredMetadata: entry.StructuredMetadata, + Parsed: entry.Parsed, }, } if keepTimestamp { diff --git a/internal/component/loki/source/api/internal/lokipush/push_api_server_test.go b/internal/component/loki/source/api/internal/lokipush/push_api_server_test.go index 7e38572437c6..de68ffaf64ba 100644 --- a/internal/component/loki/source/api/internal/lokipush/push_api_server_test.go +++ b/internal/component/loki/source/api/internal/lokipush/push_api_server_test.go @@ -23,6 +23,7 @@ import ( frelabel "github.com/grafana/agent/internal/component/common/relabel" "github.com/grafana/dskit/flagext" "github.com/grafana/loki/pkg/logproto" + "github.com/grafana/loki/pkg/push" "github.com/grafana/river" "github.com/phayes/freeport" "github.com/prometheus/client_golang/prometheus" @@ -79,6 +80,10 @@ regex = "dropme" Entry: logproto.Entry{ Timestamp: time.Unix(int64(i), 0), Line: "line" + strconv.Itoa(i), + StructuredMetadata: push.LabelsAdapter{ + {Name: "i", Value: strconv.Itoa(i)}, + {Name: "anotherMetaData", Value: "val"}, + }, }, } } @@ -98,9 +103,18 @@ regex = "dropme" "pushserver": "pushserver1", "stream": "stream1", } + + expectedStructuredMetadata := push.LabelsAdapter{ + {Name: "i", Value: strconv.Itoa(0)}, + {Name: "anotherMetaData", Value: "val"}, + } + // Spot check the first value in the result to make sure relabel rules were applied properly require.Equal(t, expectedLabels, eh.Received()[0].Labels) + // Spot check the first value in the result to make sure structured metadata was received properly + require.Equal(t, expectedStructuredMetadata, eh.Received()[0].StructuredMetadata) + // With keep timestamp enabled, verify timestamp require.Equal(t, time.Unix(99, 0).Unix(), eh.Received()[99].Timestamp.Unix())