From 068086347b41c6d31c545cf9b37e3a9a6c1ff59f Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Sat, 7 Sep 2024 08:51:46 +1000 Subject: [PATCH] fix withspan handler nullable + example (#1377) - class is nullable for pre hooks - add phpt tests for withspan and its interaction with auto root span --- src/API/Instrumentation/WithSpanHandler.php | 2 +- ...auto_root_span_withspan_localrootspan.phpt | 108 ++++++++++++++ .../Trace/test_withspan_instrumentation.phpt | 132 ++++++++++++++++++ 3 files changed, 241 insertions(+), 1 deletion(-) create mode 100644 tests/Integration/SDK/Trace/test_auto_root_span_withspan_localrootspan.phpt create mode 100644 tests/Integration/SDK/Trace/test_withspan_instrumentation.phpt diff --git a/src/API/Instrumentation/WithSpanHandler.php b/src/API/Instrumentation/WithSpanHandler.php index f0f0dc928..979ebcd73 100644 --- a/src/API/Instrumentation/WithSpanHandler.php +++ b/src/API/Instrumentation/WithSpanHandler.php @@ -18,7 +18,7 @@ class WithSpanHandler /** * @psalm-suppress ArgumentTypeCoercion */ - public static function pre(mixed $target, array $params, string $class, string $function, ?string $filename, ?int $lineno, ?array $span_args = [], ?array $attributes = []): void + public static function pre(mixed $target, array $params, ?string $class, string $function, ?string $filename, ?int $lineno, ?array $span_args = [], ?array $attributes = []): void { static $instrumentation; $instrumentation ??= new CachedInstrumentation(name: 'io.opentelemetry.php.with-span', schemaUrl: 'https://opentelemetry.io/schemas/1.25.0'); diff --git a/tests/Integration/SDK/Trace/test_auto_root_span_withspan_localrootspan.phpt b/tests/Integration/SDK/Trace/test_auto_root_span_withspan_localrootspan.phpt new file mode 100644 index 000000000..4e4c492f2 --- /dev/null +++ b/tests/Integration/SDK/Trace/test_auto_root_span_withspan_localrootspan.phpt @@ -0,0 +1,108 @@ +--TEST-- +Tests that auto root span, withspan auto-instrumentation and local root span all work together +--SKIPIF-- += 1.1.0'); ?> +--INI-- +opentelemetry.attr_hooks_enabled = On +opentelemetry.attr_pre_handler_function = OpenTelemetry\API\Instrumentation\WithSpanHandler::pre +opentelemetry.attr_post_handler_function = OpenTelemetry\API\Instrumentation\WithSpanHandler::post +--ENV-- +OTEL_PHP_AUTOLOAD_ENABLED=true +OTEL_PHP_EXPERIMENTAL_AUTO_ROOT_SPAN=true +OTEL_TRACES_EXPORTER=console +OTEL_METRICS_EXPORTER=none +OTEL_LOGS_EXPORTER=none +OTEL_PHP_DETECTORS=none +REQUEST_METHOD=GET +REQUEST_URI=/foo?bar=baz +REQUEST_SCHEME=https +SERVER_NAME=example.com +SERVER_PORT=8080 +HTTP_HOST=example.com:8080 +HTTP_USER_AGENT=my-user-agent/1.0 +REQUEST_TIME_FLOAT=1721706151.242976 +HTTP_TRACEPARENT=00-ff000000000000000000000000000041-ff00000000000041-01 +--FILE-- +updateName('GET updated-name'); + +#[WithSpan] +function foo( + #[SpanAttribute] string $word +): void +{ + //do nothing +} +//"word" -> "bar" should appear as a span attribute +foo('bar'); +?> +--EXPECTF-- +%A +[ + { + "name": "foo", + "context": { + "trace_id": "ff000000000000000000000000000041", + "span_id": "%s", + "trace_state": "", + "trace_flags": 1 + }, + "resource": [], + "parent_span_id": "%s", + "kind": "KIND_INTERNAL", + "start": %d, + "end": %d, + "attributes": { + "code.function": "foo", + "code.filepath": "%s", + "code.lineno": %d, + "word": "bar" + }, + "status": { + "code": "Unset", + "description": "" + }, + "events": [], + "links": [], + "schema_url": "%s" + } +] +[ + { + "name": "GET updated-name", + "context": { + "trace_id": "ff000000000000000000000000000041", + "span_id": "%s", + "trace_state": "", + "trace_flags": 1 + }, + "resource": [], + "parent_span_id": "ff00000000000041", + "kind": "KIND_SERVER", + "start": %d, + "end": %d, + "attributes": { + "url.full": "%s", + "http.request.method": "GET", + "http.request.body.size": "", + "user_agent.original": "my-user-agent\/1.0", + "server.address": "%S", + "server.port": %d, + "url.scheme": "https", + "url.path": "\/foo" + }, + "status": { + "code": "Unset", + "description": "" + }, + "events": [], + "links": [], + "schema_url": "%s" + } +] diff --git a/tests/Integration/SDK/Trace/test_withspan_instrumentation.phpt b/tests/Integration/SDK/Trace/test_withspan_instrumentation.phpt new file mode 100644 index 000000000..b7a11ee2f --- /dev/null +++ b/tests/Integration/SDK/Trace/test_withspan_instrumentation.phpt @@ -0,0 +1,132 @@ +--TEST-- +Auto root span creation +--SKIPIF-- += 1.1.0'); ?> +--INI-- +opentelemetry.attr_hooks_enabled = On +--ENV-- +OTEL_PHP_AUTOLOAD_ENABLED=true +OTEL_TRACES_EXPORTER=console +OTEL_METRICS_EXPORTER=none +OTEL_LOGS_EXPORTER=none +OTEL_PHP_DETECTORS=none +--FILE-- + +--EXPECTF-- +%A +string(10) "foo::start" +string(8) "foo::end" +[ + { + "name": "TestClass::baz", + "context": { + "trace_id": "%s", + "span_id": "%s", + "trace_state": "", + "trace_flags": 1 + }, + "resource": [], + "parent_span_id": "%s", + "kind": "KIND_INTERNAL", + "start": %d, + "end": %d, + "attributes": { + "code.function": "baz", + "code.namespace": "TestClass", + "code.filepath": "Standard input code", + "code.lineno": %d + }, + "status": { + "code": "Unset", + "description": "" + }, + "events": [], + "links": [], + "schema_url": "https:\/\/opentelemetry.io\/schemas\/%d.%d.%d" + } +] +[ + { + "name": "TestClass::bar", + "context": { + "trace_id": "%s", + "span_id": "%s", + "trace_state": "", + "trace_flags": 1 + }, + "resource": [], + "parent_span_id": "%s", + "kind": "KIND_INTERNAL", + "start": %d, + "end": %d, + "attributes": { + "code.function": "bar", + "code.namespace": "TestClass", + "code.filepath": "Standard input code", + "code.lineno": %d + }, + "status": { + "code": "Unset", + "description": "" + }, + "events": [], + "links": [], + "schema_url": "https:\/\/opentelemetry.io\/schemas\/%d.%d.%d" + } +] +[ + { + "name": "foo", + "context": { + "trace_id": "%s", + "span_id": "%s", + "trace_state": "", + "trace_flags": 1 + }, + "resource": [], + "parent_span_id": "", + "kind": "KIND_INTERNAL", + "start": %d, + "end": %d, + "attributes": { + "code.function": "foo", + "code.filepath": "Standard input code", + "code.lineno": %d + }, + "status": { + "code": "Unset", + "description": "" + }, + "events": [], + "links": [], + "schema_url": "https:\/\/opentelemetry.io\/schemas\/%d.%d.%d" + } +]