Skip to content

Store counter keys raw and convert them on read - #45

Merged
jessestimpson merged 2 commits into
masterfrom
feature/raw-counter-keys
Oct 26, 2026
Merged

jessestimpson merged 2 commits into
masterfrom
feature/raw-counter-keys

Conversation

@jessestimpson

Copy link
Copy Markdown
Member
  1. Store {Name, Tags} directly in ets table, speeding up calls to add/*: buys about +20% throughput gain on benchmarks.
  2. Store separator in persistent_term. This isn't for performance, though it doesn't hurt. This change exists to make the grouping of metrics predictable by disallowing change of separator.

@jessestimpson

Copy link
Copy Markdown
Member Author

Write cost, single process (measured in-process, 3M calls)

call master this PR
add(Name) 134 ns, 25 reductions, 45 words garbage ~65 ns (61–68), 7 reductions, 9 words
add(Name, #{code => "200"}) 186 ns, 32 reductions, 62 words ~88 ns, 7 reductions, 9 words
add_m(Name, Key) 201 ns, 35 reductions, 72 words ~78–94 ns, 8 reductions, 13 words
imetrics_hist_openmetrics:add 308 ns 308 ns (unchanged, histograms still convert on write)

Concurrent writes (18 schedulers)

benchmark master this PR
contended key, 18 workers (counter + histogram per op) 12.9M–13.2M ops/s 15.7M–16.3M ops/s (+~23%)
write scaling, add, all schedulers ~53M–57M calls/s ~76M–80M calls/s
write scaling, add_dim, all schedulers ~38M–40M calls/s ~65M–67M calls/s

Scrape time is unchanged (within run-to-run noise at 100, 1,000 and 10,000 series, on 1, 4 and 18 schedulers).

@jkashimba jkashimba left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It looks like this differs from 870357f in that it will allow a bad key write which then stays in ets. Is that the behavior you want?

@jessestimpson

Copy link
Copy Markdown
Member Author

It looks like this differs from 870357f in that it will allow a bad key write which then stays in ets. Is that the behavior you want?

Yeah I did make that simplifying tradeoff. The bad key will get dropped on scrape, but it will be in the table nonetheless.

Maybe the right call is to delete it from ets at scrape time? (cc @weaversam8 )

@weaversam8

Copy link
Copy Markdown
Member

This is a smart idea to improve performance, nice find!

I don't love the idea of scrapes having side effects (deleting keys) but it's probably better than leaving them there to slowly bloat memory otherwise. I'm OK with that approach.

Another idea could be to log (or set a gauge) on scrape indicating how many bad series exist in ETS and were scraped, so it's something we can monitor and address manually?

@jessestimpson

Copy link
Copy Markdown
Member Author

I opted for discarding on scrape and setting a counter+exemplar.

@jessestimpson
jessestimpson merged commit 2580693 into master Oct 26, 2026
2 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