A Histogram tracks the size and number of events in buckets. You can use Histograms for aggregatable calculation of quantiles. Example use cases: - Response latency - Request size Example for a Histogram: from prometheus_client import Histogram h = Histogram(
| 573 | |
| 574 | |
| 575 | class Histogram(MetricWrapperBase): |
| 576 | """A Histogram tracks the size and number of events in buckets. |
| 577 | |
| 578 | You can use Histograms for aggregatable calculation of quantiles. |
| 579 | |
| 580 | Example use cases: |
| 581 | - Response latency |
| 582 | - Request size |
| 583 | |
| 584 | Example for a Histogram: |
| 585 | |
| 586 | from prometheus_client import Histogram |
| 587 | |
| 588 | h = Histogram('request_size_bytes', 'Request size (bytes)') |
| 589 | h.observe(512) # Observe 512 (bytes) |
| 590 | |
| 591 | Example for a Histogram using time: |
| 592 | |
| 593 | from prometheus_client import Histogram |
| 594 | |
| 595 | REQUEST_TIME = Histogram('response_latency_seconds', 'Response latency (seconds)') |
| 596 | |
| 597 | @REQUEST_TIME.time() |
| 598 | def create_response(request): |
| 599 | '''A dummy function''' |
| 600 | time.sleep(1) |
| 601 | |
| 602 | Example of using the same Histogram object as a context manager: |
| 603 | |
| 604 | with REQUEST_TIME.time(): |
| 605 | pass # Logic to be timed |
| 606 | |
| 607 | The default buckets are intended to cover a typical web/rpc request from milliseconds to seconds. |
| 608 | They can be overridden by passing `buckets` keyword argument to `Histogram`. |
| 609 | """ |
| 610 | _type = 'histogram' |
| 611 | _reserved_labelnames = ['le'] |
| 612 | DEFAULT_BUCKETS = (.005, .01, .025, .05, .075, .1, .25, .5, .75, 1.0, 2.5, 5.0, 7.5, 10.0, INF) |
| 613 | |
| 614 | def __init__(self, |
| 615 | name: str, |
| 616 | documentation: str, |
| 617 | labelnames: Iterable[str] = (), |
| 618 | namespace: str = '', |
| 619 | subsystem: str = '', |
| 620 | unit: str = '', |
| 621 | registry: Optional[CollectorRegistry] = REGISTRY, |
| 622 | _labelvalues: Optional[Sequence[str]] = None, |
| 623 | buckets: Sequence[Union[float, str]] = DEFAULT_BUCKETS, |
| 624 | ): |
| 625 | self._prepare_buckets(buckets) |
| 626 | super().__init__( |
| 627 | name=name, |
| 628 | documentation=documentation, |
| 629 | labelnames=labelnames, |
| 630 | namespace=namespace, |
| 631 | subsystem=subsystem, |
| 632 | unit=unit, |
no outgoing calls