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
5 changes: 5 additions & 0 deletions src/Instrumentation/PDO/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ or environment variable:
OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATION=true
```

The context sources from global propagator by default, but it can be configured using the following environment variables:
```shell
OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATORS=tracecontext
```

The modified query statement by default will not update `DbAttributes::DB_QUERY_TEXT` due to high cardinality risk, but it can be configured using the following configuration directive:
```
otel.instrumentation.pdo.context_propagation.attribute = true
Expand Down
14 changes: 0 additions & 14 deletions src/Instrumentation/PDO/src/ContextInfoPropagator.php

This file was deleted.

74 changes: 74 additions & 0 deletions src/Instrumentation/PDO/src/ContextPropagatorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Contrib\Instrumentation\PDO;

use OpenTelemetry\API\Behavior\LogsMessagesTrait;
use OpenTelemetry\Context\Propagation\MultiTextMapPropagator;
use OpenTelemetry\Context\Propagation\NoopTextMapPropagator;
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
use OpenTelemetry\SDK\Common\Configuration\Configuration;
use OpenTelemetry\SDK\Registry;

class ContextPropagatorFactory
{
use LogsMessagesTrait;

public function create(): TextMapPropagatorInterface | null
{
$propagators = [];
if (class_exists('OpenTelemetry\SDK\Common\Configuration\Configuration')) {
$propagators = Configuration::getList('OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATORS', []);
}

switch (count($propagators)) {
case 0:
return null;
case 1:
$propagator = $this->buildPropagator($propagators[0]);
if ($propagator !== null && is_a($propagator, NoopTextMapPropagator::class)) {
return null;
}

return $propagator;
default:
$props = $this->buildPropagators($propagators);
if ($props) {
return new MultiTextMapPropagator($props);
}

return null;
}
}

/**
* @return ?list<TextMapPropagatorInterface>
*/
private function buildPropagators(array $names): ?array
{
$propagators = [];
foreach ($names as $name) {
$propagator = $this->buildPropagator($name);
if ($propagator !== null && !is_a($propagator, NoopTextMapPropagator::class)) {
$propagators[] = $propagator;
}
}
if (count($propagators) === 0) {
return null;
}

return $propagators;
}

private function buildPropagator(string $name): ?TextMapPropagatorInterface
{
try {
return Registry::textMapPropagator($name);
} catch (\RuntimeException $e) {
self::logWarning($e->getMessage());
}

return null;
}
}
27 changes: 21 additions & 6 deletions src/Instrumentation/PDO/src/PDOInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static function register(): void
Version::VERSION_1_36_0->url(),
);
$pdoTracker = new PDOTracker();
$contextPropagator = (new ContextPropagatorFactory())->create();

// Hook for the new PDO::connect static method
if (method_exists(PDO::class, 'connect')) {
Expand Down Expand Up @@ -110,7 +111,7 @@ public static function register(): void
hook(
PDO::class,
'query',
pre: static function (PDO $pdo, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($pdoTracker, $instrumentation) {
pre: static function (PDO $pdo, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($contextPropagator, $pdoTracker, $instrumentation) {
/** @psalm-suppress ArgumentTypeCoercion */
$builder = self::makeBuilder($instrumentation, 'PDO::query', $function, $class, $filename, $lineno)
->setSpanKind(SpanKind::KIND_CLIENT);
Expand All @@ -135,8 +136,15 @@ public static function register(): void
case 'postgresql':
case 'mysql':
$comments = [];
Globals::propagator()->inject($comments);
$sqlStatement = SqlCommentPropagator::inject($sqlStatement, $comments);
if ($contextPropagator !== null) {
// Propagator passed by user
$contextPropagator->inject($comments);
} else {
// fallback to global propagator if user didn't pass one
Globals::propagator()->inject($comments);
}
// Inject comments into SQL statement
$sqlStatement = SqlCommentInjector::inject($sqlStatement, $comments);
if (ContextPropagation::isAttributeEnabled()) {
$span->setAttributes([
DbAttributes::DB_QUERY_TEXT => $sqlStatement,
Expand All @@ -163,7 +171,7 @@ public static function register(): void
hook(
PDO::class,
'exec',
pre: static function (PDO $pdo, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($pdoTracker, $instrumentation) {
pre: static function (PDO $pdo, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($contextPropagator, $pdoTracker, $instrumentation) {
/** @psalm-suppress ArgumentTypeCoercion */
$builder = self::makeBuilder($instrumentation, 'PDO::exec', $function, $class, $filename, $lineno)
->setSpanKind(SpanKind::KIND_CLIENT);
Expand All @@ -188,8 +196,15 @@ public static function register(): void
case 'postgresql':
case 'mysql':
$comments = [];
Globals::propagator()->inject($comments);
$sqlStatement = SqlCommentPropagator::inject($sqlStatement, $comments);
if ($contextPropagator !== null) {
// Propagator passed by user
$contextPropagator->inject($comments);
} else {
// fallback to global propagator if user didn't pass one
Globals::propagator()->inject($comments);
}
// Inject comments into SQL statement
$sqlStatement = SqlCommentInjector::inject($sqlStatement, $comments);
if (ContextPropagation::isAttributeEnabled()) {
$span->setAttributes([
DbAttributes::DB_QUERY_TEXT => $sqlStatement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use OpenTelemetry\SDK\Common\Configuration\Configuration;

class SqlCommentPropagator implements SqlPropagatorInterface
class SqlCommentInjector implements SqlInjectorInterface
{
public static function isPrepend(): bool
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OpenTelemetry\Contrib\Instrumentation\PDO;

interface SqlPropagatorInterface
interface SqlInjectorInterface
{
public static function inject(string $query, array $comments): string;
}
24 changes: 0 additions & 24 deletions src/Instrumentation/PDO/tests/Unit/ContextInfoPropagatorTest.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Tests\Instrumentation\PDO\Unit;

use OpenTelemetry\API\Baggage\Propagation\BaggagePropagator;
use OpenTelemetry\API\Behavior\Internal\Logging;
use OpenTelemetry\API\LoggerHolder;
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
use OpenTelemetry\Context\Propagation\MultiTextMapPropagator;
use OpenTelemetry\Contrib\Instrumentation\PDO\ContextPropagatorFactory;
use OpenTelemetry\SDK\Common\Configuration\KnownValues;
use PHPUnit\Framework\TestCase;

class ContextPropagatorFactoryTest extends TestCase
{
#[\Override]
public function setUp(): void
{
LoggerHolder::disable();
Logging::disable();
}

/**
* @psalm-suppress ArgumentTypeCoercion
* @dataProvider propagatorsProvider
*/
public function test_create(string $propagators, ?string $expected): void
{
putenv('OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATORS=' . $propagators);
$propagator = (new ContextPropagatorFactory())->create();
if ($expected === null) {
$this->assertNull($propagator);
} else {
$this->assertInstanceOf($expected, $propagator);
}
putenv('OTEL_PHP_INSTRUMENTATION_PDO_CONTEXT_PROPAGATORS');
}

public static function propagatorsProvider(): array
{
return [
[KnownValues::VALUE_BAGGAGE, BaggagePropagator::class],
[KnownValues::VALUE_TRACECONTEXT, TraceContextPropagator::class],
[KnownValues::VALUE_NONE, null],
[sprintf('%s,%s', KnownValues::VALUE_TRACECONTEXT, KnownValues::VALUE_BAGGAGE), MultiTextMapPropagator::class],
['', null],
['invalid', null],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

namespace OpenTelemetry\Tests\Instrumentation\PDO\tests\Unit;

use OpenTelemetry\Contrib\Instrumentation\PDO\SqlCommentPropagator;
use OpenTelemetry\Contrib\Instrumentation\PDO\SqlCommentInjector;
use PHPUnit\Framework\TestCase;

class SqlCommentPropagatorTest extends TestCase
class SqlCommentInjectorTest extends TestCase
{
public function testIsPrependReturnsTrue()
{
$_SERVER['OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_PREPEND'] = true;
$result = SqlCommentPropagator::isPrepend();
$result = SqlCommentInjector::isPrepend();
$this->assertTrue($result);
}

public function testIsPrependReturnsFalse()
{
$_SERVER['OTEL_PHP_INSTRUMENTATION_PDO_SQL_COMMENTER_PREPEND'] = false;
$result = SqlCommentPropagator::isPrepend();
$result = SqlCommentInjector::isPrepend();
$this->assertFalse($result);
}

Expand All @@ -32,7 +32,7 @@ public function testInjectPrepend()
'key3' => 'value3',
];
$query = 'SELECT 1;';
$result = SqlCommentPropagator::inject($query, $comments);
$result = SqlCommentInjector::inject($query, $comments);
$this->assertEquals("/*key1='value1',key2='value2',key3='value3'*/SELECT 1;", $result);
}

Expand All @@ -45,7 +45,7 @@ public function testInjectAppend()
'key3' => 'value3',
];
$query = 'SELECT 1;';
$result = SqlCommentPropagator::inject($query, $comments);
$result = SqlCommentInjector::inject($query, $comments);
$this->assertEquals("SELECT 1/*key1='value1',key2='value2',key3='value3'*/;", $result);
}
}