Skip to content

perf(metric): cache attribute-exclude filter decisions per key (mondaytweaks flag)#14

Draft
arutkowski00 wants to merge 1 commit into
monday-tweaksfrom
feature/adamru/metric-exclude-labels-map
Draft

perf(metric): cache attribute-exclude filter decisions per key (mondaytweaks flag)#14
arutkowski00 wants to merge 1 commit into
monday-tweaksfrom
feature/adamru/metric-exclude-labels-map

Conversation

@arutkowski00

Copy link
Copy Markdown
Collaborator

What

Caches the attribute-exclude filter decision for each attribute key so the regex loop runs once per unique key rather than on every metric measurement.

Without caching, every call to AddCustomMetricAttributes iterates over all exclusion patterns for every attribute it receives — at high metric throughput this shows up as a measurable hot path. A 2026-07-10 production pprof analysis (12:00–12:05 UTC, 3 regions) confirmed the OTEL regex exclude filter at 4.4% CPU + 16.5 GB alloc/90 s.

New mondaytweaks flag

Flag Default Behaviour
CacheMetricAttributeExcludeDecisions true (on) Cache per-key exclude decisions in a sync.Map; skip regex loop on subsequent measurements
false Original behaviour — re-evaluate all patterns on every measurement

Benchmarks

Measured with go test -bench=BenchmarkMetricAttributeExclude -benchmem -count=5 on the same machine before and after the change.

Before

Pipeline ns/op B/op allocs/op
OTEL 1320 0 0
Prometheus 2130 350 15

After

Pipeline ns/op B/op allocs/op
OTEL 145 0 0
Prometheus 152 0 0

~9× faster for OTEL, ~14× faster for Prometheus; Prometheus allocations eliminated.

Source

Derives from the 2026-07-10 production pprof analysis of the Cosmo router.

…loop on every measurement

Add CacheMetricAttributeExcludeDecisions flag (default true) in mondaytweaks. When enabled,
both the Prometheus and OTEL attribute-filter closures use a sync.Map to memoize the
regex-loop result per distinct attribute.Key. Attribute keys are a small, bounded, static
set, so after warmup the hot path is a single map lookup (O(1), zero regex calls).

Benchmarks (15 keys, 3 regexes, Apple M4 Pro):
  OTEL  old: 1320 ns/op,  0 B/op,   0 allocs  →  new:  145 ns/op, 0 B/op, 0 allocs (~9x)
  Prom  old: 2130 ns/op, 350 B/op, 15 allocs  →  new:  152 ns/op, 0 B/op, 0 allocs (~14x, all allocs eliminated)

Semantics are identical: same regexes, same inputs, result cached per key.
@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown

Router image scan passed

✅ No security vulnerabilities found in image:

ghcr.io/mondaycom/cosmo/router:sha-8456100ce00e48fe1576873ffae01735696f78e4

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.

1 participant