-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix withspan handler nullable + example (#1377)
- class is nullable for pre hooks - add phpt tests for withspan and its interaction with auto root span
- Loading branch information
Showing
3 changed files
with
241 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
tests/Integration/SDK/Trace/test_auto_root_span_withspan_localrootspan.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
--TEST-- | ||
Tests that auto root span, withspan auto-instrumentation and local root span all work together | ||
--SKIPIF-- | ||
<?php if (!extension_loaded('opentelemetry') || phpversion('opentelemetry') < '1.1.0') die('WithSpan requires ext-opentelemetry >= 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-- | ||
<?php | ||
require_once 'vendor/autoload.php'; | ||
|
||
use OpenTelemetry\API\Instrumentation\WithSpan; | ||
use OpenTelemetry\API\Instrumentation\SpanAttribute; | ||
|
||
$root = \OpenTelemetry\API\Trace\LocalRootSpan::current(); | ||
$root->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" | ||
} | ||
] |
132 changes: 132 additions & 0 deletions
132
tests/Integration/SDK/Trace/test_withspan_instrumentation.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
--TEST-- | ||
Auto root span creation | ||
--SKIPIF-- | ||
<?php if (!extension_loaded('opentelemetry') || phpversion('opentelemetry') < '1.1.0') die('WithSpan requires ext-opentelemetry >= 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-- | ||
<?php | ||
require_once 'vendor/autoload.php'; | ||
|
||
use OpenTelemetry\API\Instrumentation\WithSpan; | ||
|
||
class TestClass | ||
{ | ||
#[WithSpan] | ||
public static function bar(): void | ||
{ | ||
self::baz(); | ||
} | ||
#[WithSpan] | ||
private static function baz(): void | ||
{ | ||
//do nothing | ||
} | ||
} | ||
|
||
#[WithSpan] | ||
function foo(): void | ||
{ | ||
var_dump('foo::start'); | ||
TestClass::bar(); | ||
var_dump('foo::end'); | ||
} | ||
|
||
foo(); | ||
?> | ||
--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" | ||
} | ||
] |