Skip to content

Ignore foreign .db files in the multiprocess directory - #1238

Open
Om-singhaI wants to merge 1 commit into
prometheus:masterfrom
Om-singhaI:fix/multiproc-only-own-db-files
Open

Om-singhaI wants to merge 1 commit into
prometheus:masterfrom
Om-singhaI:fix/multiproc-only-own-db-files

Conversation

@Om-singhaI

Copy link
Copy Markdown

Fixes #878

MultiProcessCollector.collect() globbed every *.db file in PROMETHEUS_MULTIPROC_DIR and parsed each one as a metric file, so another database in the same directory breaks collection. I reproduced it on master with a small SQLite file next to a counter file: collect() raised struct.error: unpack_from requires a buffer of at least 1634562704 bytes. The reader takes the first 4 bytes of a file as its used size, which for a SQLite header is about 1.8 GB, so a large foreign file could also be read into memory up to that size.

With this change collect() only reads files named the way MultiProcessValue writes them: counter, histogram or summary, or gauge plus one of the modes in Gauge._MULTIPROC_MODES, then the process identifier and .db. The identifier can be any string, so a custom process_identifier still works. Other files are skipped. merge() is unchanged, so callers that pass their own file list see the same behavior as before. mark_process_dead() already removes only exact gauge_<mode>_<pid>.db names, so it needed no change.

The new test writes a counter and a livesum gauge under two process identifiers (one of them a string), then adds four junk files: app.db, cache_0.db, counter.db and gauge_unknown_0.db. It fails on master with the struct.error above and passes with the fix. pytest tests/test_multiprocess.py tests/test_core.py passes, and flake8 and isort are clean on the changed files.

A dedicated directory is still the right setup, as you said in the issue. This just keeps a stray file from taking the metrics endpoint down.

@csmarchbanks could you take a look when you have time?

MultiProcessCollector.collect() read every *.db file in
PROMETHEUS_MULTIPROC_DIR and parsed it as a metric file. Another
database in the same directory, such as a SQLite file, made collection
fail with struct.error. Because the first 4 bytes of a file are taken
as its used size, a large foreign file could also be read into memory
in full.

Only collect files named the way MultiProcessValue writes them: counter,
histogram or summary, or gauge plus a known multiprocess mode, followed
by the process identifier. The identifier can be any string, so custom
process_identifier functions keep working.

Signed-off-by: Om Singhal <wengsinghal@gmail.com>

This branch has not been deployed

No deployments
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.

Prometheus reads all .db files from PROMETHEUS_MULTIPROC_DIR without regard

1 participant