Skip to content

Commit

Permalink
Merge branch 'release/v1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDavies committed Oct 4, 2019
2 parents 306bdf0 + 765fc23 commit ae61369
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 10 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ $renderer = new RenderTextFormat();
$result = $renderer->render($registry->getMetricFamilySamples());
```

### Advanced Usage

#### Advanced Histogram Usage
On passing an empty array for the bucket parameter on instantiation, a set of default buckets will be used instead.
Whilst this is a good base for a typical web application, there is named constructor to assist in the generation of
exponential / geometric buckets.

Eg:
```
Histogram::exponentialBuckets(0.05, 1.5, 10);
```

This will start your buckets with a value of 1.5, grow them by a factor of 1.5 per bucket across a set of 10 buckets.

Also look at the [examples](examples).

## Development
Expand Down
10 changes: 1 addition & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
{
"name": "endclothing/prometheus_client_php",
"version": "1.0.0",
"version": "1.0.1",
"authors": [
{
"name": "Joscha",
"email": "[email protected]"
},
{
"name": "Jan Brauer",
"email": "[email protected]"
},
{
"name": "Daniel Noel-Davies",
"email": "[email protected]"
Expand Down
32 changes: 31 additions & 1 deletion src/Prometheus/Histogram.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types = 1);
declare(strict_types=1);

namespace Prometheus;

Expand Down Expand Up @@ -62,6 +62,36 @@ public static function getDefaultBuckets(): array
];
}

/**
* @param float $start
* @param float $growthFactor
* @param int $numberOfBuckets
* @return array
*/
public static function exponentialBuckets(float $start, float $growthFactor, int $numberOfBuckets): array
{
if ($numberOfBuckets < 1) {
throw new InvalidArgumentException('Number of buckets must be a positive integer');
}

if ($start <= 0) {
throw new InvalidArgumentException('The starting position of a set of buckets must be a positive integer');
}

if ($growthFactor <= 1) {
throw new InvalidArgumentException('The growth factor must greater than 1');
}

$buckets = [];

for ($i = 0; $i < $numberOfBuckets; $i++) {
$buckets[$i] = $start;
$start *= $growthFactor;
}

return $buckets;
}

/**
* @param double $value e.g. 123
* @param array $labels e.g. ['status', 'opcode']
Expand Down
31 changes: 31 additions & 0 deletions tests/Test/Prometheus/AbstractHistogramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,37 @@ public function isShouldAcceptAnySequenceOfBasicLatinCharactersForLabelValues($v
}
}

/**
* @test
*/
public function itShouldBeAbleToGenerateExponentialBucketsGivenSpecificBounds()
{
$start = 0.05;
$growthFactor = 1.5;
$numberOfbuckets = 14;

$generatedBuckets = Histogram::exponentialBuckets($start, $growthFactor, $numberOfbuckets);

$expectedBuckets = [
0.05,
0.075,
0.1125,
0.16875,
0.253125,
0.3796875,
0.56953125,
0.854296875,
1.2814453125,
1.92216796875,
2.883251953125,
4.3248779296875,
6.4873168945313,
9.7309753417969,
];

self::assertEquals($generatedBuckets, $expectedBuckets);
}

/**
* @return array
* @see isShouldAcceptArbitraryLabelValues
Expand Down

0 comments on commit ae61369

Please sign in to comment.