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

Port loki/#10752 to Agent #5481

Merged
merged 3 commits into from
Oct 16, 2023
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ Main (unreleased)

- Update Prometheus dependency to v2.47.2. (@tpaschalis)

- Allow converting labels to structured metadata with Loki's structured_metadata stage. (@gonzalesraul)


v0.37.1 (2023-10-10)
-----------------

Expand Down
22 changes: 21 additions & 1 deletion component/loki/process/stages/structured_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ func (s *structuredMetadataStage) Run(in chan Entry) chan Entry {
processLabelsConfigs(s.logger, e.Extracted, s.cfgs, func(labelName model.LabelName, labelValue model.LabelValue) {
e.StructuredMetadata = append(e.StructuredMetadata, logproto.LabelAdapter{Name: string(labelName), Value: string(labelValue)})
})
return e
return s.extractFromLabels(e)
})
}

func (s *structuredMetadataStage) extractFromLabels(e Entry) Entry {
labels := e.Labels
foundLabels := []model.LabelName{}

for lName, lSrc := range s.cfgs.Values {
labelKey := model.LabelName(*lSrc)
if lValue, ok := labels[labelKey]; ok {
e.StructuredMetadata = append(e.StructuredMetadata, logproto.LabelAdapter{Name: lName, Value: string(lValue)})
foundLabels = append(foundLabels, labelKey)
}
}

// Remove found labels, do this after append to structure metadata
for _, fl := range foundLabels {
delete(labels, fl)
}
e.Labels = labels
return e
}
32 changes: 32 additions & 0 deletions component/loki/process/stages/structured_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ stage.labels {
}
`

var pipelineStagesStructuredMetadataFromStaticLabels = `
stage.static_labels {
values = {"component" = "querier", "pod" = "loki-querier-664f97db8d-qhnwg"}
}

stage.structured_metadata {
values = {"pod" = ""}
}
`

var pipelineStagesStructuredMetadataFromStaticLabelsDifferentKey = `
stage.static_labels {
values = {"component" = "querier", "pod" = "loki-querier-664f97db8d-qhnwg"}
}

stage.structured_metadata {
values = {"pod_name" = "pod"}
}
`

func Test_StructuredMetadataStage(t *testing.T) {
tests := map[string]struct {
pipelineStagesYaml string
Expand Down Expand Up @@ -104,6 +124,18 @@ func Test_StructuredMetadataStage(t *testing.T) {
expectedStructuredMetadata: push.LabelsAdapter{push.LabelAdapter{Name: "app", Value: "loki"}},
expectedLabels: model.LabelSet{model.LabelName("component"): model.LabelValue("ingester")},
},
"expected structured metadata and regular labels to be extracted with static labels stage and to be added to entry": {
pipelineStagesYaml: pipelineStagesStructuredMetadataFromStaticLabels,
logLine: `sample log line`,
expectedStructuredMetadata: push.LabelsAdapter{push.LabelAdapter{Name: "pod", Value: "loki-querier-664f97db8d-qhnwg"}},
expectedLabels: model.LabelSet{model.LabelName("component"): model.LabelValue("querier")},
},
"expected structured metadata and regular labels to be extracted with static labels stage using different structured key": {
pipelineStagesYaml: pipelineStagesStructuredMetadataFromStaticLabelsDifferentKey,
logLine: `sample log line`,
expectedStructuredMetadata: push.LabelsAdapter{push.LabelAdapter{Name: "pod_name", Value: "loki-querier-664f97db8d-qhnwg"}},
expectedLabels: model.LabelSet{model.LabelName("component"): model.LabelValue("querier")},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
Expand Down