55namespace Sentry \Tests \Logs ;
66
77use PHPUnit \Framework \TestCase ;
8+ use Sentry \Client ;
89use Sentry \ClientBuilder ;
910use Sentry \Logs \LogLevel ;
1011use Sentry \Logs \LogsAggregator ;
1112use Sentry \SentrySdk ;
1213use 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 ;
1320
1421final class LogsAggregatorTest extends TestCase
1522{
@@ -35,7 +42,7 @@ public function testMessageFormatting(string $message, array $values, string $ex
3542
3643 $ log = $ logs [0 ];
3744
38- $ this ->assertEquals ($ expected , $ log ->getBody ());
45+ $ this ->assertSame ($ expected , $ log ->getBody ());
3946 }
4047
4148 public static function messageFormattingDataProvider (): \Generator
@@ -82,4 +89,54 @@ public static function messageFormattingDataProvider(): \Generator
8289 : 'Message with a percentage: 42 ' ,
8390 ];
8491 }
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 ('foo@example.com ' );
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 ('foo@example.com ' , $ attributes ->get ('user.email ' )->getValue ());
140+ $ this ->assertSame ('my_user ' , $ attributes ->get ('user.name ' )->getValue ());
141+ }
85142}
0 commit comments