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

OpenCensus - Redis Integration #236

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
15 changes: 13 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ jobs:
php71-32bit:
<<: *unit-config
docker:
- image: gcr.io/php-stackdriver/php71-32bit
- image: gcr.io/php-stackdriver/php71-64bit
asenlog marked this conversation as resolved.
Show resolved Hide resolved
environment:
TEST_PHP_ARGS: -q
REPORT_EXIT_STATUS: 1
Expand Down Expand Up @@ -141,6 +141,10 @@ jobs:
environment:
POSTGRES_PASSWORD: pgsql
POSTGRES_USER: postgres
- image: redis:3.2
environment:
REDIS_HOST: 127.0.0.1
REDIS_PORT: 6379
steps:
- checkout
- run:
Expand Down Expand Up @@ -175,6 +179,11 @@ jobs:
- run:
name: Install pdo_mysql extension
command: sudo docker-php-ext-install pdo_mysql
- run:
name: Install redis extension
command: |
sudo pecl install redis
sudo docker-php-ext-enable redis
- run:
name: Install mysqli extension
command: sudo docker-php-ext-install mysqli
Expand All @@ -199,6 +208,9 @@ jobs:
- run:
name: Memcached test
command: tests/integration/memcached/test.sh
- run:
name: Redis test
command: tests/integration/redis/test.sh
- run:
name: Pgsql test
command: tests/integration/pgsql/test.sh
Expand Down Expand Up @@ -226,6 +238,5 @@ workflows:
- php72-zts
- php73
- php73-zts
- php71-32bit
- php71-debug
- integration
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"twig/twig": "~2.0 || ~1.35",
"symfony/yaml": "~3.3",
"guzzlehttp/guzzle": "~5.3",
"predis/predis": "1.1.0",
"guzzlehttp/psr7": "~1.4"
},
"conflict": {
Expand Down
84 changes: 84 additions & 0 deletions src/Trace/Integrations/Redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Copyright 2019 OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace OpenCensus\Trace\Integrations;

use OpenCensus\Trace\Span;

/**
* This class handles instrumenting Redis requests using the opencensus extension.
*
* Example:
* ```
* use OpenCensus\Trace\Integrations\Redis;
*
* Redis::load();
* ```
*/
class Redis implements IntegrationInterface
{
/**
* Static method to add instrumentation to memcache requests
asenlog marked this conversation as resolved.
Show resolved Hide resolved
*/
public static function load()
{
if (!extension_loaded('opencensus')) {
trigger_error('opencensus extension required to load Redis integrations.', E_USER_WARNING);
}

opencensus_trace_method('Predis\Client', '__construct', [static::class, 'handleConstruct']);

opencensus_trace_method('Predis\Client', 'set', [static::class, 'handleCall']);

opencensus_trace_method('Predis\Client', 'get', [static::class, 'handleCall']);

opencensus_trace_method('Predis\Client', 'flushDB');
}

/**
* Trace Construct Options
*
* @param $predis
* @param $params
* @return array
*/
public static function handleConstruct($predis, $params)
{
return [
'attributes' => [
'host' => $params['host'],
'port' => $params['port']
],
'kind' => Span::KIND_CLIENT
];
}

/**
* Trace Set / Get Operations
*
* @param $predis
* @param $key
* @return array
*/
public static function handleCall($predis, $key)
{
return [
'attributes' => ['key' => $key],
'kind' => Span::KIND_CLIENT
];
}
}
18 changes: 18 additions & 0 deletions tests/integration/redis/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"require": {
"php": "^7.2",
"opencensus/opencensus": "dev-master",
"predis/predis": "1.1.0",
"ext-opencensus": "*",
"ext-redis": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
},
"repositories": [
{
"type": "git",
"url": "https://github.com/census-instrumentation/opencensus-php"
}
]
}
21 changes: 21 additions & 0 deletions tests/integration/redis/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php" colors="true">
<testsuites>
<testsuite>
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<directory suffix=".php">src/*/V[!a-zA-Z]*</directory>
<directory suffix=".php">src/*/*/V[!a-zA-Z]*</directory>
<directory suffix=".php">src/*/*/*/V[!a-zA-Z]*</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="build/clover.xml"/>
</logging>
</phpunit>
27 changes: 27 additions & 0 deletions tests/integration/redis/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
# Copyright 2018 OpenCensus Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

pushd $(dirname ${BASH_SOURCE[0]})
source ../setup_test_repo.sh

sed -i "s|dev-master|dev-${BRANCH}|" composer.json
sed -i "s|https://github.com/census-instrumentation/opencensus-php|${REPO}|" composer.json
composer install -n --prefer-dist

vendor/bin/phpunit

popd
71 changes: 71 additions & 0 deletions tests/integration/redis/tests/RedisTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Copyright 2019 OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace OpenCensus\Tests\Integration\Trace\Exporter;

use OpenCensus\Trace\Tracer;
use OpenCensus\Trace\Exporter\ExporterInterface;
use OpenCensus\Trace\Integrations\Redis as RedisIntegration;
use PHPUnit\Framework\TestCase;
use Predis\Client;

class RedisTest extends TestCase
{
private $tracer;
private static $redisHost;
private static $redisPort;

public static function setUpBeforeClass()
{
RedisIntegration::load();
self::$redisHost = getenv('REDIS_HOST') ?: '127.0.0.1';
self::$redisPort = (int) (getenv('REDIS_PORT') ?: 6379);
}

public function setUp()
{
if (!extension_loaded('opencensus')) {
$this->markTestSkipped('Please enable the opencensus extension.');
}
opencensus_trace_clear();
$exporter = $this->prophesize(ExporterInterface::class);
$this->tracer = Tracer::start($exporter->reveal(), [
'skipReporting' => true
]);
}

private function getSpans()
{
$this->tracer->onExit();
return $this->tracer->tracer()->spans();
}

public function testAddGet()
{
$client = new Client([
'host' => self::$redisHost,
'port' => self::$redisPort
]);

$client->set('foo', 'bar');
$value = $client->get('foo');
$this->assertEquals('bar', $value);

$spans = $this->getSpans();
$this->assertCount(4, $spans);
}
}