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

Commit 2774ccf

Browse files
authored
Add trace integrations (#7)
* Add trace integrations * Fix extension method names * Add trace integration tests for Curl and PDO * Testing the testable integrations * CS fixes * Capture SQL query as a label for Eloquent queries. Remove the model as it's inferred. * Fix documentation for Doctrine/Eloquent load() * Add Memcached integration * CS fixes * trigger E_USER_WARNING if trying to load integrations without the opencensus extension * Add tests for the label handlers for Memcache and Memcached integrations
1 parent 33671e5 commit 2774ccf

21 files changed

Lines changed: 1729 additions & 2 deletions

src/Trace/Integrations/Curl.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright 2017 Google Inc. All Rights Reserved.
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\Integrations;
19+
20+
/**
21+
* This class handles instrumenting curl requests using the opencensus extension.
22+
*
23+
* Example:
24+
* ```
25+
* use OpenCensus\Trace\Integrations\Curl
26+
*
27+
* Curl::load();
28+
* ```
29+
*/
30+
class Curl implements IntegrationInterface
31+
{
32+
/**
33+
* Static method to add instrumentation to curl requests
34+
*/
35+
public static function load()
36+
{
37+
if (!extension_loaded('opencensus')) {
38+
trigger_error('opencensus extension required to load curl integrations.', E_USER_WARNING);
39+
return;
40+
}
41+
42+
opencensus_trace_function('curl_exec', [static::class, 'handleCurlResource']);
43+
opencensus_trace_function('curl_multi_add_handle');
44+
opencensus_trace_function('curl_multi_remove_handle');
45+
}
46+
47+
/**
48+
* Handle extracting the uri from a given curl resource handler
49+
*
50+
* @param resource $resource The curl handler
51+
* @return array
52+
*/
53+
public static function handleCurlResource($resource)
54+
{
55+
return [
56+
'labels' => [
57+
'uri' => curl_getinfo($resource, CURLINFO_EFFECTIVE_URL)
58+
]
59+
];
60+
}
61+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright 2017 Google Inc. All Rights Reserved.
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\Integrations;
19+
20+
use Doctrine\ORM\Version;
21+
22+
/**
23+
* This class handles instrumenting the Doctrine ORM queries using the opencensus extension.
24+
*
25+
* Example:
26+
* ```
27+
* use OpenCensus\Trace\Integrations\Doctrine
28+
*
29+
* Doctrine::load();
30+
*/
31+
class Doctrine implements IntegrationInterface
32+
{
33+
/**
34+
* Static method to add instrumentation to Doctrine ORM calls
35+
*/
36+
public static function load()
37+
{
38+
if (!extension_loaded('opencensus')) {
39+
trigger_error('opencensus extension required to load Doctrine integrations.', E_USER_WARNING);
40+
return;
41+
}
42+
43+
PDO::load();
44+
45+
$persisterClass = (Version::compare('2.5.0') < 0)
46+
? 'Doctrine\ORM\Persisters\Entity\BasicEntityPersister' // Doctrine 2.5 or greater
47+
: 'Doctrine\ORM\Persisters\BasicEntityPersister'; // Doctrine 2.4 or earlier
48+
49+
// public function load(array $criteria, $entity = null, $assoc = null, array $hints = array(),
50+
// $lockMode = null, $limit = null, array $orderBy = null)
51+
opencensus_trace_method($persisterClass, 'load', function ($bep) {
52+
return [
53+
'name' => 'doctrine/load',
54+
'labels' => ['entity' => $bep->getClassMetadata()->name]
55+
];
56+
});
57+
58+
// public function loadAll(array $criteria = array(), array $orderBy = null, $limit = null, $offset = null)
59+
opencensus_trace_method($persisterClass, 'loadAll', function ($bep) {
60+
return [
61+
'name' => 'doctrine/loadAll',
62+
'labels' => ['entity' => $bep->getClassMetadata()->name]
63+
];
64+
});
65+
66+
// public int PDOConnection::exec(string $query)
67+
opencensus_trace_method(PDOConnection::class, 'exec', function ($scope, $query) {
68+
return [
69+
'name' => 'doctrine/exec',
70+
'labels' => ['query' => $query]
71+
];
72+
});
73+
}
74+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright 2017 Google Inc. All Rights Reserved.
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\Integrations;
19+
20+
use Illuminate\Database\Eloquent\Builder;
21+
use Illuminate\Database\Eloquent\Model;
22+
23+
/**
24+
* This class handles instrumenting the Eloquent ORM queries using the opencensus extension.
25+
*
26+
* Example:
27+
* ```
28+
* use OpenCensus\Trace\Integrations\Eloquent
29+
*
30+
* Eloquent::load();
31+
*/
32+
class Eloquent implements IntegrationInterface
33+
{
34+
/**
35+
* Static method to add instrumentation to Eloquent ORM calls
36+
*/
37+
public static function load()
38+
{
39+
if (!extension_loaded('opencensus')) {
40+
trigger_error('opencensus extension required to load Eloquent integrations.', E_USER_WARNING);
41+
return;
42+
}
43+
44+
// public function getModels($columns = ['*'])
45+
opencensus_trace_method(Builder::class, 'getModels', function ($builder) {
46+
return [
47+
'name' => 'eloquent/get',
48+
'labels' => [
49+
'query' => $builder->toBase()->toSql()
50+
]
51+
];
52+
});
53+
54+
// protected function performInsert(Builder $query)
55+
opencensus_trace_method(Model::class, 'performInsert', function ($model, $query) {
56+
return [
57+
'name' => 'eloquent/insert',
58+
'labels' => [
59+
'query' => $query->toBase()->toSql()
60+
]
61+
];
62+
});
63+
64+
// protected function performUpdate(Builder $query)
65+
opencensus_trace_method(Model::class, 'performUpdate', function ($model, $query) {
66+
return [
67+
'name' => 'eloquent/update',
68+
'labels' => [
69+
'query' => $query->toBase()->toSql()
70+
]
71+
];
72+
});
73+
74+
// public function delete()
75+
opencensus_trace_method(Model::class, 'delete', function ($model, $query) {
76+
return [
77+
'name' => 'eloquent/delete',
78+
'labels' => [
79+
'query' => $query->toBase()->toSql()
80+
]
81+
];
82+
});
83+
}
84+
}

src/Trace/Integrations/Grpc.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright 2017 Google Inc. All Rights Reserved.
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\Integrations;
19+
20+
use Grpc\BaseStub;
21+
22+
/**
23+
* This class handles instrumenting grpc requests using the opencensus extension.
24+
*
25+
* Example:
26+
* ```
27+
* use OpenCensus\Trace\Integrations\Grpc
28+
*
29+
* Grpc::load();
30+
* ```
31+
*/
32+
class Grpc implements IntegrationInterface
33+
{
34+
/**
35+
* Static method to add instrumentation to grpc requests
36+
*/
37+
public static function load()
38+
{
39+
if (!extension_loaded('opencensus')) {
40+
trigger_error('opencensus extension required to load grpc integrations.', E_USER_WARNING);
41+
return;
42+
}
43+
44+
// protected function _simpleRequest($method, $argument, $deserialize, array $metadata = [],
45+
// array $options = [])
46+
opencensus_trace_method(BaseStub::class, '_simpleRequest', function ($stub, $method) {
47+
return [
48+
'name' => 'grpc/simpleRequest',
49+
'labels' => [
50+
'host' => $stub->getTarget(),
51+
'uri' => $method
52+
]
53+
];
54+
});
55+
56+
// protected function _clientStreamRequest($method, $argument, $deserialize, array $metadata = [],
57+
// array $options = [])
58+
opencensus_trace_method(BaseStub::class, '_clientStreamRequest', function ($stub, $method) {
59+
return [
60+
'name' => 'grpc/clientStreamRequest',
61+
'labels' => [
62+
'host' => $stub->getTarget(),
63+
'uri' => $method
64+
]
65+
];
66+
});
67+
68+
// protected function _serverStreamRequest($method, $argument, $deserialize, array $metadata = [],
69+
// array $options = [])
70+
opencensus_trace_method(BaseStub::class, '_serverStreamRequest', function ($stub, $method) {
71+
return [
72+
'name' => 'grpc/serverStreamRequest',
73+
'labels' => [
74+
'host' => $stub->getTarget(),
75+
'uri' => $method
76+
]
77+
];
78+
});
79+
80+
// protected function _bidiRequest($method, $deserialize, array $metadata = [], array $options = [])
81+
opencensus_trace_method(BaseStub::class, '_bidiRequest', function ($stub, $method) {
82+
return [
83+
'name' => 'grpc/bidiRequest',
84+
'labels' => [
85+
'host' => $stub->getTarget(),
86+
'uri' => $method
87+
]
88+
];
89+
});
90+
}
91+
}

0 commit comments

Comments
 (0)