Skip to content

victoriametrics: fix seriesByTag name tag mapping and quote-aware tag splitting#927

Merged
deniszh merged 1 commit into
go-graphite:mainfrom
rassvetalov:fix/vm-seriesbytag-name-to-graphite
Feb 12, 2026
Merged

victoriametrics: fix seriesByTag name tag mapping and quote-aware tag splitting#927
deniszh merged 1 commit into
go-graphite:mainfrom
rassvetalov:fix/vm-seriesbytag-name-to-graphite

Conversation

@rassvetalov
Copy link
Copy Markdown
Contributor

@rassvetalov rassvetalov commented Feb 12, 2026

Summary

Two fixes for seriesByTag(...) conversion to PromQL:

  1. VictoriaMetrics: map name tag 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 used strings.ReplaceAll to rewrite 'name=' to '__name__=', which is incorrect for VM (selects the wrong label) and fragile (matches substrings like 'hostname='). Replaced with a new SeriesByTagToPromQLWithRenames helper that renames tags at the parsed-map level.

  2. Quote-aware comma splitting in SplitTagValues.
    SplitTagValues used strings.Split(query, ",") which incorrectly splits tag values that contain commas (e.g. seriesByTag('desc=hello, world')). Added splitWithQuotes that respects single/double-quoted regions. Also added a guard against panics on malformed (too-short) segments.

Changes

  • zipper/protocols/prometheus/helpers/helpers.go:
    • Add splitWithQuotes(s, delimiter) — quote-aware string splitter.
    • SplitTagValues now uses splitWithQuotes and skips segments shorter than 2 chars.
    • Add SeriesByTagToPromQLWithRenames(step, target, tagRenames) — applies a tag rename map after parsing, before building PromQL. The original SeriesByTagToPromQL is preserved as a zero-rename wrapper (fully backward-compatible).
  • zipper/protocols/victoriametrics/fetch.go:
    • Replace strings.ReplaceAll(target.name, "'name=", "'__name__=") + SeriesByTagToPromQL with SeriesByTagToPromQLWithRenames(..., {"name": "__graphite__"}).
  • zipper/protocols/prometheus/helpers/helpers_test.go:
    • Add TestSplitTagValues (simple, comma-in-value, empty, single tag).
    • Add TestSeriesByTagToPromQLWithRenames (name to graphite rename, hostname unaffected, nil renames).

Backward compatibility

  • SeriesByTagToPromQL signature and behavior unchanged. All existing callers (Prometheus protocol) are unaffected.
  • Only the VictoriaMetrics Fetch path changes behavior: name tag now maps to __graphite__ instead of __name__.

Test plan

  • Existing helpers_test.go tests pass.
  • New TestSplitTagValues covers comma-in-value edge case.
  • New TestSeriesByTagToPromQLWithRenames covers rename logic and no-false-positive on similar tag names (e.g. hostname).
  • Manual test with VictoriaMetrics + Graphite ingestion: seriesByTag('name=servers.web.cpu') should produce {__graphite__="servers.web.cpu"}.

@rassvetalov rassvetalov force-pushed the fix/vm-seriesbytag-name-to-graphite branch 2 times, most recently from 1a84477 to d515b62 Compare February 12, 2026 08:31
@deniszh deniszh requested a review from Copilot February 12, 2026 09:15
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes two issues in the VictoriaMetrics seriesByTag(...) to PromQL conversion:

  1. VictoriaMetrics name tag mapping: Changes the Graphite name tag to map to VictoriaMetrics' __graphite__ label instead of __name__. Previously used a fragile strings.ReplaceAll approach that could incorrectly match substrings like 'hostname='.

  2. Quote-aware comma splitting: Implements a new splitWithQuotes function to correctly handle tag values containing commas (e.g., 'desc=hello, world'). The previous strings.Split implementation would incorrectly split on commas within quoted values.

Changes:

  • Introduces SeriesByTagToPromQLWithRenames as a new public API that applies tag name transformations
  • Refactors SeriesByTagToPromQL to delegate to a new internal convertSeriesByTagToPromQL function
  • Adds splitWithQuotes helper for quote-aware string splitting
  • Adds safety check in SplitTagValues to 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.

Comment thread zipper/protocols/prometheus/helpers/helpers.go
Comment thread zipper/protocols/prometheus/helpers/helpers_test.go
Comment thread zipper/protocols/prometheus/helpers/helpers.go
Comment thread zipper/protocols/prometheus/helpers/helpers_test.go
…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>
@rassvetalov rassvetalov force-pushed the fix/vm-seriesbytag-name-to-graphite branch from d515b62 to 5aaf1ef Compare February 12, 2026 10:15
@deniszh
Copy link
Copy Markdown
Member

deniszh commented Feb 12, 2026

LGTM, merging this!
Thanks a lot for your contribution, @rassvetalov !

@deniszh deniszh merged commit 7f897ac into go-graphite:main Feb 12, 2026
9 checks passed
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.

3 participants