Skip to content

Add structured metadata support for Loki output plugin #1579

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

error9098x
Copy link

@error9098x error9098x commented Apr 19, 2025

Signed-off-by: ProCodec 43810146+error9098x@users.noreply.github.com

What this PR does / why we need it:

Adds support for structured metadata in the Loki output plugin. This allows attaching high-cardinality data (like pod names, trace IDs, thread IDs) to log lines without using them as labels, following Loki's recommended best practices for Loki 3.0+.

The implementation adds:

  • structuredMetadata field to set key-value pairs as structured metadata
  • structuredMetadataKeys field to reference record keys for structured metadata
  • Processing logic in the Params() method to generate the proper Fluent Bit configuration
  • Test cases to verify the functionality

Which issue(s) this PR fixes:

Fixes #1551

Does this PR introduced a user-facing change?


Added support for structured metadata in Loki output plugin. Users can now configure high-cardinality data to be sent as structured metadata instead of labels.

Note: This feature requires Loki 3.0+ with schema v13 or higher, TSDB storage, and allow_structured_metadata: true in Loki configuration.

Additional documentation, usage docs, etc.:


Example usage:

apiVersion: fluentbit.fluent.io/v1alpha2
kind: ClusterOutput
metadata:
name: loki-output
spec:
match: kube.*
loki:
host: loki-gateway
port: 3100
labels:
- job=fluentbit
- environment=production
structuredMetadata:
pod: "${record['kubernetes']['pod_name']}"
      container: "${record['kubernetes']['container_name']}"
trace_id: "\${record['trace_id']}"
structuredMetadataKeys:
- level
- caller

For more info on structured metadata: https://grafana.com/docs/loki/latest/get-started/labels/structured-metadata/

Signed-off-by: ProCodec <43810146+error9098x@users.noreply.github.com>
@error9098x error9098x marked this pull request as ready for review April 19, 2025 23:23
@wenchajun
Copy link
Member

This one lacks the necessary crd

StructuredMetadata map[string]string `json:"structuredMetadata,omitempty"`
// Optional list of record keys that will be placed as structured metadata.
// This allows using record accessor patterns (e.g. $kubernetes['pod_name']) to reference record keys.
StructuredMetadataKeys []string `json:"structuredMetadataKeys,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcofranssen marcofranssen changed the title Add structured metadata support for Loki output plugin" Add structured metadata support for Loki output plugin May 2, 2025
@cw-Guo
Copy link
Collaborator

cw-Guo commented May 2, 2025

to resolve this issue, please run make generate, make manifests, and make docs-update

Signed-off-by: ProCodec <43810146+error9098x@users.noreply.github.com>
@error9098x
Copy link
Author

to resolve this issue, please run make generate, make manifests, and make docs-update

did that, is it ok now?

cw-Guo
cw-Guo previously approved these changes May 3, 2025
Copy link
Collaborator

@cw-Guo cw-Guo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cw-Guo cw-Guo self-requested a review May 3, 2025 02:15
@cw-Guo
Copy link
Collaborator

cw-Guo commented May 3, 2025

@error9098x , there is a failed test, can you please fix it?

Signed-off-by: ProCodec <43810146+error9098x@users.noreply.github.com>
@error9098x
Copy link
Author

@error9098x , there is a failed test, can you please fix it?

it appears that it was failing because of the label order difference in loki_type.go output and the test. I have added sorting mechanism to fix that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fluent-operator don't support set structured_metadata of loki
3 participants