Skip to content

fix: load contexts not setting the user for transactions #2395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
}
Loading