MCPcopy Index your code
hub / github.com/prometheus/client_python / Histogram

Class Histogram

prometheus_client/metrics.py:575–703  ·  view source on GitHub ↗

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(

Source from the content-addressed store, hash-verified

573
574
575class 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,

Callers 15

test_histogramMethod · 0.90
test_histogramMethod · 0.90
__init__.pyFile · 0.85
test_histogram_addsMethod · 0.85
test_collectMethod · 0.85
test_histogramMethod · 0.85
setUpMethod · 0.85

Calls

no outgoing calls

Tested by 15

test_histogramMethod · 0.72
test_histogramMethod · 0.72
test_histogram_addsMethod · 0.68
test_collectMethod · 0.68
test_histogramMethod · 0.68
setUpMethod · 0.68
test_setting_bucketsMethod · 0.68