diff --git a/src/Instrumentation/PDO/README.md b/src/Instrumentation/PDO/README.md index 7f6303529..7a56fbda4 100644 --- a/src/Instrumentation/PDO/README.md +++ b/src/Instrumentation/PDO/README.md @@ -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 +``` \ No newline at end of file diff --git a/src/Instrumentation/PDO/src/PDOInstrumentation.php b/src/Instrumentation/PDO/src/PDOInstrumentation.php index 312c3104f..2b4b81c98 100644 --- a/src/Instrumentation/PDO/src/PDOInstrumentation.php +++ b/src/Instrumentation/PDO/src/PDOInstrumentation.php @@ -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; @@ -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) @@ -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) @@ -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'); + } }