Skip to content

Metric without labelnames causes issues in when MULTIPROC is enabled #1123

Open

Description

@gg-mmill

Hello,
I noticed (see #902 (comment) for example ) that when

  • having a metric defined without a label
  • multiprocess mode is enabled (i.e. PROMETHEUS_MULTIPROC_DIR is set )
  • an app has a single process

then this causes issues, and some metrics will not be reported.

It's not exactly clear to me what is happening, but an indication that something is wrong is that a prometheus db file is created at load time (i.e. when metrics are defined, before they are set).

Here's what I have in more details:


Observations in a prod application:

  1. Some histogram metrics set in a threaded celery worker where PROMETHEUS_MULTIPROC_DIR are not reported
  2. No such issue in pre-fork workers
  3. The issue was resolved by adding the labelnames argument to a metric where it was missing (which was an histogram as well)

Other observations:

  1. When starting the app, a prom db file is created for the metric that was missing labelnames (before any measurement is made)

Hypothesis:

  • in a celery worker in pre-fork mode, the process creating the first db file is not the same process were metrics are set afterwards, so there is no "collision" (because of the pid suffix)
  • in a celery worker in threaded mode, there is a single process creating the first db file and setting the metrics, and somehow collisions happens and some metrics are not reported

Values set at startup:

$ curl localhost:8000

# HELP test_histogram_no_label test Histogram
# TYPE test_histogram_no_label histogram
test_histogram_no_label_sum 0.0
test_histogram_no_label_bucket{le="1.0"} 0.0
test_histogram_no_label_bucket{le="2.0"} 0.0
test_histogram_no_label_bucket{le="+Inf"} 0.0
test_histogram_no_label_count 0.0


It seems to me that a simple way to address this would be to raise when a metric is defined without labelnames, as labelnames are mandatory anyway.

Activity

  1. naveensheejachacko commented on Dec 5, 2026

    @naveensheejachacko

    I think I found what's going on here. It isn't threads, and it isn't specific to metrics without labels: it's the multiprocess directory being wiped after the process has already opened its .db file.

    A metric without labels creates its value as soon as it's defined, usually at import time. In multiprocess mode that opens and memory-maps <type>_<pid>.db. If something then deletes that file (a cleanup in a Celery worker_init handler, or an entrypoint that recreates the directory after the app module has been imported), the process keeps writing to the deleted file. Every metric of that type in the process stops being reported, including labelled metrics that are first used after the wipe, because they share the same per-type file.

    That matches all three observations:

    • Prefork worker fine, threaded worker broken: prefork children have a new pid, so __check_for_pid_change opens new files after the wipe. A threaded (or solo) worker is a single process and keeps the old, deleted file.
    • Adding labelnames "fixed" it: with labels, nothing is created until .labels() is first called, which happens after the wipe, so a fresh file is created.
    • A .db file exists right after start-up: that's the metric without labels being initialised at definition time.

    Reproduction (no Celery needed), on main:

    import os, tempfile
    d = tempfile.mkdtemp(); os.environ["PROMETHEUS_MULTIPROC_DIR"] = d
    from prometheus_client import Counter, CollectorRegistry, generate_latest, multiprocess
    
    c_nolabel = Counter("c_nolabel", "defined at import, no labels")
    c_label = Counter("c_label", "labels, first used after the wipe", ["k"])
    
    for f in os.listdir(d):             # e.g. cleanup in a worker_init hook
        os.remove(os.path.join(d, f))
    
    c_nolabel.inc(); c_label.labels(k="a").inc()
    
    reg = CollectorRegistry(); multiprocess.MultiProcessCollector(reg)
    print(generate_latest(reg).decode())  # neither counter is exported
    
    if os.fork() == 0:                    # a child forked after the wipe is fine
        c_nolabel.inc(); os._exit(0)
    os.wait()
    reg = CollectorRegistry(); multiprocess.MultiProcessCollector(reg)
    print(generate_latest(reg).decode())  # c_nolabel_total 1.0 now appears

    Detecting the deleted file at write time would cost a stat on every inc()/observe(), so I don't think the library should try to recover. The docs say the directory "must be wiped between process/Gunicorn runs (before startup is recommended)", but they don't say what breaks if you wipe later. I've opened a docs PR that makes the timing explicit and describes this symptom: #1270

    @gg-mmill, does your setup clean or recreate the directory anywhere after the app is imported? That would confirm it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions