Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Instrumentation/PDO/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ The extension can be disabled via [runtime configuration](https://opentelemetry.
```shell
OTEL_PHP_DISABLED_INSTRUMENTATIONS=pdo
```

In case UI used to view telemetry data does not support links between spans (for example newrelic),
you can optionally enable setting db statements attribute to `fetchAll` and `execute` spans using
configuration directive:
```
otel.instrumentation.pdo.distribute_statement_to_linked_spans = true
```
or environment variable:
```shell
OTEL_PHP_INSTRUMENTATION_PDO_DISTRIBUTE_STATEMENT_TO_LINKED_SPANS=true
```
18 changes: 18 additions & 0 deletions src/Instrumentation/PDO/src/PDOInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\Common\Configuration\Configuration;
use function OpenTelemetry\Instrumentation\hook;
use OpenTelemetry\SemConv\TraceAttributes;
use PDO;
Expand Down Expand Up @@ -200,6 +201,9 @@ public static function register(): void
'fetchAll',
pre: static function (PDOStatement $statement, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($pdoTracker, $instrumentation) {
$attributes = $pdoTracker->trackedAttributesForStatement($statement);
if (self::isDistributeStatementToLinkedSpansEnabled()) {
$attributes[TraceAttributes::DB_STATEMENT] = $statement->queryString;
}
/** @psalm-suppress ArgumentTypeCoercion */
$builder = self::makeBuilder($instrumentation, 'PDOStatement::fetchAll', $function, $class, $filename, $lineno)
->setSpanKind(SpanKind::KIND_CLIENT)
Expand All @@ -221,6 +225,11 @@ public static function register(): void
'execute',
pre: static function (PDOStatement $statement, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($pdoTracker, $instrumentation) {
$attributes = $pdoTracker->trackedAttributesForStatement($statement);

if (self::isDistributeStatementToLinkedSpansEnabled()) {
$attributes[TraceAttributes::DB_STATEMENT] = $statement->queryString;
}

/** @psalm-suppress ArgumentTypeCoercion */
$builder = self::makeBuilder($instrumentation, 'PDOStatement::execute', $function, $class, $filename, $lineno)
->setSpanKind(SpanKind::KIND_CLIENT)
Expand Down Expand Up @@ -268,4 +277,13 @@ private static function end(?Throwable $exception): void

$span->end();
}

private static function isDistributeStatementToLinkedSpansEnabled(): bool
{
if (class_exists('OpenTelemetry\SDK\Common\Configuration\Configuration')) {
return Configuration::getBoolean('OTEL_PHP_INSTRUMENTATION_PDO_DISTRIBUTE_STATEMENT_TO_LINKED_SPANS', false);
}

return get_cfg_var('otel.instrumentation.pdo.distribute_statement_to_linked_spans');
}
}
Loading