8000 _MultiProcessValue should resolve pid dynamically by dkruchinin · Pull Request #173 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

_MultiProcessValue should resolve pid dynamically #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions prometheus_client/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ def close(self):
self._f = None


def _MultiProcessValue(__pid=os.getpid()):
pid = __pid
def _MultiProcessValue(__pid=None):
_pid = __pid
files = {}
# Use a single global lock when in multi-processing mode
# as we presume this means there is no threading going on.
Expand All @@ -406,6 +406,7 @@ class _MmapedValue(object):
_multiprocess = True

def __init__(self, typ, metric_name, name, labelnames, labelvalues, multiprocess_mode='', **kwargs):
pid = _pid if _pid is not None else os.getpid()
if typ == 'gauge':
file_prefix = typ + '_' + multiprocess_mode
else:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tempfile
import time
import unittest
from multiprocessing import Process

import prometheus_client
from prometheus_client.core import *
Expand All @@ -22,6 +23,20 @@ def tearDown(self):
shutil.rmtree(self.tempdir)
prometheus_client.core._ValueClass = prometheus_client.core._MutexValue

def test_mpval_different_pids(self):
def worker_fn():
prometheus_client.core._ValueClass = prometheus_client.core._MultiProcessValue()
c = Counter('mpc', 'help', registry=None)
c.inc()

procs = [Process(target=worker_fn) for _ in range(4)]
for p in procs:
p.start()
for p in procs:
p.join()

self.assertEqual(4, self.registry.get_sample_value('mpc'))

def test_counter_adds(self):
c1 = Counter('c', 'help', registry=None)
prometheus_client.core._ValueClass = prometheus_client.core._MultiProcessValue(456)
Expand Down
0