Skip to content

LOG-8386: Initialize custom Loki label keys in VRL remap#3248

Open
vparfonov wants to merge 1 commit intoopenshift:release-6.4from
vparfonov:log8386
Open

LOG-8386: Initialize custom Loki label keys in VRL remap#3248
vparfonov wants to merge 1 commit intoopenshift:release-6.4from
vparfonov:log8386

Conversation

@vparfonov
Copy link
Copy Markdown
Contributor

@vparfonov vparfonov commented Apr 10, 2026

Description

When users specify custom labelKeys in Loki output, they were not initialized in the VRL remap transform, causing empty strings in Loki. Add remapLabelKeys() to initialize both container metadata fields and custom user-specified fields that are read directly from log records.

/cc @cahartma @Clee2691
/assign @jcantrill

Links

@qodo-code-review
Copy link
Copy Markdown

Review Summary by Qodo

Initialize custom Loki label keys in VRL remap transform

✨ Enhancement

Grey Divider

Walkthroughs

Description
• Initialize custom Loki label keys in VRL remap transform
• Add remapLabelKeys() function to include container metadata and custom fields
• Prevent missing field errors when users specify custom labelKeys in Loki output
• Update remap labels to use dynamic label key initialization
Diagram
flowchart LR
  A["User Custom LabelKeys"] --> B["remapLabelKeys Function"]
  C["Container Metadata Labels"] --> B
  B --> D["VRL Remap Transform"]
  D --> E["Loki Output with Initialized Fields"]
Loading

Grey Divider

File Changes

1. internal/generator/vector/output/loki/loki.go ✨ Enhancement +19/-1

Add dynamic label key initialization function

• Added remapLabelKeys() function to dynamically initialize label keys
• Function combines container metadata labels with custom user-specified labelKeys
• Filters out keys that are environment variables or mapped to other fields
• Updated RemapLabels() to use remapLabelKeys() instead of static containerLabels

internal/generator/vector/output/loki/loki.go


2. internal/generator/vector/output/loki/with_custom_labels.toml ⚙️ Configuration changes +6/-3

Initialize additional Kubernetes label fields in VRL

• Added VRL initialization checks for kubernetes.container_name and kubernetes.labels.app
• Reordered initialization checks for consistency
• Ensures all custom label fields are initialized to empty strings if missing

internal/generator/vector/output/loki/with_custom_labels.toml


Grey Divider

Qodo Logo

@qodo-code-review
Copy link
Copy Markdown

qodo-code-review bot commented Apr 10, 2026

Code Review by Qodo

🐞 Bugs (1)   📘 Rule violations (0)   📎 Requirement gaps (0)   🎨 UX Issues (0)
🐞\ ≡ Correctness (1)

Grey Divider


Action required

1. Invalid VRL for labelKeys 🐞
Description
remapLabelKeys() inserts raw spec.Loki.LabelKeys into remapLabelsVrl(), which emits
exists(.<key>) and assignments using the unsanitized key string. For Kubernetes labelKeys
containing slashes/dashes/dots (e.g. kubernetes.labels.app.kubernetes.io/name), this produces
syntactically invalid VRL (and does not initialize the sanitized field that the Loki sink template
actually reads), which can prevent the collector from starting.
Code

internal/generator/vector/output/loki/loki.go[R286-306]

+func remapLabelKeys(l *obs.Loki) []string {
+	if l == nil || len(l.LabelKeys) == 0 {
+		return containerLabels
+	}
+	keys := *sets.NewString(containerLabels...)
+	for _, k := range l.LabelKeys {
+		// Only add custom keys that are read directly from the record,
+		// skip keys that are env vars or mapped to other fields.
+		if generateCustomLabelValues(k) == "" {
+			keys.Insert(k)
+		}
+	}
+	return keys.List()
+}
+
func RemapLabels(id string, o obs.OutputSpec, inputs []string) Element {
	return Remap{
		ComponentID: id,
		Inputs:      vectorhelpers.MakeInputs(inputs...),
-		VRL:         remapLabelsVrl(containerLabels),
+		VRL:         remapLabelsVrl(remapLabelKeys(o.Loki)),
	}
Evidence
The generated VRL remap source uses the label key verbatim as a VRL field path. However, elsewhere
the generator explicitly sanitizes/quotes Kubernetes label keys for template access, and there is an
existing functional test that configures labelKeys with slashes/dashes and expects the collector to
start. With this PR, those same labelKeys will now be injected into the VRL remap initialization,
yielding invalid field paths like .kubernetes.labels.app.kubernetes.io/name and
.kubernetes.labels.prefix-cloud_com_platform-stage.

internal/generator/vector/output/loki/loki.go[260-269]
internal/generator/vector/output/loki/loki.go[272-280]
internal/generator/vector/output/loki/loki.go[283-299]
internal/generator/vector/output/loki/loki.go[301-306]
test/functional/outputs/loki/application_logs_vector_test.go[70-81]
internal/generator/vector/output/loki/loki_test.go[14-21]
internal/generator/vector/output/loki/loki_test.go[34-37]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
The remap VRL initializer is generated from raw `Loki.LabelKeys`, but VRL field paths cannot contain characters like `/` or `-` unquoted. This breaks configurations where users specify Kubernetes labelKeys like `kubernetes.labels.app.kubernetes.io/name` or `kubernetes.labels.prefix-cloud_com_platform-stage` (covered by an existing functional test).

### Issue Context
The Loki sink label *template* path already sanitizes these Kubernetes labelKeys (replacing `/` and `.` with `_` and quoting the resulting key segment). The VRL remap initializer must initialize the same sanitized/quoted record path that the template reads, and must never emit invalid VRL.

### Fix Focus Areas
- internal/generator/vector/output/loki/loki.go[260-306]

### Suggested fix approach
1. Introduce a helper that converts a labelKey into a VRL-safe field path **for initialization**:
  - For `kubernetes.labels.*` and `kubernetes.namespace_labels.*`:
    - Split at `labels.`
    - Replace `/` and `.` with `_` in the suffix (same as `formatLokiLabelValue`)
    - Return `kubernetes.labels."<sanitized>"` **without backslashes** in the actual VRL output, i.e. `kubernetes.labels."..."` should become `kubernetes.labels."..."` in Go but result in VRL: `.kubernetes.labels."..."`? (Prefer building the VRL as `kubernetes.labels."..."` in the string so the emitted VRL is `.kubernetes.labels."..."` with real quotes, not backslashes.)
    - Ensure keys containing `-` are quoted (these prefixes should always be quoted).
  - For other keys, keep as-is.
2. In `remapLabelKeys()`, insert the VRL-safe path (not the raw key) into the set.
3. Add/adjust unit coverage for remap initialization generation using a complex key like `kubernetes.labels.app.kubernetes.io/name` to prevent regressions.
4. Update expected TOML fixtures if the emitted VRL changes (e.g., `.kubernetes.labels."app"` vs `.kubernetes.labels.app`).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

@openshift-ci-robot openshift-ci-robot added the jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. label Apr 10, 2026
@openshift-ci-robot
Copy link
Copy Markdown

openshift-ci-robot commented Apr 10, 2026

@vparfonov: This pull request references LOG-8386 which is a valid jira issue.

Details

In response to this:

Description

When users specify custom labelKeys in Loki output, they were not initialized in the VRL remap transform, causing empty strings in Loki. Add remapLabelKeys() to initialize both container metadata fields and custom user-specified fields that are read directly from log records.

/cc @cahartma @Clee2691
/assign @jcantrill

Links

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

Comment thread internal/generator/vector/output/loki/loki.go
@openshift-ci
Copy link
Copy Markdown
Contributor

openshift-ci bot commented Apr 10, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: vparfonov
Once this PR has been reviewed and has the lgtm label, please assign xperimental for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

When users specify custom labelKeys in Loki output configuration, they were
not being properly initialized in the VRL remap transform, causing empty
strings in Loki.

Fix two issues:

Add remapLabelKeys() that returns both the required container metadata fields
(kubernetes.namespace_name, kubernetes.pod_name, kubernetes.container_name)
plus any user-specified custom labelKeys that are read directly from log records.
Labels sanitizes by replacing `/` and `.` with `_` and quotes it.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
@vparfonov
Copy link
Copy Markdown
Contributor Author

/retest

@openshift-ci
Copy link
Copy Markdown
Contributor

openshift-ci bot commented Apr 16, 2026

@vparfonov: all tests passed!

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jira/valid-reference Indicates that this PR references a valid Jira ticket of any type.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants