Skip to content

Commit f284027

Browse files
Add db statements to linked spans (open-telemetry#325)
1 parent 3fd3101 commit f284027

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Instrumentation/PDO/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,14 @@ The extension can be disabled via [runtime configuration](https://opentelemetry.
2323
```shell
2424
OTEL_PHP_DISABLED_INSTRUMENTATIONS=pdo
2525
```
26+
27+
In case UI used to view telemetry data does not support links between spans (for example newrelic),
28+
you can optionally enable setting db statements attribute to `fetchAll` and `execute` spans using
29+
configuration directive:
30+
```
31+
otel.instrumentation.pdo.distribute_statement_to_linked_spans = true
32+
```
33+
or environment variable:
34+
```shell
35+
OTEL_PHP_INSTRUMENTATION_PDO_DISTRIBUTE_STATEMENT_TO_LINKED_SPANS=true
36+
```

src/Instrumentation/PDO/src/PDOInstrumentation.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OpenTelemetry\API\Trace\SpanKind;
1111
use OpenTelemetry\API\Trace\StatusCode;
1212
use OpenTelemetry\Context\Context;
13+
use OpenTelemetry\SDK\Common\Configuration\Configuration;
1314
use function OpenTelemetry\Instrumentation\hook;
1415
use OpenTelemetry\SemConv\TraceAttributes;
1516
use PDO;
@@ -200,6 +201,9 @@ public static function register(): void
200201
'fetchAll',
201202
pre: static function (PDOStatement $statement, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($pdoTracker, $instrumentation) {
202203
$attributes = $pdoTracker->trackedAttributesForStatement($statement);
204+
if (self::isDistributeStatementToLinkedSpansEnabled()) {
205+
$attributes[TraceAttributes::DB_STATEMENT] = $statement->queryString;
206+
}
203207
/** @psalm-suppress ArgumentTypeCoercion */
204208
$builder = self::makeBuilder($instrumentation, 'PDOStatement::fetchAll', $function, $class, $filename, $lineno)
205209
->setSpanKind(SpanKind::KIND_CLIENT)
@@ -221,6 +225,11 @@ public static function register(): void
221225
'execute',
222226
pre: static function (PDOStatement $statement, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($pdoTracker, $instrumentation) {
223227
$attributes = $pdoTracker->trackedAttributesForStatement($statement);
228+
229+
if (self::isDistributeStatementToLinkedSpansEnabled()) {
230+
$attributes[TraceAttributes::DB_STATEMENT] = $statement->queryString;
231+
}
232+
224233
/** @psalm-suppress ArgumentTypeCoercion */
225234
$builder = self::makeBuilder($instrumentation, 'PDOStatement::execute', $function, $class, $filename, $lineno)
226235
->setSpanKind(SpanKind::KIND_CLIENT)
@@ -268,4 +277,13 @@ private static function end(?Throwable $exception): void
268277

269278
$span->end();
270279
}
280+
281+
private static function isDistributeStatementToLinkedSpansEnabled(): bool
282+
{
283+
if (class_exists('OpenTelemetry\SDK\Common\Configuration\Configuration')) {
284+
return Configuration::getBoolean('OTEL_PHP_INSTRUMENTATION_PDO_DISTRIBUTE_STATEMENT_TO_LINKED_SPANS', false);
285+
}
286+
287+
return get_cfg_var('otel.instrumentation.pdo.distribute_statement_to_linked_spans');
288+
}
271289
}

0 commit comments

Comments
 (0)