prometheus / prometheus/client_python
Default namespace/prefix for all exported metrics?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.4k
- Forks
- 876
- Avg merge
- 8d 4h
- Merged PRs (30d)
- 1
Description
Is there a way to define a default, global prefix for all metrics?
For example instead of:
python_gc_objects_collected_total
I'd like to add the prefix myproject_:
myproject_python_gc_objects_collected_total
I'm currently doing it like that:
_original_generate_latest = prometheus_client.openmetrics.exposition.generate_latest
def init_prometheus(namespace):
def new_generate_latest(*args, **kwargs):
original_output = _original_generate_latest(*args, **kwargs).decode('utf-8')
namespace_prefix = namespace + '_'
new_output = ''
for line in original_output.splitlines():
if not line.startswith('#') and not line.startswith(namespace_prefix):
new_output += namespace_prefix
new_output += line + '\n'
new_output = new_output.encode('utf-8')
return new_output
prometheus_client.generate_latest = new_generate_latest
But it doesn't always work. For example, prometheus_client.start_http_server ignores this function for whatever reason.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the exposed generate_latest function and the prometheus_client.start_http_server entry point mentioned in the issue. Determine how exported metrics are produced through each path and define done as applying one configured prefix consistently to all exported metrics without monkey-patching.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- prometheus, python
- Domain
- observability
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100