33 $this->statsFactory = $statsFactory;
36 throw new InvalidArgumentException(
"Stats: ({$name}) Histogram buckets cannot be an empty array." );
38 $bucketCount = count( $buckets );
39 if ( $bucketCount > self::MAX_BUCKETS ) {
40 throw new InvalidArgumentException(
41 "Stats: ({$name}) Too many buckets defined. Got:{$bucketCount}, Max:" . self::MAX_BUCKETS
44 foreach ( $buckets as $bucket ) {
45 if ( !( is_float( $bucket ) || is_int( $bucket ) ) ) {
46 throw new InvalidArgumentException(
"Stats: ({$name}) Histogram buckets can only be float or int." );
49 $normalizedBuckets = array_unique( $buckets );
50 sort( $normalizedBuckets, SORT_NUMERIC );
51 if ( $buckets !== $normalizedBuckets ) {
52 throw new InvalidArgumentException(
53 "Stats: ({$name}) Histogram buckets must be unique and in order of least to greatest."
56 $this->buckets = $buckets;
75 public function observe(
float $value ): void {
76 $count = $this->statsFactory->getCounter(
"{$this->name}_count" );
77 $bucket = $this->statsFactory->getCounter(
"{$this->name}_bucket" );
78 $sum = $this->statsFactory->getCounter(
"{$this->name}_sum" );
80 foreach ( $this->labels as $k => $v ) {
81 $count->setLabel( $k, $v );
82 $bucket->setLabel( $k, $v );
83 $sum->setLabel( $k, $v );
86 if ( $bucket->getSampleCount() === 0 ) {
87 $this->preloadBuckets( $bucket );
90 $bucket->setBucket(
'+Inf' )->increment();
91 foreach ( $this->buckets as $le ) {
92 if ( $value <= $le ) {
93 $bucket->setBucket( $le )->increment();
98 $sum->incrementBy( $value );