Skip to content

Commit ccb94b0

Browse files
committed
PostgreSQL instrumentation test
1 parent 38f0f9c commit ccb94b0

File tree

5 files changed

+646
-23
lines changed

5 files changed

+646
-23
lines changed

docker-compose.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ services:
1818
MONGODB_HOST: ${MONGODB_HOST:-mongodb}
1919
MONGODB_PORT: ${MONGODB_PORT:-27017}
2020
MYSQL_HOST: ${MYSQL_HOST:-mysql}
21+
POSTGRESQL_HOST: ${POSTGRESQL_HOST:-postgresql}
2122

2223
zipkin:
2324
image: openzipkin/zipkin-slim
@@ -88,3 +89,20 @@ services:
8889
retries: 3
8990
volumes:
9091
- ./docker/mysql/init.sql:/docker-entrypoint-initdb.d/init.sql
92+
93+
postgresql:
94+
image: postgres:17.5
95+
hostname: postgresql
96+
ports:
97+
- "5432:5432/tcp"
98+
environment:
99+
POSTGRES_DB: otel_db
100+
POSTGRES_USER: otel_user
101+
POSTGRES_PASSWORD: otel_passwd
102+
healthcheck:
103+
test: pg_isready -U otel_user -d otel_db
104+
interval: 30s
105+
timeout: 30s
106+
retries: 3
107+
volumes:
108+
- ./docker/postgresql/init.sql:/docker-entrypoint-initdb.d/init.sql

docker/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ RUN install-php-extensions \
88
mongodb \
99
amqp \
1010
rdkafka \
11-
mysqli
11+
mysqli \
12+
pgsql
1213

1314
USER php

src/Instrumentation/PostgreSql/src/PostgreSqlInstrumentation.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ class PostgreSqlInstrumentation
2929
public static function register(): void
3030
{
3131

32-
//TODO DB_OPERATION_BATCH_SIZE
33-
//TODO Large objet - track by PgLob instance
34-
//db.response.status_code
35-
3632
// https://opentelemetry.io/docs/specs/semconv/database/postgresql/
3733
$instrumentation = new CachedInstrumentation(
3834
'io.opentelemetry.contrib.php.postgresql',
@@ -69,7 +65,7 @@ public static function register(): void
6965
self::basicPreHook('pg_convert', $instrumentation, $tracker, ...$args);
7066
},
7167
post: static function (...$args) use ($instrumentation, $tracker) {
72-
self::tableOperationsPostHook($instrumentation, $tracker, true, ...$args);
68+
self::tableOperationsPostHook($instrumentation, $tracker, true, null, ...$args);
7369
}
7470
);
7571

@@ -82,7 +78,7 @@ public static function register(): void
8278
self::basicPreHook('pg_copy_from', $instrumentation, $tracker, ...$args);
8379
},
8480
post: static function (...$args) use ($instrumentation, $tracker) {
85-
self::tableOperationsPostHook($instrumentation, $tracker, false, ...$args);
81+
self::tableOperationsPostHook($instrumentation, $tracker, false, null, ...$args);
8682
}
8783
);
8884

@@ -93,7 +89,7 @@ public static function register(): void
9389
self::basicPreHook('pg_copy_to', $instrumentation, $tracker, ...$args);
9490
},
9591
post: static function (...$args) use ($instrumentation, $tracker) {
96-
self::tableOperationsPostHook($instrumentation, $tracker, false, ...$args);
92+
self::tableOperationsPostHook($instrumentation, $tracker, false, null, ...$args);
9793
}
9894
);
9995

@@ -104,7 +100,7 @@ public static function register(): void
104100
self::basicPreHook('pg_delete', $instrumentation, $tracker, ...$args);
105101
},
106102
post: static function (...$args) use ($instrumentation, $tracker) {
107-
self::tableOperationsPostHook($instrumentation, $tracker, false, ...$args);
103+
self::tableOperationsPostHook($instrumentation, $tracker, false, 'DELETE', ...$args);
108104
}
109105
);
110106

@@ -126,7 +122,7 @@ public static function register(): void
126122
self::basicPreHook('pg_execute', $instrumentation, $tracker, ...$args);
127123
},
128124
post: static function (...$args) use ($instrumentation, $tracker) {
129-
self::executePostHook($instrumentation, false, $tracker, ...$args);
125+
self::executePostHook($instrumentation, $tracker, false, ...$args);
130126
}
131127
);
132128

@@ -315,13 +311,16 @@ private static function basicPostHook(CachedInstrumentation $instrumentation, Pg
315311
self::endSpan($attributes, $exception, $errorStatus);
316312
}
317313

318-
private static function tableOperationsPostHook(CachedInstrumentation $instrumentation, PgSqlTracker $tracker, bool $dropIfNoError, $obj, array $params, mixed $retVal, ?\Throwable $exception)
314+
private static function tableOperationsPostHook(CachedInstrumentation $instrumentation, PgSqlTracker $tracker, bool $dropIfNoError, ?string $operationName, $obj, array $params, mixed $retVal, ?\Throwable $exception)
319315
{
320316
$connection = $params[0];
321317
$attributes = null;
322318
if ($connection instanceof Connection) {
323319
$attributes = $tracker->getConnectionAttributes($connection);
324-
$attributes[TraceAttributes::DB_NAMESPACE] = mb_convert_encoding($params[1], 'UTF-8');
320+
$attributes[TraceAttributes::DB_COLLECTION_NAME] = mb_convert_encoding($params[1], 'UTF-8');
321+
if ($operationName) {
322+
$attributes[TraceAttributes::DB_OPERATION_NAME] = $operationName;
323+
}
325324
}
326325

327326
$errorStatus = $retVal == false ? pg_last_error($params[0]) : null;
@@ -464,6 +463,7 @@ private static function selectPostHook(CachedInstrumentation $instrumentation, P
464463
if ($query) {
465464
$attributes[TraceAttributes::DB_QUERY_TEXT] = mb_convert_encoding($query, 'UTF-8');
466465
}
466+
$attributes[TraceAttributes::DB_COLLECTION_NAME] = mb_convert_encoding($table, 'UTF-8');
467467
$attributes[TraceAttributes::DB_OPERATION_NAME] = 'SELECT';
468468
}
469469

@@ -511,9 +511,6 @@ private static function loReadPostHook(CachedInstrumentation $instrumentation, P
511511
if ($connection = $tracker->getConnectionFromLob($lob)) {
512512
$attributes = $tracker->getConnectionAttributes($connection);
513513
}
514-
if ($retVal !== false) {
515-
$attributes['db.postgres.bytes_read'] = $params[1];
516-
}
517514
}
518515
$attributes[TraceAttributes::DB_OPERATION_NAME] = 'READ';
519516
$errorStatus = $retVal == false ? pg_last_error($params[0]) : null;
@@ -551,14 +548,7 @@ private static function loUnlinkPostHook(CachedInstrumentation $instrumentation,
551548

552549
private static function loImportExportPostHook(CachedInstrumentation $instrumentation, PgSqlTracker $tracker, string $operation, $obj, array $params, mixed $retVal, ?\Throwable $exception)
553550
{
554-
$attributes = [];
555-
556-
$lob = $params[0];
557-
if ($lob instanceof Lob) {
558-
if ($connection = $tracker->getConnectionFromLob($lob)) {
559-
$attributes = $tracker->getConnectionAttributes($connection);
560-
}
561-
}
551+
$attributes = $tracker->getConnectionAttributes($params[0]);
562552
$attributes[TraceAttributes::DB_OPERATION_NAME] = $operation;
563553

564554
$errorStatus = $retVal == false ? pg_last_error($params[0]) : null;

0 commit comments

Comments
 (0)