prometheus / prometheus/client_python

Default namespace/prefix for all exported metrics?

Open
#712 9 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature
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

Open the contributing guide

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.