Skip to content

Feature: user defined pidprovider #441

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 3 commits 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
2 changes: 1 addition & 1 deletion prometheus_client/mmap_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _read_all_values(data, used=0):
if encoded_len + pos > used:
raise RuntimeError('Read beyond file size detected, file is corrupted.')
pos += 4
encoded_key = data[pos : pos + encoded_len]
encoded_key = data[pos: pos + encoded_len]
padded_len = encoded_len + (8 - (encoded_len + 4) % 8)
pos += padded_len
value = _unpack_double(data, pos)[0]
Expand Down
9 changes: 9 additions & 0 deletions prometheus_client/pidprovider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os


class Pidprovider(object):
source = os.getpid

@classmethod
def getpid(cls):
return cls.source()
3 changes: 2 additions & 1 deletion prometheus_client/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from threading import Lock

from .mmap_dict import mmap_key, MmapedDict
from .pidprovider import Pidprovider


class MutexValue(object):
Expand All @@ -28,7 +29,7 @@ def get(self):
return self._value


def MultiProcessValue(_pidFunc=os.getpid):
def MultiProcessValue(_pidFunc=Pidprovider.getpid):
files = {}
values = []
pid = {'value': _pidFunc()}
Expand Down
45 changes: 45 additions & 0 deletions tests/test_pidprovider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import shutil
import sys
import tempfile

from prometheus_client import values
from prometheus_client.core import Counter
from prometheus_client.values import MultiProcessValue, Pidprovider

if sys.version_info < (2, 7):
# We need the skip decorators from unittest2 on Python 2.6.
import unittest2 as unittest
else:
import unittest


class TestPidprovider(unittest.TestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
os.environ['prometheus_multiproc_dir'] = self.tempdir
self.originalValueClass = values.ValueClass
self.originalPidproviderSource = Pidprovider.source
values.ValueClass = MultiProcessValue()

def tearDown(self):
del os.environ['prometheus_multiproc_dir']
shutil.rmtree(self.tempdir)
values.ValueClass = self.originalValueClass
Pidprovider.source = self.originalPidproviderSource

# can not inspect the files cache directly, as it's a closure, so we
# check for the actual files themselves
def _files(self):
fs = os.listdir(self.tempdir)
fs.sort()
return fs

def test_with_default_pidprovider(self):
Counter('c1', 'c1', registry=None)
self.assertEqual(self._files(), ['counter_{0}.db'.format(os.getpid())])

def test_with_user_defined_pidprovider(self):
Pidprovider.source = staticmethod(lambda: 1234)
Counter('c1', 'c1', registry=None)
self.assertEqual(self._files(), ['counter_1234.db'])