A Summary tracks the size and number of events. Example use cases for Summaries: - Response latency - Request size Example for a Summary: from prometheus_client import Summary s = Summary('request_size_bytes', 'Request size (bytes)') s.observe(512) # Obse
| 503 | |
| 504 | |
| 505 | class Summary(MetricWrapperBase): |
| 506 | """A Summary tracks the size and number of events. |
| 507 | |
| 508 | Example use cases for Summaries: |
| 509 | - Response latency |
| 510 | - Request size |
| 511 | |
| 512 | Example for a Summary: |
| 513 | |
| 514 | from prometheus_client import Summary |
| 515 | |
| 516 | s = Summary('request_size_bytes', 'Request size (bytes)') |
| 517 | s.observe(512) # Observe 512 (bytes) |
| 518 | |
| 519 | Example for a Summary using time: |
| 520 | |
| 521 | from prometheus_client import Summary |
| 522 | |
| 523 | REQUEST_TIME = Summary('response_latency_seconds', 'Response latency (seconds)') |
| 524 | |
| 525 | @REQUEST_TIME.time() |
| 526 | def create_response(request): |
| 527 | '''A dummy function''' |
| 528 | time.sleep(1) |
| 529 | |
| 530 | Example for using the same Summary object as a context manager: |
| 531 | |
| 532 | with REQUEST_TIME.time(): |
| 533 | pass # Logic to be timed |
| 534 | """ |
| 535 | _type = 'summary' |
| 536 | _reserved_labelnames = ['quantile'] |
| 537 | |
| 538 | def _metric_init(self) -> None: |
| 539 | self._count = values.ValueClass(self._type, self._name, self._name + '_count', self._labelnames, |
| 540 | self._labelvalues, self._documentation) |
| 541 | self._sum = values.ValueClass(self._type, self._name, self._name + '_sum', self._labelnames, self._labelvalues, self._documentation) |
| 542 | self._created = time.time() |
| 543 | |
| 544 | def observe(self, amount: float) -> None: |
| 545 | """Observe the given amount. |
| 546 | |
| 547 | The amount is usually positive or zero. Negative values are |
| 548 | accepted but prevent current versions of Prometheus from |
| 549 | properly detecting counter resets in the sum of |
| 550 | observations. See |
| 551 | https://prometheus.io/docs/practices/histograms/#count-and-sum-of-observations |
| 552 | for details. |
| 553 | """ |
| 554 | self._raise_if_not_observable() |
| 555 | self._count.inc(1) |
| 556 | self._sum.inc(amount) |
| 557 | |
| 558 | def time(self) -> Timer: |
| 559 | """Time a block of code or function, and observe the duration in seconds. |
| 560 | |
| 561 | Can be used as a function decorator or context manager. |
| 562 | """ |
no outgoing calls