Skip to content

Commit 771a39e

Browse files
committed
Added opt in for sql commenter attribute and update READMD.md
1 parent b837248 commit 771a39e

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/Instrumentation/PDO/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,22 @@ otel.instrumentation.pdo.distribute_statement_to_linked_spans = true
3333
or environment variable:
3434
```shell
3535
OTEL_PHP_INSTRUMENTATION_PDO_DISTRIBUTE_STATEMENT_TO_LINKED_SPANS=true
36-
```
36+
```
37+
38+
The [sqlcommenter](https://google.github.io/sqlcommenter/) feature can be enabled using configuration directive:
39+
```
40+
otel.instrumentation.pdo.sql_commenter = true
41+
```
42+
or environment variable:
43+
```shell
44+
OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER=true
45+
```
46+
47+
and its attribute can be configured using the following configuration directive:
48+
```
49+
otel.instrumentation.pdo.sql_commenter_attribute = true
50+
```
51+
or environment variable:
52+
```shell
53+
OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_ATTRIBUTE=true
54+
```

src/Instrumentation/PDO/src/PDOInstrumentation.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ public static function register(): void
128128
Context::storage()->attach($span->storeInContext($parent));
129129
if (self::isSqlCommenterEnabled() && $sqlStatement !== self::UNDEFINED) {
130130
$sqlStatement = self::appendSqlComments($sqlStatement, true);
131-
$span->setAttributes([
132-
TraceAttributes::DB_QUERY_TEXT => $sqlStatement,
133-
]);
131+
if (self::isSqlCommenterAttributeEnabled()) {
132+
$span->setAttributes([
133+
TraceAttributes::DB_QUERY_TEXT => $sqlStatement,
134+
]);
135+
}
134136

135137
return [
136138
0 => $sqlStatement,
@@ -167,9 +169,11 @@ public static function register(): void
167169
Context::storage()->attach($span->storeInContext($parent));
168170
if (self::isSqlCommenterEnabled() && $sqlStatement !== self::UNDEFINED) {
169171
$sqlStatement = self::appendSqlComments($sqlStatement, true);
170-
$span->setAttributes([
171-
TraceAttributes::DB_QUERY_TEXT => $sqlStatement,
172-
]);
172+
if (self::isSqlCommenterAttributeEnabled()) {
173+
$span->setAttributes([
174+
TraceAttributes::DB_QUERY_TEXT => $sqlStatement,
175+
]);
176+
}
173177

174178
return [
175179
0 => $sqlStatement,
@@ -206,9 +210,11 @@ public static function register(): void
206210
Context::storage()->attach($span->storeInContext($parent));
207211
if (self::isSqlCommenterEnabled() && $sqlStatement !== self::UNDEFINED) {
208212
$sqlStatement = self::appendSqlComments($sqlStatement, false);
209-
$span->setAttributes([
210-
TraceAttributes::DB_QUERY_TEXT => $sqlStatement,
211-
]);
213+
if (self::isSqlCommenterAttributeEnabled()) {
214+
$span->setAttributes([
215+
TraceAttributes::DB_QUERY_TEXT => $sqlStatement,
216+
]);
217+
}
212218

213219
return [
214220
0 => $sqlStatement,
@@ -388,6 +394,15 @@ private static function isSqlCommenterEnabled(): bool
388394
return filter_var(get_cfg_var('otel.instrumentation.pdo.sql_commenter'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
389395
}
390396

397+
private static function isSqlCommenterAttributeEnabled(): bool
398+
{
399+
if (class_exists('OpenTelemetry\SDK\Common\Configuration\Configuration')) {
400+
return Configuration::getBoolean('OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_ATTRIBUTE', false);
401+
}
402+
403+
return filter_var(get_cfg_var('otel.instrumentation.pdo.sql_commenter_attribute'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
404+
}
405+
391406
private static function appendSqlComments(string $query, bool $withTraceContext): string
392407
{
393408
$comments = [];

0 commit comments

Comments
 (0)