Skip to content

Report Flutter framework feature flags #2991

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- Add `message` parameter to `captureException()` ([#2882](https://github.com/getsentry/sentry-dart/pull/2882))
- Add module in SentryStackFrame ([#2931](https://github.com/getsentry/sentry-dart/pull/2931))
- Set `SentryOptions.includeModuleInStackTrace = true` to enable this. This may change grouping of exceptions.
- Report Flutter framework feature flags ([#2991](https://github.com/getsentry/sentry-dart/pull/2991))
- This works on Flutter builds that include [this PR](https://github.com/flutter/flutter/pull/168437) which is likely Flutter v3.34 and up

### Dependencies

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:sentry/sentry.dart';

const _featureFlag = 'FLUTTER_ENABLED_FEATURE_FLAGS';

// The Flutter framework feature flag works like this:
// An enabled (experimental) feature gets added to the `FLUTTER_ENABLED_FEATURE_FLAGS`
// dart define. Being in there means the feature is enabled, the feature is disabled
// if it's not in there.
// As a result, we also don't know the whole list of flags, but only the active ones.
//
// See https://github.com/flutter/flutter/pull/168437

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I'm sure you're aware but this reaches into Flutter internals that aren't meant to be public. This isn't covered by Flutter's breaking change policy. I'd assume the format of FLUTTER_ENABLED_FEATURE_FLAGS can and will change, even in a patch release. The current changes seem reasonable, but I'd consider documenting this so that future contributors are aware as well.

class FlutterFrameworkFeatureFlagIntegration
extends Integration<SentryOptions> {
final String flags;

FlutterFrameworkFeatureFlagIntegration({
@visibleForTesting this.flags = const String.fromEnvironment(_featureFlag),
});

@override
FutureOr<void> call(Hub hub, SentryOptions options) {
final enabledFeatureFlags = flags.split(',');

for (final featureFlag in enabledFeatureFlags) {
Sentry.addFeatureFlag(featureFlag, true);
}
options.sdk.addIntegration('FlutterFrameworkFeatureFlag');
}
}

extension FlutterFrameworkFeatureFlagIntegrationX
on List<Integration<SentryOptions>> {
/// For better tree-shake-ability we only add the integration if any feature flag is enabled.
void addFlutterFrameworkFeatureFlagIntegration() {
if (const bool.hasEnvironment(_featureFlag)) {
add(FlutterFrameworkFeatureFlagIntegration());
}
}
}
4 changes: 4 additions & 0 deletions flutter/lib/src/sentry_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'file_system_transport.dart';
import 'flutter_exception_type_identifier.dart';
import 'frame_callback_handler.dart';
import 'integrations/connectivity/connectivity_integration.dart';
import 'integrations/flutter_framework_feature_flag_integration.dart';
import 'integrations/frames_tracking_integration.dart';
import 'integrations/integrations.dart';
import 'integrations/native_app_start_handler.dart';
Expand Down Expand Up @@ -172,6 +173,9 @@ mixin SentryFlutter {
// This tracks Flutter application events, such as lifecycle events.
integrations.add(WidgetsBindingIntegration());

// Adds Flutter framework feature flags.
integrations.addFlutterFrameworkFeatureFlagIntegration();

// The ordering here matters, as we'd like to first start the native integration.
// That allow us to send events to the network and then the Flutter integrations.
final native = _native;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry_flutter/src/integrations/flutter_framework_feature_flag_integration.dart';

void main() {
group(FlutterFrameworkFeatureFlagIntegration, () {
late Fixture fixture;

setUp(() async {
fixture = Fixture();

await Sentry.init((options) {
options.dsn = 'https://example.com/sentry-dsn';
});

// ignore: invalid_use_of_internal_member
fixture.hub = Sentry.currentHub;
// ignore: invalid_use_of_internal_member
fixture.options = fixture.hub.options;
});

tearDown(() {
Sentry.close();
});

test('adds sdk integration', () {
final sut = fixture.getSut('foo,bar,baz');
sut.call(fixture.hub, fixture.options);

expect(
fixture.options.sdk.integrations
.contains('FlutterFrameworkFeatureFlag'),
true);
});

test('adds feature flags', () {
final sut = fixture.getSut('foo,bar,baz');
sut.call(fixture.hub, fixture.options);

// ignore: invalid_use_of_internal_member
final featureFlags = fixture.hub.scope.contexts[SentryFeatureFlags.type]
as SentryFeatureFlags?;

expect(featureFlags, isNotNull);
expect(featureFlags?.values.length, 3);
expect(featureFlags?.values.first.flag, 'foo');
expect(featureFlags?.values.first.result, true);
expect(featureFlags?.values[1].flag, 'bar');
expect(featureFlags?.values[1].result, true);
expect(featureFlags?.values[2].flag, 'baz');
expect(featureFlags?.values[2].result, true);
});
});
}

class Fixture {
late Hub hub;
late SentryOptions options;

FlutterFrameworkFeatureFlagIntegration getSut(String features) {
return FlutterFrameworkFeatureFlagIntegration(flags: features);
}
}
Loading