prometheus / prometheus/client_js

Speed up label escaping

Open
#800 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement performance
Dominant language
JavaScript
Stars
3.5k
Forks
429
Avg merge
3d 3h
Merged PRs (30d)
22

Description

On the back of #798, there is now an opportunity to escape label values once at insert time rather than on every metrics call.

The metrics call is a high priority target for both time and space improvements. When I first came to the project, it was the most common complaint in the issue backlog. We have managed to speed up everything but this part. Collection is ~20% faster, aggregation 2.4x faster, but metrics are only a couple % faster.

With the introduction of #insert, we should be able to move the escapeString() call to insert. This could be done two ways:

  1. change the labels to only be escaped labels
  2. store the escaped form of the labels beside the original form

Number 2 may be more compatible, and since most strings do not need to be escaped in the first place, represent only a small increase in memory usage.

This work also may reduce or remove the need for the label cache in the Registry. Despite the use of a WeakMap for storage, this does represent a memory cost, and if the label concatenation is cheaper, then it's worth also considering whether the added overhead of the lookups is really worth the extra space and code, or whether the whole thing can be deleted.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing #798 and the insertion path introduced there, then trace escapeString() and the Registry label cache. Compare storing escaped labels beside originals with replacing the stored labels, and measure the effect on metrics time, memory, and compatibility. Done means metrics performance improves without breaking label behavior and the cache decision is supported by measurements.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
observability, performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.