Skip to content
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
10 changes: 9 additions & 1 deletion prometheus_client/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,15 @@ def _metric_init(self):
self._count = values.ValueClass(self._type, self._name, self._name + '_count', self._labelnames,
self._labelvalues)
self._sum = values.ValueClass(self._type, self._name, self._name + '_sum', self._labelnames, self._labelvalues)
self._delta = 0.0
self._created = time.time()


def observe(self, amount):
"""Observe the given amount."""
self._count.inc(1)
self._sum.inc(amount)
self._delta = amount

def time(self):
"""Time a block of code or function, and observe the duration in seconds.
Expand All @@ -430,7 +433,8 @@ def _child_samples(self):
return (
('_count', {}, self._count.get()),
('_sum', {}, self._sum.get()),
('_created', {}, self._created))
('_created', {}, self._created),
('_delta', {}, self._delta))


class Histogram(MetricWrapperBase):
Expand Down Expand Up @@ -511,6 +515,7 @@ def _prepare_buckets(self, buckets):
def _metric_init(self):
self._buckets = []
self._created = time.time()
self._delta = 0.0
bucket_labelnames = self._labelnames + ('le',)
self._sum = values.ValueClass(self._type, self._name, self._name + '_sum', self._labelnames, self._labelvalues)
for b in self._upper_bounds:
Expand All @@ -525,6 +530,7 @@ def _metric_init(self):
def observe(self, amount):
"""Observe the given amount."""
self._sum.inc(amount)
self._delta = amount
for i, bound in enumerate(self._upper_bounds):
if amount <= bound:
self._buckets[i].inc(1)
Expand All @@ -546,6 +552,8 @@ def _child_samples(self):
samples.append(('_count', {}, acc))
samples.append(('_sum', {}, self._sum.get()))
samples.append(('_created', {}, self._created))
samples.append(('_delta', {}, self._delta))

return tuple(samples)


Expand Down
2 changes: 2 additions & 0 deletions tests/openmetrics/test_exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_summary(self):
ss_count{a="c",b="d"} 1.0
ss_sum{a="c",b="d"} 17.0
ss_created{a="c",b="d"} 123.456
ss_delta{a="c",b="d"} 17.0
# EOF
""", generate_latest(self.registry))

Expand Down Expand Up @@ -88,6 +89,7 @@ def test_histogram(self):
hh_count 1.0
hh_sum 0.05
hh_created 123.456
hh_delta 0.05
# EOF
""", generate_latest(self.registry))

Expand Down
2 changes: 2 additions & 0 deletions tests/test_exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def test_summary(self):
# TYPE ss summary
ss_count{a="c",b="d"} 1.0
ss_sum{a="c",b="d"} 17.0
ss_delta{a="c",b="d"} 17.0
# TYPE ss_created gauge
ss_created{a="c",b="d"} 123.456
""", generate_latest(self.registry))
Expand Down Expand Up @@ -109,6 +110,7 @@ def test_histogram(self):
hh_bucket{le="+Inf"} 1.0
hh_count 1.0
hh_sum 0.05
hh_delta 0.05
# TYPE hh_created gauge
hh_created 123.456
""", generate_latest(self.registry))
Expand Down