Skip to content

Commit 0680863

Browse files
authored
fix withspan handler nullable + example (open-telemetry#1377)
- class is nullable for pre hooks - add phpt tests for withspan and its interaction with auto root span
1 parent bee9c64 commit 0680863

File tree

3 files changed

+241
-1
lines changed

3 files changed

+241
-1
lines changed

src/API/Instrumentation/WithSpanHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WithSpanHandler
1818
/**
1919
* @psalm-suppress ArgumentTypeCoercion
2020
*/
21-
public static function pre(mixed $target, array $params, string $class, string $function, ?string $filename, ?int $lineno, ?array $span_args = [], ?array $attributes = []): void
21+
public static function pre(mixed $target, array $params, ?string $class, string $function, ?string $filename, ?int $lineno, ?array $span_args = [], ?array $attributes = []): void
2222
{
2323
static $instrumentation;
2424
$instrumentation ??= new CachedInstrumentation(name: 'io.opentelemetry.php.with-span', schemaUrl: 'https://opentelemetry.io/schemas/1.25.0');
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
--TEST--
2+
Tests that auto root span, withspan auto-instrumentation and local root span all work together
3+
--SKIPIF--
4+
<?php if (!extension_loaded('opentelemetry') || phpversion('opentelemetry') < '1.1.0') die('WithSpan requires ext-opentelemetry >= 1.1.0'); ?>
5+
--INI--
6+
opentelemetry.attr_hooks_enabled = On
7+
opentelemetry.attr_pre_handler_function = OpenTelemetry\API\Instrumentation\WithSpanHandler::pre
8+
opentelemetry.attr_post_handler_function = OpenTelemetry\API\Instrumentation\WithSpanHandler::post
9+
--ENV--
10+
OTEL_PHP_AUTOLOAD_ENABLED=true
11+
OTEL_PHP_EXPERIMENTAL_AUTO_ROOT_SPAN=true
12+
OTEL_TRACES_EXPORTER=console
13+
OTEL_METRICS_EXPORTER=none
14+
OTEL_LOGS_EXPORTER=none
15+
OTEL_PHP_DETECTORS=none
16+
REQUEST_METHOD=GET
17+
REQUEST_URI=/foo?bar=baz
18+
REQUEST_SCHEME=https
19+
SERVER_NAME=example.com
20+
SERVER_PORT=8080
21+
HTTP_HOST=example.com:8080
22+
HTTP_USER_AGENT=my-user-agent/1.0
23+
REQUEST_TIME_FLOAT=1721706151.242976
24+
HTTP_TRACEPARENT=00-ff000000000000000000000000000041-ff00000000000041-01
25+
--FILE--
26+
<?php
27+
require_once 'vendor/autoload.php';
28+
29+
use OpenTelemetry\API\Instrumentation\WithSpan;
30+
use OpenTelemetry\API\Instrumentation\SpanAttribute;
31+
32+
$root = \OpenTelemetry\API\Trace\LocalRootSpan::current();
33+
$root->updateName('GET updated-name');
34+
35+
#[WithSpan]
36+
function foo(
37+
#[SpanAttribute] string $word
38+
): void
39+
{
40+
//do nothing
41+
}
42+
//"word" -> "bar" should appear as a span attribute
43+
foo('bar');
44+
?>
45+
--EXPECTF--
46+
%A
47+
[
48+
{
49+
"name": "foo",
50+
"context": {
51+
"trace_id": "ff000000000000000000000000000041",
52+
"span_id": "%s",
53+
"trace_state": "",
54+
"trace_flags": 1
55+
},
56+
"resource": [],
57+
"parent_span_id": "%s",
58+
"kind": "KIND_INTERNAL",
59+
"start": %d,
60+
"end": %d,
61+
"attributes": {
62+
"code.function": "foo",
63+
"code.filepath": "%s",
64+
"code.lineno": %d,
65+
"word": "bar"
66+
},
67+
"status": {
68+
"code": "Unset",
69+
"description": ""
70+
},
71+
"events": [],
72+
"links": [],
73+
"schema_url": "%s"
74+
}
75+
]
76+
[
77+
{
78+
"name": "GET updated-name",
79+
"context": {
80+
"trace_id": "ff000000000000000000000000000041",
81+
"span_id": "%s",
82+
"trace_state": "",
83+
"trace_flags": 1
84+
},
85+
"resource": [],
86+
"parent_span_id": "ff00000000000041",
87+
"kind": "KIND_SERVER",
88+
"start": %d,
89+
"end": %d,
90+
"attributes": {
91+
"url.full": "%s",
92+
"http.request.method": "GET",
93+
"http.request.body.size": "",
94+
"user_agent.original": "my-user-agent\/1.0",
95+
"server.address": "%S",
96+
"server.port": %d,
97+
"url.scheme": "https",
98+
"url.path": "\/foo"
99+
},
100+
"status": {
101+
"code": "Unset",
102+
"description": ""
103+
},
104+
"events": [],
105+
"links": [],
106+
"schema_url": "%s"
107+
}
108+
]
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
--TEST--
2+
Auto root span creation
3+
--SKIPIF--
4+
<?php if (!extension_loaded('opentelemetry') || phpversion('opentelemetry') < '1.1.0') die('WithSpan requires ext-opentelemetry >= 1.1.0'); ?>
5+
--INI--
6+
opentelemetry.attr_hooks_enabled = On
7+
--ENV--
8+
OTEL_PHP_AUTOLOAD_ENABLED=true
9+
OTEL_TRACES_EXPORTER=console
10+
OTEL_METRICS_EXPORTER=none
11+
OTEL_LOGS_EXPORTER=none
12+
OTEL_PHP_DETECTORS=none
13+
--FILE--
14+
<?php
15+
require_once 'vendor/autoload.php';
16+
17+
use OpenTelemetry\API\Instrumentation\WithSpan;
18+
19+
class TestClass
20+
{
21+
#[WithSpan]
22+
public static function bar(): void
23+
{
24+
self::baz();
25+
}
26+
#[WithSpan]
27+
private static function baz(): void
28+
{
29+
//do nothing
30+
}
31+
}
32+
33+
#[WithSpan]
34+
function foo(): void
35+
{
36+
var_dump('foo::start');
37+
TestClass::bar();
38+
var_dump('foo::end');
39+
}
40+
41+
foo();
42+
?>
43+
--EXPECTF--
44+
%A
45+
string(10) "foo::start"
46+
string(8) "foo::end"
47+
[
48+
{
49+
"name": "TestClass::baz",
50+
"context": {
51+
"trace_id": "%s",
52+
"span_id": "%s",
53+
"trace_state": "",
54+
"trace_flags": 1
55+
},
56+
"resource": [],
57+
"parent_span_id": "%s",
58+
"kind": "KIND_INTERNAL",
59+
"start": %d,
60+
"end": %d,
61+
"attributes": {
62+
"code.function": "baz",
63+
"code.namespace": "TestClass",
64+
"code.filepath": "Standard input code",
65+
"code.lineno": %d
66+
},
67+
"status": {
68+
"code": "Unset",
69+
"description": ""
70+
},
71+
"events": [],
72+
"links": [],
73+
"schema_url": "https:\/\/opentelemetry.io\/schemas\/%d.%d.%d"
74+
}
75+
]
76+
[
77+
{
78+
"name": "TestClass::bar",
79+
"context": {
80+
"trace_id": "%s",
81+
"span_id": "%s",
82+
"trace_state": "",
83+
"trace_flags": 1
84+
},
85+
"resource": [],
86+
"parent_span_id": "%s",
87+
"kind": "KIND_INTERNAL",
88+
"start": %d,
89+
"end": %d,
90+
"attributes": {
91+
"code.function": "bar",
92+
"code.namespace": "TestClass",
93+
"code.filepath": "Standard input code",
94+
"code.lineno": %d
95+
},
96+
"status": {
97+
"code": "Unset",
98+
"description": ""
99+
},
100+
"events": [],
101+
"links": [],
102+
"schema_url": "https:\/\/opentelemetry.io\/schemas\/%d.%d.%d"
103+
}
104+
]
105+
[
106+
{
107+
"name": "foo",
108+
"context": {
109+
"trace_id": "%s",
110+
"span_id": "%s",
111+
"trace_state": "",
112+
"trace_flags": 1
113+
},
114+
"resource": [],
115+
"parent_span_id": "",
116+
"kind": "KIND_INTERNAL",
117+
"start": %d,
118+
"end": %d,
119+
"attributes": {
120+
"code.function": "foo",
121+
"code.filepath": "Standard input code",
122+
"code.lineno": %d
123+
},
124+
"status": {
125+
"code": "Unset",
126+
"description": ""
127+
},
128+
"events": [],
129+
"links": [],
130+
"schema_url": "https:\/\/opentelemetry.io\/schemas\/%d.%d.%d"
131+
}
132+
]

0 commit comments

Comments
 (0)