Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 829c42f

Browse files
authored
Add MultiSampler implementation (#147)
* Add MultiSampler implementation * Add MultiSampler to README table
1 parent 1ea277c commit 829c42f

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ The provided samplers are:
8787
| ----- | ----------- |
8888
| [NeverSampleSampler][never-sampler] | Never trace any requests |
8989
| [AlwaysSampleSampler][always-sampler] | Trace all requests |
90+
| [MultiSampler][multi-sampler] | Check multiple samplers |
9091
| [QpsSampler][qps-sampler] | Trace X requests per second. Requires a PSR-6 cache implementation |
9192
| [ProbabilitySampler][probability-sampler] | Trace X percent of requests. |
9293
@@ -159,6 +160,7 @@ This is not an official Google product.
159160
[pecl]: https://pecl.php.net/
160161
[never-sampler]: https://census-instrumentation.github.io/opencensus-php/api/OpenCensus/Trace/Sampler/NeverSampleSampler.html
161162
[always-sampler]: https://census-instrumentation.github.io/opencensus-php/api/OpenCensus/Trace/Sampler/NeverSampleSampler.html
163+
[multi-sampler]: https://census-instrumentation.github.io/opencensus-php/api/OpenCensus/Trace/Sampler/MultiSampler.html
162164
[qps-sampler]: https://census-instrumentation.github.io/opencensus-php/api/OpenCensus/Trace/Sampler/NeverSampleSampler.html
163165
[probability-sampler]: https://census-instrumentation.github.io/opencensus-php/api/OpenCensus/Trace/Sampler/NeverSampleSampler.html
164166
[echo-exporter]: https://census-instrumentation.github.io/opencensus-php/api/OpenCensus/Trace/Exporter/EchoExporter.html

src/Trace/Sampler/MultiSampler.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright 2018 OpenCensus Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace OpenCensus\Trace\Sampler;
19+
20+
/**
21+
* This implementation of the SamplerInterface wraps any number of child
22+
* SamplerInterface implementations. All provided implementations must return
23+
* true in order for the request to be sampled.
24+
*
25+
* Example:
26+
* ```
27+
* use OpenCensus\Trace\Sampler\AlwaysSampleSampler;
28+
* use OpenCensus\Trace\Sampler\MultiSampler;
29+
*
30+
* $sampler = new MultiSampler([
31+
* new AlwaysSampleSampler()
32+
* ]);
33+
* ```
34+
*/
35+
class MultiSampler implements SamplerInterface
36+
{
37+
38+
/**
39+
* @var SamplerInterface[]
40+
*/
41+
private $samplers;
42+
43+
/**
44+
* Create a new MultiSampler.
45+
*
46+
* @param SamplerInterface[] $samplers The samplers to consult.
47+
*/
48+
public function __construct(array $samplers = [])
49+
{
50+
$this->samplers = $samplers;
51+
}
52+
53+
/**
54+
* Returns false if any provided sampler chooses not to sample this
55+
* request.
56+
*
57+
* @return bool
58+
*/
59+
public function shouldSample()
60+
{
61+
foreach ($this->samplers as $sampler) {
62+
if ($sampler->shouldSample() === false) {
63+
return false;
64+
}
65+
}
66+
return true;
67+
}
68+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright 2018 OpenCensus Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace OpenCensus\Tests\Unit\Trace\Sampler;
19+
20+
use OpenCensus\Trace\Sampler\MultiSampler;
21+
use OpenCensus\Trace\Sampler\SamplerInterface;
22+
23+
/**
24+
* @group trace
25+
*/
26+
class MultiSamplerTest extends \PHPUnit_Framework_TestCase
27+
{
28+
public function testNoSamplers()
29+
{
30+
$sampler = new MultiSampler();
31+
$this->assertTrue($sampler->shouldSample());
32+
}
33+
34+
public function testSingleSampler()
35+
{
36+
$innerSampler = $this->prophesize(SamplerInterface::class);
37+
$innerSampler->shouldSample()->willReturn(true)->shouldBeCalled();
38+
39+
$sampler = new MultiSampler([
40+
$innerSampler->reveal()
41+
]);
42+
$this->assertTrue($sampler->shouldSample());
43+
}
44+
45+
public function testMultipleSamplers()
46+
{
47+
$innerSampler = $this->prophesize(SamplerInterface::class);
48+
$innerSampler->shouldSample()->willReturn(true)->shouldBeCalled();
49+
$innerSampler2 = $this->prophesize(SamplerInterface::class);
50+
$innerSampler2->shouldSample()->willReturn(true)->shouldBeCalled();
51+
52+
$sampler = new MultiSampler([
53+
$innerSampler->reveal(),
54+
$innerSampler2->reveal()
55+
]);
56+
$this->assertTrue($sampler->shouldSample());
57+
}
58+
59+
public function testInnerSamplerFails()
60+
{
61+
$innerSampler = $this->prophesize(SamplerInterface::class);
62+
$innerSampler->shouldSample()->willReturn(true)->shouldBeCalled();
63+
$innerSampler2 = $this->prophesize(SamplerInterface::class);
64+
$innerSampler2->shouldSample()->willReturn(false)->shouldBeCalled();
65+
66+
$sampler = new MultiSampler([
67+
$innerSampler->reveal(),
68+
$innerSampler2->reveal()
69+
]);
70+
$this->assertFalse($sampler->shouldSample());
71+
}
72+
}

0 commit comments

Comments
 (0)