Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PDO] fix: Explicitly convert PDO queries to utf-8 #296

Merged
merged 6 commits into from
Oct 2, 2024
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
6 changes: 5 additions & 1 deletion src/Instrumentation/PDO/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
"ext-pdo": "*",
"ext-opentelemetry": "*",
"open-telemetry/api": "^1.0",
"open-telemetry/sem-conv": "^1.24"
"open-telemetry/sem-conv": "^1.24",
"symfony/polyfill-mbstring": "^1.31"
},
"suggest": {
"ext-mbstring": "For better performance than symfony/polyfill-mbstring"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3",
Expand Down
6 changes: 3 additions & 3 deletions src/Instrumentation/PDO/src/PDOInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function register(): void
$builder = self::makeBuilder($instrumentation, 'PDO::query', $function, $class, $filename, $lineno)
->setSpanKind(SpanKind::KIND_CLIENT);
if ($class === PDO::class) {
$builder->setAttribute(TraceAttributes::DB_STATEMENT, $params[0] ?? 'undefined');
$builder->setAttribute(TraceAttributes::DB_STATEMENT, mb_convert_encoding($params[0] ?? 'undefined', 'UTF-8'));
}
$parent = Context::getCurrent();
$span = $builder->startSpan();
Expand All @@ -93,7 +93,7 @@ public static function register(): void
$builder = self::makeBuilder($instrumentation, 'PDO::exec', $function, $class, $filename, $lineno)
->setSpanKind(SpanKind::KIND_CLIENT);
if ($class === PDO::class) {
$builder->setAttribute(TraceAttributes::DB_STATEMENT, $params[0] ?? 'undefined');
$builder->setAttribute(TraceAttributes::DB_STATEMENT, mb_convert_encoding($params[0] ?? 'undefined', 'UTF-8'));
}
$parent = Context::getCurrent();
$span = $builder->startSpan();
Expand All @@ -116,7 +116,7 @@ public static function register(): void
$builder = self::makeBuilder($instrumentation, 'PDO::prepare', $function, $class, $filename, $lineno)
->setSpanKind(SpanKind::KIND_CLIENT);
if ($class === PDO::class) {
$builder->setAttribute(TraceAttributes::DB_STATEMENT, $params[0] ?? 'undefined');
$builder->setAttribute(TraceAttributes::DB_STATEMENT, mb_convert_encoding($params[0] ?? 'undefined', 'UTF-8'));
}
$parent = Context::getCurrent();
$span = $builder->startSpan();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,31 @@ public function test_execute_and_fetch_all_spans_linked_to_prepared_statement_sp
$this->assertCount(1, $fetchAllSpan->getLinks());
$this->assertEquals($prepareSpan->getContext(), $fetchAllSpan->getLinks()[0]->getSpanContext());
}

/**
* Tests that the db statement is encoded as UTF-8, this is relevant for grpc exporter which expects UTF-8 encoding.
*/
public function test_encode_db_statement_as_utf8(): void
{
//setup
$db = self::createDB();
$db->exec($this->fillDB());

$non_utf8_id = mb_convert_encoding('rückwärts', 'ISO-8859-1', 'UTF-8');

$db->prepare("SELECT id FROM technology WHERE id = '{$non_utf8_id}'");
$span_db_prepare = $this->storage->offsetGet(2);
$this->assertTrue(mb_check_encoding($span_db_prepare->getAttributes()->get(TraceAttributes::DB_STATEMENT), 'UTF-8'));
$this->assertCount(3, $this->storage);

$db->query("SELECT id FROM technology WHERE id = '{$non_utf8_id}'");
$span_db_query = $this->storage->offsetGet(3);
$this->assertTrue(mb_check_encoding($span_db_query->getAttributes()->get(TraceAttributes::DB_STATEMENT), 'UTF-8'));
$this->assertCount(4, $this->storage);

$db->exec("SELECT id FROM technology WHERE id = '{$non_utf8_id}'");
$span_db_exec = $this->storage->offsetGet(4);
$this->assertTrue(mb_check_encoding($span_db_exec->getAttributes()->get(TraceAttributes::DB_STATEMENT), 'UTF-8'));
$this->assertCount(5, $this->storage);
}
}
Loading