class QpsSampler implements SamplerInterface

This implementation of the SamplerInterface uses a cache to limit sampling to the a certain number of queries per second. It requires a PSR-6 cache implementation.

Example using cache/memcached-adapter:

// This example uses the  Memcached extension and requires the
// cache/memcached-adapter composer package
use OpenCensus\Trace\Sampler\QpsSampler;
use Cache\Adapter\Memcached\MemcachedCachePool;
use Cache\Adapter\Common\PhpCacheItem;

$client = new \Memcached();
$client->addServer('localhost', 11211);
$cache = new Memcached\MemcachedCachePool($client);
$sampler = new QpsSampler($cache, [
    'cacheItemClass' => PhpCacheItem::class
]);

You can find a list of PSR-6 cache implementations here.

Constants

DEFAULT_CACHE_KEY

DEFAULT_QPS_RATE

Methods

__construct(CacheItemPoolInterface $cache = null, array $options = [])

Create a new QpsSampler. If the provided cache is shared between servers, the queries per second will be counted across servers. If the cache is shared between servers and you wish to sample independently on the servers, provide your own cache key that is different on each server.

bool
shouldSample()

Returns whether or not the request should be sampled.

float
rate()

Return the query-per-second rate

Details

at line 91
__construct(CacheItemPoolInterface $cache = null, array $options = [])

Create a new QpsSampler. If the provided cache is shared between servers, the queries per second will be counted across servers. If the cache is shared between servers and you wish to sample independently on the servers, provide your own cache key that is different on each server.

There may be race conditions between simultaneous requests where they may both (all) be sampled.

Parameters

CacheItemPoolInterface $cache The cache store to use
array $options [optional] { configuration options.

@type string $cacheItemClass The class of the item to use. This class must implement
      CacheItemInterface.
@type float $rate The number of queries per second to allow. Must be less than or equal to 1.
      **Defaults to** `0.1`
@type string $key The cache key to use. **Defaults to** `__opencensus_trace__`

}

at line 121
bool shouldSample()

Returns whether or not the request should be sampled.

Return Value

bool

at line 161
float rate()

Return the query-per-second rate

Return Value

float