Skip to content
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

include timestamp in samples returned by MultiProcessCollector._accumulate_metrics #1057

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions prometheus_client/multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ def _accumulate_metrics(metrics, accumulate):
sample_timestamps = defaultdict(float)
buckets = defaultdict(lambda: defaultdict(float))
samples_setdefault = samples.setdefault
generate_pidless_key = lambda x, y: (x, tuple(l for l in y if l[0] != 'pid'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than x, y can we keep name, labels so we know what each variable is? It would also be fine to move this to a real func instead of a lambda if there are more name conflicts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick bump on this request, otherwise good to merge.

for s in metric.samples:
name, labels, value, timestamp, exemplar = s
if metric.type == 'gauge':
without_pid_key = (name, tuple(l for l in labels if l[0] != 'pid'))
without_pid_key = generate_pidless_key(name, labels)
if metric._multiprocess_mode in ('min', 'livemin'):
current = samples_setdefault(without_pid_key, value)
if value < current:
Expand Down Expand Up @@ -150,7 +151,13 @@ def _accumulate_metrics(metrics, accumulate):
samples[(metric.name + '_count', labels)] = acc

# Convert to correct sample format.
metric.samples = [Sample(name_, dict(labels), value) for (name_, labels), value in samples.items()]
timestamped_samples = []
for (name_, labels), value in samples.items():
without_pid_key = generate_pidless_key(name_, labels)
timestamped_samples.append(
Sample(name_, dict(labels), value, sample_timestamps[without_pid_key] or None)
)
metric.samples = timestamped_samples
return metrics.values()

def collect(self):
Expand Down