Skip to content

Commit

Permalink
fix: load contexts not setting the user for transactions (#2395)
Browse files Browse the repository at this point in the history
* update

* remove import

* Update load_contexts_integration_test.dart

* update CHANGELOG

* update

* update
  • Loading branch information
buenaflor authored Nov 11, 2024
1 parent 6d5e62f commit a13ab6e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
```
- ⚠️ Frame tracking will be disabled if a different binding is used

### Fixes

- Apply default IP address (`{{auto}}`) to transactions ([#2395](https://github.com/getsentry/sentry-dart/pull/2395))
- Previously, transactions weren't getting the default IP address when user context was loaded
- Now consistently applies default IP address to both events and transactions when:
- No user context exists
- User context exists but IP address is null

## 8.10.1

### Fixes
Expand Down
3 changes: 3 additions & 0 deletions dart/lib/src/sentry_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ class SentryClient {
return _emptySentryId;
}

preparedTransaction = _createUserOrSetDefaultIpAddress(preparedTransaction)
as SentryTransaction;

preparedTransaction =
await _runBeforeSend(preparedTransaction, hint) as SentryTransaction?;

Expand Down
43 changes: 40 additions & 3 deletions flutter/test/integrations/load_contexts_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:sentry_flutter/src/integrations/load_contexts_integration.dart';
import 'package:sentry/src/sentry_tracer.dart';

import 'fixture.dart';

Expand Down Expand Up @@ -84,7 +85,8 @@ void main() {
expect(event.breadcrumbs!.first.message, 'native-mutated-applied');
});

test('apply default IP to user during captureEvent after loading context',
test(
'apply default IP to user during captureEvent after loading context if ip is null',
() async {
fixture.options.enableScopeSync = true;

Expand All @@ -111,8 +113,43 @@ void main() {

await client.captureEvent(event);

expect(expectedIp, actualIp);
expect(expectedId, actualId);
expect(actualIp, expectedIp);
expect(actualId, expectedId);
});

test(
'apply default IP to user during captureTransaction after loading context if ip is null',
() async {
fixture.options.enableScopeSync = true;

const expectedIp = '{{auto}}';
String? actualIp;

const expectedId = '1';
String? actualId;

fixture.options.beforeSendTransaction = (transaction) {
actualIp = transaction.user?.ipAddress;
actualId = transaction.user?.id;
return transaction;
};

final options = fixture.options;

final user = SentryUser(id: expectedId);
when(fixture.binding.loadContexts())
.thenAnswer((_) async => {'user': user.toJson()});

final client = SentryClient(options);
final tracer =
SentryTracer(SentryTransactionContext('name', 'op'), fixture.hub);
final transaction = SentryTransaction(tracer);

// ignore: invalid_use_of_internal_member
await client.captureTransaction(transaction);

expect(actualIp, expectedIp);
expect(actualId, expectedId);
});
});
}

0 comments on commit a13ab6e

Please sign in to comment.