Skip to content

Commit 0ce7b73

Browse files
committed
Updated
1 parent 2ee3590 commit 0ce7b73

File tree

9 files changed

+181
-110
lines changed

9 files changed

+181
-110
lines changed

src/Instrumentation/PDO/README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,28 @@ OTEL_PHP_INSTRUMENTATION_PDO_DISTRIBUTE_STATEMENT_TO_LINKED_SPANS=true
3838
### SQL Commenter feature
3939
The [sqlcommenter](https://google.github.io/sqlcommenter/) feature can be enabled using configuration directive, currently it can be used with `postgresql` and `mysql` drivers only.
4040
```
41-
otel.instrumentation.pdo.sql_commenter = true
41+
otel.instrumentation.pdo.context_propagation = true
4242
```
4343
or environment variable:
4444
```shell
45-
OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER=true
45+
OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION=true
4646
```
4747

48-
This feature by default will append a SQL comment to the query statement with the information about the code that executed the query.
49-
The SQL comment can be configured to prepend to the query statement using the following configuration directive:
48+
The modified query statement by default will not update `TraceAttributes::DB_QUERY_TEXT` due to high cardinality risk, but it can be configured using the following configuration directive:
5049
```
51-
otel.instrumentation.pdo.sql_commenter.prepend = true
50+
otel.instrumentation.pdo.context_propagation.attribute = true
5251
```
5352
or environment variable:
5453
```shell
55-
OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_PREPEND=true
54+
OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION_ATTRIBUTE=true
5655
```
57-
The modified query statement by default will not update `TraceAttributes::DB_QUERY_TEXT` due to high cardinality risk, but it can be configured using the following configuration directive:
56+
57+
This feature by default will append a SQL comment to the query statement with the information about the code that executed the query.
58+
The SQL comment can be configured to prepend to the query statement using the following configuration directive:
5859
```
59-
otel.instrumentation.pdo.sql_commenter.attribute = true
60+
otel.instrumentation.pdo.sql_commenter.prepend = true
6061
```
6162
or environment variable:
6263
```shell
63-
OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_ATTRIBUTE=true
64-
```
64+
OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_PREPEND=true
65+
```

src/Instrumentation/PDO/src/ContextPropagation.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ public static function isEnabled(): bool
1717
return filter_var(get_cfg_var('otel.instrumentation.pdo.context_propagation'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
1818
}
1919

20-
public static function isPrepend(): bool
21-
{
22-
if (class_exists('OpenTelemetry\SDK\Common\Configuration\Configuration') && Configuration::getBoolean('OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION_PREPEND', false)) {
23-
return true;
24-
}
25-
26-
return filter_var(get_cfg_var('otel.instrumentation.pdo.context_propagation.prepend'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
27-
}
28-
2920
public static function isAttributeEnabled(): bool
3021
{
3122
if (class_exists('OpenTelemetry\SDK\Common\Configuration\Configuration') && Configuration::getBoolean('OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION_ATTRIBUTE', false)) {
@@ -34,22 +25,4 @@ public static function isAttributeEnabled(): bool
3425

3526
return filter_var(get_cfg_var('otel.instrumentation.pdo.context_propagation.attribute'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
3627
}
37-
38-
public static function addSqlComments(string $query, array $comments) : string
39-
{
40-
$query = trim($query);
41-
if (self::isPrepend()) {
42-
return Utils::formatComments(array_filter($comments)) . $query;
43-
}
44-
$hasSemicolon = $query !== '' && $query[strlen($query) - 1] === ';';
45-
$query = rtrim($query, ';');
46-
47-
return $query . Utils::formatComments(array_filter($comments)) . ($hasSemicolon ? ';' : '');
48-
49-
}
50-
51-
public static function isOptInDatabase(string $db) : bool
52-
{
53-
return $db == 'postgresql' || $db == 'mysql';
54-
}
5528
}

src/Instrumentation/PDO/src/PDOInstrumentation.php

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,27 @@ public static function register(): void
129129

130130
Context::storage()->attach($span->storeInContext($parent));
131131
if (ContextPropagation::isEnabled() && $sqlStatement !== self::UNDEFINED) {
132-
/** @psalm-suppress PossiblyInvalidCast */
133-
if (array_key_exists(TraceAttributes::DB_SYSTEM_NAME, $attributes) && ContextPropagation::isOptInDatabase((string) ($attributes[TraceAttributes::DB_SYSTEM_NAME]))) {
134-
$comments = [];
135-
Globals::propagator()->inject($comments);
136-
$sqlStatement = ContextPropagation::addSqlComments($sqlStatement, $comments);
137-
if (ContextPropagation::isAttributeEnabled()) {
138-
$span->setAttributes([
139-
DbAttributes::DB_QUERY_TEXT => $sqlStatement,
140-
]);
132+
if (array_key_exists(TraceAttributes::DB_SYSTEM_NAME, $attributes)) {
133+
/** @psalm-suppress PossiblyInvalidCast */
134+
switch ((string) $attributes[TraceAttributes::DB_SYSTEM_NAME]) {
135+
case 'postgresql':
136+
case 'mysql':
137+
$comments = [];
138+
Globals::propagator()->inject($comments);
139+
$sqlStatement = SqlCommentPropagator::inject($sqlStatement, $comments);
140+
if (ContextPropagation::isAttributeEnabled()) {
141+
$span->setAttributes([
142+
DbAttributes::DB_QUERY_TEXT => $sqlStatement,
143+
]);
144+
}
145+
146+
return [
147+
0 => $sqlStatement,
148+
];
149+
default:
150+
// Do nothing, not a database we want to propagate
151+
break;
141152
}
142-
143-
return [
144-
0 => $sqlStatement,
145-
];
146153
}
147154
}
148155

@@ -175,20 +182,27 @@ public static function register(): void
175182

176183
Context::storage()->attach($span->storeInContext($parent));
177184
if (ContextPropagation::isEnabled() && $sqlStatement !== self::UNDEFINED) {
178-
/** @psalm-suppress PossiblyInvalidCast */
179-
if (array_key_exists(TraceAttributes::DB_SYSTEM_NAME, $attributes) && ContextPropagation::isOptInDatabase((string) ($attributes[TraceAttributes::DB_SYSTEM_NAME]))) {
180-
$comments = [];
181-
Globals::propagator()->inject($comments);
182-
$sqlStatement = ContextPropagation::addSqlComments($sqlStatement, $comments);
183-
if (ContextPropagation::isAttributeEnabled()) {
184-
$span->setAttributes([
185-
DbAttributes::DB_QUERY_TEXT => $sqlStatement,
186-
]);
185+
if (array_key_exists(TraceAttributes::DB_SYSTEM_NAME, $attributes)) {
186+
/** @psalm-suppress PossiblyInvalidCast */
187+
switch ((string) $attributes[TraceAttributes::DB_SYSTEM_NAME]) {
188+
case 'postgresql':
189+
case 'mysql':
190+
$comments = [];
191+
Globals::propagator()->inject($comments);
192+
$sqlStatement = SqlCommentPropagator::inject($sqlStatement, $comments);
193+
if (ContextPropagation::isAttributeEnabled()) {
194+
$span->setAttributes([
195+
DbAttributes::DB_QUERY_TEXT => $sqlStatement,
196+
]);
197+
}
198+
199+
return [
200+
0 => $sqlStatement,
201+
];
202+
default:
203+
// Do nothing, not a database we want to propagate
204+
break;
187205
}
188-
189-
return [
190-
0 => $sqlStatement,
191-
];
192206
}
193207
}
194208

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Contrib\Instrumentation\PDO;
6+
7+
use OpenTelemetry\SDK\Common\Configuration\Configuration;
8+
9+
class SqlCommentPropagator implements SqlPropagatorInterface
10+
{
11+
public static function isPrepend(): bool
12+
{
13+
if (class_exists('OpenTelemetry\SDK\Common\Configuration\Configuration') && Configuration::getBoolean('OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_PREPEND', false)) {
14+
return true;
15+
}
16+
17+
return filter_var(get_cfg_var('otel.instrumentation.pdo.sql_commenter.prepend'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
18+
}
19+
20+
public static function inject(string $query, array $comments): string
21+
{
22+
$query = trim($query);
23+
if (self::isPrepend()) {
24+
return Utils::formatComments(array_filter($comments)) . $query;
25+
}
26+
$hasSemicolon = $query !== '' && $query[strlen($query) - 1] === ';';
27+
$query = rtrim($query, ';');
28+
29+
return $query . Utils::formatComments(array_filter($comments)) . ($hasSemicolon ? ';' : '');
30+
}
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Contrib\Instrumentation\PDO;
6+
7+
interface SqlPropagatorInterface
8+
{
9+
public static function inject(string $query, array $comments): string;
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Contrib\Instrumentation\PDO;
6+
7+
class SqlServerPropagator implements SqlPropagatorInterface
8+
{
9+
public static function inject(string $query, array $comments): string
10+
{
11+
// To-do: Implement SET_CONTEXT_INFO https://github.com/open-telemetry/semantic-conventions/pull/2363
12+
return $query;
13+
}
14+
}

src/Instrumentation/PDO/tests/Unit/ContextPropagationTest.php

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,6 @@ public function testIsEnabledReturnsFalse()
2323
$this->assertFalse($result);
2424
}
2525

26-
public function testIsPrependReturnsTrue()
27-
{
28-
$_SERVER['OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION_PREPEND'] = true;
29-
$result = ContextPropagation::isPrepend();
30-
$this->assertTrue($result);
31-
}
32-
33-
public function testIsPrependReturnsFalse()
34-
{
35-
$_SERVER['OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION_PREPEND'] = false;
36-
$result = ContextPropagation::isPrepend();
37-
$this->assertFalse($result);
38-
}
39-
4026
public function testIsAttributeEnabledReturnsTrue()
4127
{
4228
$_SERVER['OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION_ATTRIBUTE'] = true;
@@ -50,37 +36,4 @@ public function testIsAttributeEnabledReturnsFalse()
5036
$result = ContextPropagation::isAttributeEnabled();
5137
$this->assertFalse($result);
5238
}
53-
54-
public function testAddSqlCommentsPrepend()
55-
{
56-
$_SERVER['OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION_PREPEND'] = true;
57-
$comments = [
58-
'key1' => 'value1',
59-
'key2' => 'value2',
60-
'key3' => 'value3',
61-
];
62-
$query = 'SELECT 1;';
63-
$result = ContextPropagation::addSqlComments($query, $comments);
64-
$this->assertEquals("/*key1='value1',key2='value2',key3='value3'*/SELECT 1;", $result);
65-
}
66-
67-
public function testAddSqlCommentsAppend()
68-
{
69-
$_SERVER['OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION_PREPEND'] = false;
70-
$comments = [
71-
'key1' => 'value1',
72-
'key2' => 'value2',
73-
'key3' => 'value3',
74-
];
75-
$query = 'SELECT 1;';
76-
$result = ContextPropagation::addSqlComments($query, $comments);
77-
$this->assertEquals("SELECT 1/*key1='value1',key2='value2',key3='value3'*/;", $result);
78-
}
79-
80-
public function testIsOptInDatabase()
81-
{
82-
$this->assertTrue(ContextPropagation::isOptInDatabase('postgresql'));
83-
$this->assertTrue(ContextPropagation::isOptInDatabase('mysql'));
84-
$this->assertFalse(ContextPropagation::isOptInDatabase('sqlite'));
85-
}
8639
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Tests\Instrumentation\PDO\tests\Unit;
6+
7+
use OpenTelemetry\Contrib\Instrumentation\PDO\SqlCommentPropagator;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class SqlCommentPropagatorTest extends TestCase
11+
{
12+
public function testIsPrependReturnsTrue()
13+
{
14+
$_SERVER['OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION_PREPEND'] = true;
15+
$result = SqlCommentPropagator::isPrepend();
16+
$this->assertTrue($result);
17+
}
18+
19+
public function testIsPrependReturnsFalse()
20+
{
21+
$_SERVER['OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION_PREPEND'] = false;
22+
$result = SqlCommentPropagator::isPrepend();
23+
$this->assertFalse($result);
24+
}
25+
26+
public function testInjectPrepend()
27+
{
28+
$_SERVER['OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION_PREPEND'] = true;
29+
$comments = [
30+
'key1' => 'value1',
31+
'key2' => 'value2',
32+
'key3' => 'value3',
33+
];
34+
$query = 'SELECT 1;';
35+
$result = SqlCommentPropagator::inject($query, $comments);
36+
$this->assertEquals("/*key1='value1',key2='value2',key3='value3'*/SELECT 1;", $result);
37+
}
38+
39+
public function testInjectAppend()
40+
{
41+
$_SERVER['OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION_PREPEND'] = false;
42+
$comments = [
43+
'key1' => 'value1',
44+
'key2' => 'value2',
45+
'key3' => 'value3',
46+
];
47+
$query = 'SELECT 1;';
48+
$result = SqlCommentPropagator::inject($query, $comments);
49+
$this->assertEquals("SELECT 1/*key1='value1',key2='value2',key3='value3'*/;", $result);
50+
}
51+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Tests\Instrumentation\PDO\tests\Unit;
6+
7+
use OpenTelemetry\Contrib\Instrumentation\PDO\SqlServerPropagator;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class SqlServerPropagatorTest extends TestCase
11+
{
12+
public function testInject()
13+
{
14+
$comments = [
15+
'key1' => 'value1',
16+
'key2' => 'value2',
17+
'key3' => 'value3',
18+
];
19+
$query = 'SELECT 1;';
20+
$result = SqlServerPropagator::inject($query, $comments);
21+
$this->assertEquals('SELECT 1;', $result);
22+
}
23+
24+
}

0 commit comments

Comments
 (0)