victoriametrics: fix seriesByTag name tag mapping and quote-aware tag splitting#927
Conversation
1a84477 to
d515b62
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes two issues in the VictoriaMetrics seriesByTag(...) to PromQL conversion:
-
VictoriaMetrics name tag mapping: Changes the Graphite
nametag to map to VictoriaMetrics'__graphite__label instead of__name__. Previously used a fragilestrings.ReplaceAllapproach that could incorrectly match substrings like'hostname='. -
Quote-aware comma splitting: Implements a new
splitWithQuotesfunction to correctly handle tag values containing commas (e.g.,'desc=hello, world'). The previousstrings.Splitimplementation would incorrectly split on commas within quoted values.
Changes:
- Introduces
SeriesByTagToPromQLWithRenamesas a new public API that applies tag name transformations - Refactors
SeriesByTagToPromQLto delegate to a new internalconvertSeriesByTagToPromQLfunction - Adds
splitWithQuoteshelper for quote-aware string splitting - Adds safety check in
SplitTagValuesto skip malformed segments
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| zipper/protocols/victoriametrics/fetch.go | Replaces fragile string replacement with proper tag renaming using SeriesByTagToPromQLWithRenames to map name → __graphite__; includes minor style improvement |
| zipper/protocols/prometheus/helpers/helpers.go | Adds quote-aware splitting, new rename API, and safety guards; includes style improvements for string comparisons |
| zipper/protocols/prometheus/helpers/helpers_test.go | Adds tests for new split and rename functionality |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…aware tag splitting
Two fixes for seriesByTag conversion to PromQL:
1. VictoriaMetrics: replace fragile strings.ReplaceAll("'name=","'__name__=")
with a proper tag rename via new SeriesByTagToPromQLWithRenames helper.
The "name" tag (Graphite metric path) is now correctly mapped to
__graphite__ (matching how VM stores Graphite data), not __name__.
This is consistent with the non-seriesByTag path which already uses
{__graphite__="..."}.
2. SplitTagValues: use quote-aware comma splitting so that tag values
containing commas (e.g. seriesByTag('desc=hello, world')) are not
incorrectly split into separate arguments.
Includes tests for SplitTagValues and SeriesByTagToPromQLWithRenames.
Co-authored-by: Cursor <cursoragent@cursor.com>
d515b62 to
5aaf1ef
Compare
|
LGTM, merging this! |
Summary
Two fixes for
seriesByTag(...)conversion to PromQL:VictoriaMetrics: map
nametag to__graphite__instead of__name__.VictoriaMetrics stores Graphite metric paths in the
__graphite__label. The non-seriesByTag code path already uses{__graphite__=...}for plain targets. However, the seriesByTag branch usedstrings.ReplaceAllto rewrite'name='to'__name__=', which is incorrect for VM (selects the wrong label) and fragile (matches substrings like'hostname='). Replaced with a newSeriesByTagToPromQLWithRenameshelper that renames tags at the parsed-map level.Quote-aware comma splitting in
SplitTagValues.SplitTagValuesusedstrings.Split(query, ",")which incorrectly splits tag values that contain commas (e.g.seriesByTag('desc=hello, world')). AddedsplitWithQuotesthat respects single/double-quoted regions. Also added a guard against panics on malformed (too-short) segments.Changes
zipper/protocols/prometheus/helpers/helpers.go:splitWithQuotes(s, delimiter)— quote-aware string splitter.SplitTagValuesnow usessplitWithQuotesand skips segments shorter than 2 chars.SeriesByTagToPromQLWithRenames(step, target, tagRenames)— applies a tag rename map after parsing, before building PromQL. The originalSeriesByTagToPromQLis preserved as a zero-rename wrapper (fully backward-compatible).zipper/protocols/victoriametrics/fetch.go:strings.ReplaceAll(target.name, "'name=", "'__name__=")+SeriesByTagToPromQLwithSeriesByTagToPromQLWithRenames(..., {"name": "__graphite__"}).zipper/protocols/prometheus/helpers/helpers_test.go:TestSplitTagValues(simple, comma-in-value, empty, single tag).TestSeriesByTagToPromQLWithRenames(name to graphite rename, hostname unaffected, nil renames).Backward compatibility
SeriesByTagToPromQLsignature and behavior unchanged. All existing callers (Prometheus protocol) are unaffected.nametag now maps to__graphite__instead of__name__.Test plan
helpers_test.gotests pass.TestSplitTagValuescovers comma-in-value edge case.TestSeriesByTagToPromQLWithRenamescovers rename logic and no-false-positive on similar tag names (e.g.hostname).seriesByTag('name=servers.web.cpu')should produce{__graphite__="servers.web.cpu"}.