@@ -2913,11 +2913,12 @@ class ScopesTest {
29132913 }
29142914
29152915 @Test
2916- fun `adds user fields to log attributes` () {
2916+ fun `adds user fields to log attributes if sendDefaultPii is true ` () {
29172917 val (sut, mockClient) =
29182918 getEnabledScopes {
29192919 it.logs.isEnabled = true
29202920 it.distinctId = " distinctId"
2921+ it.isSendDefaultPii = true
29212922 }
29222923
29232924 sut.configureScope { scope ->
@@ -2951,6 +2952,37 @@ class ScopesTest {
29512952 )
29522953 }
29532954
2955+ @Test
2956+ fun `does not add user fields to log attributes by default` () {
2957+ val (sut, mockClient) =
2958+ getEnabledScopes {
2959+ it.logs.isEnabled = true
2960+ it.distinctId = " distinctId"
2961+ }
2962+
2963+ sut.configureScope { scope ->
2964+ scope.user =
2965+ User ().also {
2966+ it.id = " usrid"
2967+ it.username = " usrname"
2968+ it.email = " user@sentry.io"
2969+ }
2970+ }
2971+ sut.logger().log(SentryLogLevel .WARN , " log message" )
2972+
2973+ verify(mockClient)
2974+ .captureLog(
2975+ check {
2976+ assertEquals(" log message" , it.body)
2977+
2978+ assertNull(it.attributes?.get(" user.id" ))
2979+ assertNull(it.attributes?.get(" user.name" ))
2980+ assertNull(it.attributes?.get(" user.email" ))
2981+ },
2982+ anyOrNull(),
2983+ )
2984+ }
2985+
29542986 @Test
29552987 fun `unset user does provide distinct-id as user-id` () {
29562988 val (sut, mockClient) =
@@ -3125,6 +3157,52 @@ class ScopesTest {
31253157 )
31263158 }
31273159
3160+ @Test
3161+ fun `log event has spanId from active span` () {
3162+ val (sut, mockClient) = getEnabledScopes { it.logs.isEnabled = true }
3163+
3164+ val transaction =
3165+ sut.startTransaction(
3166+ " test transaction" ,
3167+ " test.op" ,
3168+ TransactionOptions ().also { it.isBindToScope = true },
3169+ )
3170+
3171+ sut.logger().log(SentryLogLevel .WARN , " log message" )
3172+
3173+ verify(mockClient)
3174+ .captureLog(
3175+ check {
3176+ assertEquals(" log message" , it.body)
3177+ assertEquals(transaction.spanContext.traceId, it.traceId)
3178+ assertEquals(transaction.spanContext.spanId, it.spanId)
3179+ },
3180+ anyOrNull(),
3181+ )
3182+
3183+ transaction.finish()
3184+ }
3185+
3186+ @Test
3187+ fun `log event has spanId from propagation context when no active span` () {
3188+ val (sut, mockClient) = getEnabledScopes { it.logs.isEnabled = true }
3189+
3190+ var propagationContext: PropagationContext ? = null
3191+ sut.configureScope { propagationContext = it.propagationContext }
3192+
3193+ sut.logger().log(SentryLogLevel .WARN , " log message" )
3194+
3195+ verify(mockClient)
3196+ .captureLog(
3197+ check {
3198+ assertEquals(" log message" , it.body)
3199+ assertEquals(propagationContext!! .traceId, it.traceId)
3200+ assertEquals(propagationContext!! .spanId, it.spanId)
3201+ },
3202+ anyOrNull(),
3203+ )
3204+ }
3205+
31283206 // endregion
31293207
31303208 // region metrics
@@ -4043,6 +4121,54 @@ class ScopesTest {
40434121 )
40444122 }
40454123
4124+ @Test
4125+ fun `metric event has spanId from active span` () {
4126+ val (sut, mockClient) = getEnabledScopes { it.metrics.isEnabled = true }
4127+
4128+ val transaction =
4129+ sut.startTransaction(
4130+ " test transaction" ,
4131+ " test.op" ,
4132+ TransactionOptions ().also { it.isBindToScope = true },
4133+ )
4134+
4135+ sut.metrics().count(" metric name" )
4136+
4137+ verify(mockClient)
4138+ .captureMetric(
4139+ check {
4140+ assertEquals(" metric name" , it.name)
4141+ assertEquals(transaction.spanContext.traceId, it.traceId)
4142+ assertEquals(transaction.spanContext.spanId, it.spanId)
4143+ },
4144+ anyOrNull(),
4145+ anyOrNull(),
4146+ )
4147+
4148+ transaction.finish()
4149+ }
4150+
4151+ @Test
4152+ fun `metric event has spanId from propagation context when no active span` () {
4153+ val (sut, mockClient) = getEnabledScopes { it.metrics.isEnabled = true }
4154+
4155+ var propagationContext: PropagationContext ? = null
4156+ sut.configureScope { propagationContext = it.propagationContext }
4157+
4158+ sut.metrics().count(" metric name" )
4159+
4160+ verify(mockClient)
4161+ .captureMetric(
4162+ check {
4163+ assertEquals(" metric name" , it.name)
4164+ assertEquals(propagationContext!! .traceId, it.traceId)
4165+ assertEquals(propagationContext!! .spanId, it.spanId)
4166+ },
4167+ anyOrNull(),
4168+ anyOrNull(),
4169+ )
4170+ }
4171+
40464172 // endregion
40474173
40484174 @Test
0 commit comments