5
5
namespace Sentry \Tests \Logs ;
6
6
7
7
use PHPUnit \Framework \TestCase ;
8
+ use Sentry \Client ;
8
9
use Sentry \ClientBuilder ;
9
10
use Sentry \Logs \LogLevel ;
10
11
use Sentry \Logs \LogsAggregator ;
11
12
use Sentry \SentrySdk ;
12
13
use Sentry \State \Hub ;
14
+ use Sentry \State \Scope ;
15
+ use Sentry \Tracing \Span ;
16
+ use Sentry \Tracing \SpanContext ;
17
+ use Sentry \Tracing \SpanId ;
18
+ use Sentry \Tracing \TraceId ;
19
+ use Sentry \UserDataBag ;
13
20
14
21
final class LogsAggregatorTest extends TestCase
15
22
{
@@ -35,7 +42,7 @@ public function testMessageFormatting(string $message, array $values, string $ex
35
42
36
43
$ log = $ logs [0 ];
37
44
38
- $ this ->assertEquals ($ expected , $ log ->getBody ());
45
+ $ this ->assertSame ($ expected , $ log ->getBody ());
39
46
}
40
47
41
48
public static function messageFormattingDataProvider (): \Generator
@@ -82,4 +89,54 @@ public static function messageFormattingDataProvider(): \Generator
82
89
: 'Message with a percentage: 42 ' ,
83
90
];
84
91
}
92
+
93
+ public function testAttributesAreAddedToLogMessage (): void
94
+ {
95
+ $ client = ClientBuilder::create ([
96
+ 'enable_logs ' => true ,
97
+ 'release ' => '1.0.0 ' ,
98
+ 'environment ' => 'production ' ,
99
+ 'server_name ' => 'web-server-01 ' ,
100
+ ])->getClient ();
101
+
102
+ $ hub = new Hub ($ client );
103
+ SentrySdk::setCurrentHub ($ hub );
104
+
105
+ $ hub ->configureScope (function (Scope $ scope ) {
106
+ $ userDataBag = new UserDataBag ();
107
+ $ userDataBag ->setId ('unique_id ' );
108
+ $ userDataBag->
setEmail (
'[email protected] ' );
109
+ $ userDataBag ->setUsername ('my_user ' );
110
+ $ scope ->setUser ($ userDataBag );
111
+ });
112
+
113
+ $ spanContext = new SpanContext ();
114
+ $ spanContext ->setTraceId (new TraceId ('566e3688a61d4bc888951642d6f14a19 ' ));
115
+ $ spanContext ->setSpanId (new SpanId ('566e3688a61d4bc8 ' ));
116
+ $ span = new Span ($ spanContext );
117
+ $ hub ->setSpan ($ span );
118
+
119
+ $ aggregator = new LogsAggregator ();
120
+
121
+ $ aggregator ->add (LogLevel::info (), 'User %s performed action %s ' , [
122
+ 'testuser ' , 'login ' ,
123
+ ]);
124
+
125
+ $ logs = $ aggregator ->all ();
126
+ $ this ->assertCount (1 , $ logs );
127
+
128
+ $ log = $ logs [0 ];
129
+ $ attributes = $ log ->attributes ();
130
+
131
+ $ this ->assertSame ('1.0.0 ' , $ attributes ->get ('sentry.release ' )->getValue ());
132
+ $ this ->assertSame ('production ' , $ attributes ->get ('sentry.environment ' )->getValue ());
133
+ $ this ->assertSame ('web-server-01 ' , $ attributes ->get ('sentry.server.address ' )->getValue ());
134
+ $ this ->assertSame ('User %s performed action %s ' , $ attributes ->get ('sentry.message.template ' )->getValue ());
135
+ $ this ->assertSame ('566e3688a61d4bc8 ' , $ attributes ->get ('sentry.trace.parent_span_id ' )->getValue ());
136
+ $ this ->assertSame ('sentry.php ' , $ attributes ->get ('sentry.sdk.name ' )->getValue ());
137
+ $ this ->assertSame (Client::SDK_VERSION , $ attributes ->get ('sentry.sdk.version ' )->getValue ());
138
+ $ this ->assertSame ('unique_id ' , $ attributes ->get ('user.id ' )->getValue ());
139
+ $ this ->
assertSame (
'[email protected] ' ,
$ attributes->
get (
'user.email ' )->
getValue ());
140
+ $ this ->assertSame ('my_user ' , $ attributes ->get ('user.name ' )->getValue ());
141
+ }
85
142
}
0 commit comments