Skip to content

Commit 0ab11e2

Browse files
committed
Added prepend vs append opt-in
1 parent 771a39e commit 0ab11e2

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/Instrumentation/PDO/README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ or environment variable:
3535
OTEL_PHP_INSTRUMENTATION_PDO_DISTRIBUTE_STATEMENT_TO_LINKED_SPANS=true
3636
```
3737

38+
### SQL Commenter feature
3839
The [sqlcommenter](https://google.github.io/sqlcommenter/) feature can be enabled using configuration directive:
3940
```
4041
otel.instrumentation.pdo.sql_commenter = true
@@ -43,10 +44,18 @@ or environment variable:
4344
```shell
4445
OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER=true
4546
```
46-
47-
and its attribute can be configured using the following configuration directive:
47+
This feature by default will append a SQL comment to the query statement with the information about the code that executed the query.
48+
The SQL comment can be configured to prepend to the query statement using the following configuration directive:
49+
```
50+
otel.instrumentation.pdo.sql_commenter.prepend = true
51+
```
52+
or environment variable:
53+
```shell
54+
OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_PREPEND=true
55+
```
56+
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:
4857
```
49-
otel.instrumentation.pdo.sql_commenter_attribute = true
58+
otel.instrumentation.pdo.sql_commenter.attribute = true
5059
```
5160
or environment variable:
5261
```shell

src/Instrumentation/PDO/src/PDOInstrumentation.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static function register(): void
127127

128128
Context::storage()->attach($span->storeInContext($parent));
129129
if (self::isSqlCommenterEnabled() && $sqlStatement !== self::UNDEFINED) {
130-
$sqlStatement = self::appendSqlComments($sqlStatement, true);
130+
$sqlStatement = self::addSqlComments($sqlStatement, true);
131131
if (self::isSqlCommenterAttributeEnabled()) {
132132
$span->setAttributes([
133133
TraceAttributes::DB_QUERY_TEXT => $sqlStatement,
@@ -168,7 +168,7 @@ public static function register(): void
168168

169169
Context::storage()->attach($span->storeInContext($parent));
170170
if (self::isSqlCommenterEnabled() && $sqlStatement !== self::UNDEFINED) {
171-
$sqlStatement = self::appendSqlComments($sqlStatement, true);
171+
$sqlStatement = self::addSqlComments($sqlStatement, true);
172172
if (self::isSqlCommenterAttributeEnabled()) {
173173
$span->setAttributes([
174174
TraceAttributes::DB_QUERY_TEXT => $sqlStatement,
@@ -209,7 +209,7 @@ public static function register(): void
209209

210210
Context::storage()->attach($span->storeInContext($parent));
211211
if (self::isSqlCommenterEnabled() && $sqlStatement !== self::UNDEFINED) {
212-
$sqlStatement = self::appendSqlComments($sqlStatement, false);
212+
$sqlStatement = self::addSqlComments($sqlStatement, false);
213213
if (self::isSqlCommenterAttributeEnabled()) {
214214
$span->setAttributes([
215215
TraceAttributes::DB_QUERY_TEXT => $sqlStatement,
@@ -394,16 +394,25 @@ private static function isSqlCommenterEnabled(): bool
394394
return filter_var(get_cfg_var('otel.instrumentation.pdo.sql_commenter'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
395395
}
396396

397+
private static function isSqlCommenterPrepend(): bool
398+
{
399+
if (class_exists('OpenTelemetry\SDK\Common\Configuration\Configuration')) {
400+
return Configuration::getBoolean('OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_PREPEND', false);
401+
}
402+
403+
return filter_var(get_cfg_var('otel.instrumentation.pdo.sql_commenter.prepend'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
404+
}
405+
397406
private static function isSqlCommenterAttributeEnabled(): bool
398407
{
399408
if (class_exists('OpenTelemetry\SDK\Common\Configuration\Configuration')) {
400409
return Configuration::getBoolean('OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_ATTRIBUTE', false);
401410
}
402411

403-
return filter_var(get_cfg_var('otel.instrumentation.pdo.sql_commenter_attribute'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
412+
return filter_var(get_cfg_var('otel.instrumentation.pdo.sql_commenter.attribute'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
404413
}
405414

406-
private static function appendSqlComments(string $query, bool $withTraceContext): string
415+
private static function addSqlComments(string $query, bool $withTraceContext): string
407416
{
408417
$comments = [];
409418
if ($withTraceContext) {
@@ -413,9 +422,13 @@ private static function appendSqlComments(string $query, bool $withTraceContext)
413422
$comments[$key] = $value;
414423
}
415424
$query = trim($query);
425+
if (self::isSqlCommenterPrepend()) {
426+
return Utils::formatComments(array_filter($comments)) . $query;
427+
}
416428
$hasSemicolon = $query !== '' && $query[strlen($query) - 1] === ';';
417429
$query = rtrim($query, ';');
418430

419431
return $query . Utils::formatComments(array_filter($comments)) . ($hasSemicolon ? ';' : '');
432+
420433
}
421434
}

0 commit comments

Comments
 (0)