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

Opencensus - Elastica Integration #238

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"squizlabs/php_codesniffer": "2.*",
"twig/twig": "~2.0 || ~1.35",
"symfony/yaml": "~3.3",
"ruflin/elastica": "6.0.2",
"guzzlehttp/guzzle": "~5.3",
"guzzlehttp/psr7": "~1.4"
},
Expand Down
88 changes: 88 additions & 0 deletions src/Trace/Integrations/Elastica.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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 ElasticSearch requests with elastica using the opencensus extension.
*
* Example:
* ```
* use OpenCensus\Trace\Integrations\Elastica;
*
* Elastica::load();
* ```
*/
class Elastica implements IntegrationInterface
{
/**
* Static method to add instrumentation to elasticsearch requests
*/
public static function load()
{
if (!extension_loaded('opencensus')) {
trigger_error('opencensus extension required to load Elastica integrations.', E_USER_WARNING);
}

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

opencensus_trace_method('Elastica\Client', 'request', [static::class, 'handleRequest']);
}

/**
* @param $elastica
* @param $config
*
* @return array
*/
public static function handleConstruct($elastica, $config)
{
$attributes = [];
foreach ($config as $key => $param) {
$attributes[$key] = $param;
}

return [
'attributes' => $attributes,
'kind' => Span::KIND_CLIENT,
];
}

/**
* @param $elastica
* @param $path
* @param $method
* @param $data
* @param $query
*
* @return array
*/
public static function handleRequest($elastica, $path, $method, $data, $query)
{
return [
'attributes' => [
'path' => $path,
'method' => $method,
'data' => json_encode($data),
'query' => json_encode($query),
],
'kind' => Span::KIND_CLIENT,
];
}
}
73 changes: 73 additions & 0 deletions tests/unit/Trace/Integrations/ElasticaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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\Unit\Trace\Integrations;


use OpenCensus\Trace\Integrations\Elastica;
use OpenCensus\Trace\Span;
use PHPUnit\Framework\TestCase;

/**
* @group trace
*/
class ElasticaTest extends TestCase
{
public function testHandleConstruct()
{
$scope = null;
$config = [
'transport' => 'http',
'host' => 'elastic',
'port' => '9200',
];

$spanOptions = Elastica::handleConstruct($scope, $config);
$expected = [
'attributes' => [
'transport' => 'http',
'host' => 'elastic',
'port' => '9200',
],
'kind' => Span::KIND_CLIENT
];

$this->assertEquals($expected, $spanOptions);
}

public function testHandleRequest()
{
$scope = null;
$path = '/test/path';
$method = 'GET';
$data = [];
$query = [];

$spanOptions = Elastica::handleRequest($scope, $path, $method, $data, $query);
$expected = [
'attributes' => [
'path' => $path,
'method' => $method,
'data' => json_encode($data),
'query' => json_encode($query),
],
'kind' => Span::KIND_CLIENT
];

$this->assertEquals($expected, $spanOptions);
}
}