44
55namespace Sentry \Logs ;
66
7- use Sentry \Client ;
87use Sentry \Event ;
98use Sentry \EventId ;
109use Sentry \SentrySdk ;
10+ use Sentry \State \Scope ;
1111
1212/**
1313 * @phpstan-import-type AttributeValue from LogAttribute
@@ -22,9 +22,9 @@ final class LogsAggregator
2222 private $ logs = [];
2323
2424 /**
25- * @param string $message see sprintf for a description of format
26- * @param array<int, string|int|float> $values see sprintf for a description of values
27- * @param array<string, AttributeValue> $attributes additional attributes to add to the log
25+ * @param string $message see sprintf for a description of format
26+ * @param array<int, string|int|float> $values see sprintf for a description of values
27+ * @param array<string, mixed> $attributes additional attributes to add to the log
2828 */
2929 public function add (
3030 LogLevel $ level ,
@@ -38,42 +38,40 @@ public function add(
3838 $ client = $ hub ->getClient ();
3939
4040 // There is no need to continue if there is no client or if logs are disabled
41- // @TODO: This might needs to be re-evaluated when we send logs to allow logging to start before init'ing the client
4241 if ($ client === null || !$ client ->getOptions ()->getEnableLogs ()) {
4342 return ;
4443 }
4544
46- $ span = $ hub ->getSpan ();
47- $ traceId = $ span !== null ? $ span ->getTraceId () : null ;
45+ $ scope = null ;
4846
49- $ options = $ client ->getOptions ();
47+ // This we push and pop a scope to get access to it because there is no accessor for the scope
48+ $ hub ->configureScope (function (Scope $ hubScope ) use (&$ scope ) {
49+ $ scope = $ hubScope ;
50+ });
51+
52+ \assert ($ scope !== null , 'The scope comes from the hub and cannot be null at this point. ' );
53+
54+ $ traceId = $ scope ->getPropagationContext ()->getTraceId ();
5055
5156 // @FIXME The SDK name and version won't work for Laravel & Symfony and other SDKs, needs to be more flexible
5257 $ log = (new Log ($ timestamp , (string ) $ traceId , $ level , vsprintf ($ message , $ values )))
5358 ->setAttribute ('sentry.message.template ' , $ message )
54- ->setAttribute ('sentry.environment ' , $ options ->getEnvironment () ?? Event::DEFAULT_ENVIRONMENT )
55- ->setAttribute ('sentry.sdk.name ' , Client::SDK_IDENTIFIER )
56- ->setAttribute ('sentry.sdk.version ' , Client::SDK_VERSION );
59+ ->setAttribute ('sentry.trace.parent_span_id ' , $ hub ->getSpan () ? $ hub ->getSpan ()->getSpanId () : null );
5760
5861 foreach ($ values as $ key => $ value ) {
5962 $ log ->setAttribute ("sentry.message.parameter. {$ key }" , $ value );
6063 }
6164
6265 foreach ($ attributes as $ key => $ value ) {
63- $ log ->setAttribute ($ key , $ value );
64- }
65-
66- if ($ span !== null ) {
67- $ log ->setAttribute ('sentry.trace.parent_span_id ' , (string ) $ span ->getSpanId ());
68- }
69-
70- // @TODO: Do we want to add the following attributes when we send the log rather then when we create the log?
71- if ($ options ->getServerName () !== null ) {
72- $ log ->setAttribute ('sentry.server.address ' , $ options ->getServerName ());
73- }
74-
75- if ($ options ->getRelease () !== null ) {
76- $ log ->setAttribute ('sentry.release ' , $ options ->getRelease ());
66+ $ attribute = LogAttribute::tryFromValue ($ value );
67+
68+ if ($ attribute === null ) {
69+ $ client ->getOptions ()->getLoggerOrNullLogger ()->info (
70+ \sprintf ("Dropping log attribute {$ key } with value of type '%s' because it is not serializable or an unsupported type. " , \gettype ($ value ))
71+ );
72+ } else {
73+ $ log ->setAttribute ($ key , $ attribute );
74+ }
7775 }
7876
7977 $ this ->logs [] = $ log ;
0 commit comments