Skip to content

Commit 04ccbd1

Browse files
committed
test attribute instrumentation in PHP 8.0+
1 parent 25a2df3 commit 04ccbd1

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
/*
3+
* Copyright 2025 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
The agent should correctly instrument attributes.
9+
*/
10+
11+
/*SKIPIF
12+
<?php
13+
if (version_compare(PHP_VERSION, '8.0', '<')) {
14+
die("skip: attributes are not available for PHP versions < 8.0");
15+
}
16+
*/
17+
18+
/*INI
19+
newrelic.code_level_metrics.enabled = true
20+
*/
21+
22+
/*EXPECT_METRICS_EXIST
23+
Custom/ExampleAttribute::getInfo, 2
24+
*/
25+
26+
/*EXPECT_SPAN_EVENTS_LIKE
27+
[
28+
[
29+
{
30+
"category": "generic",
31+
"type": "Span",
32+
"guid": "??",
33+
"traceId": "??",
34+
"transactionId": "??",
35+
"name": "Custom\/ExampleAttribute::getInfo",
36+
"timestamp": "??",
37+
"duration": "??",
38+
"priority": "??",
39+
"sampled": true,
40+
"parentId": "??"
41+
},
42+
{},
43+
{
44+
"code.lineno": "??",
45+
"code.namespace": "ExampleAttribute",
46+
"code.filepath": "__FILE__",
47+
"code.function": "getInfo"
48+
}
49+
],
50+
[
51+
{
52+
"category": "generic",
53+
"type": "Span",
54+
"guid": "??",
55+
"traceId": "??",
56+
"transactionId": "??",
57+
"name": "Custom\/ExampleAttribute::getInfo",
58+
"timestamp": "??",
59+
"duration": "??",
60+
"priority": "??",
61+
"sampled": true,
62+
"parentId": "??"
63+
},
64+
{},
65+
{
66+
"code.lineno": "??",
67+
"code.namespace": "ExampleAttribute",
68+
"code.filepath": "__FILE__",
69+
"code.function": "getInfo"
70+
}
71+
]
72+
]
73+
*/
74+
75+
/*EXPECT_TRACED_ERRORS null*/
76+
77+
/*EXPECT
78+
Class Attribute Info: This is a sample attribute
79+
Method Attribute Info: This is a method attribute
80+
*/
81+
82+
#[Attribute]
83+
class ExampleAttribute
84+
{
85+
public function __construct(public string $info) {}
86+
public function getInfo(): string
87+
{
88+
return $this->info;
89+
}
90+
}
91+
92+
#[ExampleAttribute("This is a sample attribute")]
93+
class SampleClass
94+
{
95+
#[ExampleAttribute("This is a method attribute")]
96+
public function sampleMethod() {}
97+
}
98+
99+
newrelic_add_custom_tracer("ExampleAttribute::getInfo");
100+
101+
// Reflect the class
102+
$reflectionClass = new ReflectionClass(SampleClass::class);
103+
104+
// Get class attributes
105+
$classAttributes = $reflectionClass->getAttributes(ExampleAttribute::class);
106+
107+
foreach ($classAttributes as $attribute) {
108+
$instance = $attribute->newInstance();
109+
echo "Class Attribute Info: " . $instance->getInfo() . "\n";
110+
}
111+
112+
// Reflect the method
113+
$reflectionMethod = $reflectionClass->getMethod('sampleMethod');
114+
115+
// Get method attributes
116+
$methodAttributes = $reflectionMethod->getAttributes(ExampleAttribute::class);
117+
118+
foreach ($methodAttributes as $attribute) {
119+
$instance = $attribute->newInstance();
120+
echo "Method Attribute Info: " . $instance->getInfo() . "\n";
121+
}

0 commit comments

Comments
 (0)