Skip to content

Conversation

@hazel-shen
Copy link

What this PR does / why we need it:

Align LogQL label/stream regex matchers with Prometheus semantics: regex patterns are treated as fully anchored (implicitly ^…$). This removes unintended substring matches in label filters, restoring the documented behavior. Line filters (|~, !~) remain substring-based and are unchanged.

Which issue(s) this PR fixes:
Fixes #14433

Special notes for your reviewer:

Checklist

  • Reviewed the CONTRIBUTING.md guide (required)
  • Documentation added
  • Tests updated
  • Title matches the required conventional commits format, see here
    • Note that Promtail is considered to be feature complete, and future development for logs collection will be in Grafana Alloy. As such, feat PRs are unlikely to be accepted unless a case can be made for the feature actually being a bug fix to existing behavior.
  • Changes that require user attention or interaction to upgrade are documented in docs/sources/setup/upgrade/_index.md
  • If the change is deprecating or removing a configuration option, update the deprecated-config.yaml and deleted-config.yaml files respectively in the tools/deprecated-config-checker directory. Example PR

@hazel-shen hazel-shen requested a review from a team as a code owner November 10, 2025 04:15
@CLAassistant
Copy link

CLAassistant commented Nov 10, 2025

CLA assistant check
All committers have signed the CLA.

@hazel-shen
Copy link
Author

Manual verification

I validated the fully-anchored label regex semantics locally. For reviewers, here's a minimal repro script (uses ns timestamps and an isolated run label):

#!/bin/bash
LOKI=${LOKI:-http://localhost:3100}
RUN=$(date +%s); NOW=$RUN

# Push 4 test logs
for i in 0 1 2 3; do
  curl -sX POST "$LOKI/loki/api/v1/push" -H 'Content-Type: application/json' \
    -d '{"streams":[{"stream":{"run":"'$RUN'"},"values":[["'$NOW$i'00000000","test"]]}]}'>/dev/null
done
sleep 2

START=$((NOW-60)); END=$((NOW+60))

# Test: substring "23.*" should NOT match "1234" (issue #14433)
curl -sG "$LOKI/loki/api/v1/query_range" \
  --data-urlencode 'query={run="'$RUN'"} | label_format test="1234" | test=~"23.*"' \
  --data-urlencode "start=$START" --data-urlencode "end=$END" \
  | jq -r '.data.result[0].values | length // 0'  # Expected: 0

# Test: prefix "12.*" should match "1234"
curl -sG "$LOKI/loki/api/v1/query_range" \
  --data-urlencode 'query={run="'$RUN'"} | label_format test="1234" | test=~"12.*"' \
  --data-urlencode "start=$START" --data-urlencode "end=$END" \
  | jq -r '.data.result[0].values | length // 0'  # Expected: 4

Results:

  • With fix: 0 and 4
  • Before fix (bug): 4 and 4

I didn't commit the script to keep the PR minimal; happy to share a gist if useful.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regex label filter return wrong result

2 participants