ML-154 Improve Crashlytics reports (#155)

* removed unused analytics event & added `logCrash`

* added analytics to `RemoteConfigService`

* run app with `runZonedGuarded`

* added crash logging to `CameraContainerBloc`

* log product id for IAP errors

* typo

* log crashes in `RemoteConfigService`

* ignore silent `FlutterError`

* fixed `evFromImage` test

* fixed `showBuyProDialog` test

* log errors in console

* depend on iap 0.7.2
This commit is contained in:
Vadim 2024-01-27 23:20:53 +01:00 committed by GitHub
parent 9cb1cbaa90
commit fc37016770
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 171 additions and 138 deletions

4
.vscode/launch.json vendored
View file

@ -41,7 +41,7 @@
"--dart-define",
"cameraPreviewAspectRatio=240/320",
],
"program": "${workspaceFolder}/lib/main_release.dart",
"program": "${workspaceFolder}/lib/main_prod.dart",
},
{
"name": "prod-profile (android)",
@ -54,7 +54,7 @@
"--dart-define",
"cameraPreviewAspectRatio=240/320",
],
"program": "${workspaceFolder}/lib/main_release.dart",
"program": "${workspaceFolder}/lib/main_prod.dart",
},
{
"name": "dev-debug (ios)",

View file

@ -26,11 +26,14 @@ class ApplicationWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final remoteConfigService = env.buildType != BuildType.dev
? const RemoteConfigService(LightmeterAnalytics(api: LightmeterAnalyticsFirebase()))
: const MockRemoteConfigService();
return FutureBuilder(
future: Future.wait<dynamic>([
SharedPreferences.getInstance(),
const LightSensorService(LocalPlatform()).hasSensor(),
if (env.buildType != BuildType.dev) const RemoteConfigService().activeAndFetchFeatures(),
remoteConfigService.activeAndFetchFeatures(),
]),
builder: (_, snapshot) {
if (snapshot.data != null) {
@ -47,8 +50,7 @@ class ApplicationWrapper extends StatelessWidget {
userPreferencesService: userPreferencesService,
volumeEventsService: const VolumeEventsService(LocalPlatform()),
child: RemoteConfigProvider(
remoteConfigService:
env.buildType != BuildType.dev ? const RemoteConfigService() : const MockRemoteConfigService(),
remoteConfigService: remoteConfigService,
child: EquipmentProfileProvider(
storageService: iapService,
child: FilmsProvider(

View file

@ -3,32 +3,56 @@ import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:lightmeter/data/analytics/api/analytics_api_interface.dart';
import 'package:lightmeter/data/analytics/entity/analytics_event.dart';
class LightmeterAnalytics {
final ILightmeterAnalyticsApi _api;
const LightmeterAnalytics({required ILightmeterAnalyticsApi api}) : _api = api;
void init() {
FlutterError.onError = (details) {
if (details.silent) return;
logCrash(details.exception, details.stack);
};
PlatformDispatcher.instance.onError = (error, stack) {
logCrash(error, stack);
return true;
};
}
Future<void> logEvent(
LightmeterAnalyticsEvent event, {
String eventName, {
Map<String, dynamic>? parameters,
}) async {
if (kDebugMode) {
log('<LightmeterAnalytics> logEvent: ${event.name} / $parameters');
if (!kReleaseMode) {
log('<LightmeterAnalytics> logEvent: $eventName / $parameters');
return;
}
return _api.logEvent(
event: event,
eventName,
parameters: parameters,
);
}
Future<void> logUnlockProFeatures(String listTileTitle) async {
return logEvent(
LightmeterAnalyticsEvent.unlockProFeatures,
parameters: {"listTileTitle": listTileTitle},
Future<void> logCrash(
dynamic exception,
StackTrace? stackTrace, {
dynamic reason,
Iterable<Object> information = const [],
}) async {
log(exception.toString(), stackTrace: stackTrace);
if (!kReleaseMode) {
return;
}
return _api.logCrash(
exception,
stackTrace,
reason: reason,
information: information,
);
}
Future<void> setCustomKey(String key, String value) async => _api.setCustomKey(key, value);
}

View file

@ -1,8 +1,15 @@
import 'package:lightmeter/data/analytics/entity/analytics_event.dart';
abstract class ILightmeterAnalyticsApi {
Future<void> logEvent({
required LightmeterAnalyticsEvent event,
Future<void> logEvent(
String eventName, {
Map<String, dynamic>? parameters,
});
Future<void> logCrash(
dynamic exception,
StackTrace? stack, {
dynamic reason,
Iterable<Object> information = const [],
});
Future<void> setCustomKey(String key, String value);
}

View file

@ -1,20 +1,20 @@
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:lightmeter/data/analytics/api/analytics_api_interface.dart';
import 'package:lightmeter/data/analytics/entity/analytics_event.dart';
class LightmeterAnalyticsFirebase implements ILightmeterAnalyticsApi {
const LightmeterAnalyticsFirebase();
@override
Future<void> logEvent({
required LightmeterAnalyticsEvent event,
Future<void> logEvent(
String eventName, {
Map<String, dynamic>? parameters,
}) async {
try {
await FirebaseAnalytics.instance.logEvent(
name: event.name,
name: eventName,
parameters: parameters,
);
} on FirebaseException catch (e) {
@ -23,4 +23,25 @@ class LightmeterAnalyticsFirebase implements ILightmeterAnalyticsApi {
debugPrint(e.toString());
}
}
@override
Future<void> logCrash(
dynamic exception,
StackTrace? stackTrace, {
dynamic reason,
Iterable<Object> information = const [],
}) async {
FirebaseCrashlytics.instance.recordError(
exception,
stackTrace,
reason: reason,
information: information,
fatal: true,
);
}
@override
Future<void> setCustomKey(String key, String value) async {
await FirebaseCrashlytics.instance.setCustomKey(key, value);
}
}

View file

@ -1,3 +0,0 @@
enum LightmeterAnalyticsEvent {
unlockProFeatures,
}

View file

@ -2,9 +2,9 @@ import 'dart:async';
import 'dart:developer';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:flutter/foundation.dart';
import 'package:lightmeter/data/analytics/analytics.dart';
import 'package:lightmeter/data/models/feature.dart';
abstract class IRemoteConfigService {
@ -24,7 +24,9 @@ abstract class IRemoteConfigService {
}
class RemoteConfigService implements IRemoteConfigService {
const RemoteConfigService();
final LightmeterAnalytics analytics;
const RemoteConfigService(this.analytics);
@override
Future<void> activeAndFetchFeatures() async {
@ -73,8 +75,8 @@ class RemoteConfigService implements IRemoteConfigService {
try {
final feature = Feature.values.firstWhere((f) => f.name == value.key);
result[feature] = value.value.toValue(feature);
} catch (e) {
log(e.toString());
} catch (e, stackTrace) {
_logError(e, stackTrace: stackTrace);
}
}
return result;
@ -88,8 +90,8 @@ class RemoteConfigService implements IRemoteConfigService {
for (final key in event.updatedKeys) {
try {
updatedFeatures.add(Feature.values.firstWhere((element) => element.name == key));
} catch (e) {
log(e.toString());
} catch (e, stackTrace) {
_logError(e, stackTrace: stackTrace);
}
}
return updatedFeatures;
@ -99,9 +101,7 @@ class RemoteConfigService implements IRemoteConfigService {
@override
bool isEnabled(Feature feature) => FirebaseRemoteConfig.instance.getBool(feature.name);
void _logError(dynamic throwable, {StackTrace? stackTrace}) {
FirebaseCrashlytics.instance.recordError(throwable, stackTrace);
}
void _logError(dynamic throwable, {StackTrace? stackTrace}) => analytics.logCrash(throwable, stackTrace);
}
class MockRemoteConfigService implements IRemoteConfigService {

View file

@ -1,22 +0,0 @@
import 'dart:developer';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:lightmeter/firebase_options.dart';
Future<void> initializeFirebase({required bool handleErrors}) async {
try {
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
if (handleErrors) {
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
PlatformDispatcher.instance.onError = (error, stack) {
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
return true;
};
}
} catch (e) {
log(e.toString());
}
}

View file

@ -1,18 +1,4 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/application.dart';
import 'package:lightmeter/application_wrapper.dart';
import 'package:lightmeter/environment.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
import 'package:lightmeter/runner.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(
IAPProducts(
products: [IAPProduct(storeId: IAPProductType.paidFeatures.storeId)],
child: const ApplicationWrapper(
Environment.dev(),
child: Application(),
),
),
);
}
Future<void> main() => runLightmeterApp(const Environment.dev());

View file

@ -1,19 +1,4 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/application.dart';
import 'package:lightmeter/application_wrapper.dart';
import 'package:lightmeter/environment.dart';
import 'package:lightmeter/firebase.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
import 'package:lightmeter/runner.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeFirebase(handleErrors: true);
runApp(
const IAPProductsProvider(
child: ApplicationWrapper(
Environment.prod(),
child: Application(),
),
),
);
}
Future<void> main() => runLightmeterApp(const Environment.prod());

View file

@ -1,19 +0,0 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/application.dart';
import 'package:lightmeter/application_wrapper.dart';
import 'package:lightmeter/environment.dart';
import 'package:lightmeter/firebase.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeFirebase(handleErrors: false);
runApp(
const IAPProductsProvider(
child: ApplicationWrapper(
Environment.prod(),
child: Application(),
),
),
);
}

View file

@ -31,8 +31,10 @@ class ServicesProvider extends InheritedWidget {
required super.child,
});
static ServicesProvider of(BuildContext context) {
return context.findAncestorWidgetOfExactType<ServicesProvider>()!;
static ServicesProvider of(BuildContext context) => ServicesProvider.maybeOf(context)!;
static ServicesProvider? maybeOf(BuildContext context) {
return context.findAncestorWidgetOfExactType<ServicesProvider>();
}
@override

35
lib/runner.dart Normal file
View file

@ -0,0 +1,35 @@
import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/widgets.dart';
import 'package:lightmeter/application.dart';
import 'package:lightmeter/application_wrapper.dart';
import 'package:lightmeter/data/analytics/analytics.dart';
import 'package:lightmeter/data/analytics/api/analytics_firebase.dart';
import 'package:lightmeter/environment.dart';
import 'package:lightmeter/firebase_options.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
const _errorsLogger = LightmeterAnalytics(api: LightmeterAnalyticsFirebase());
Future<void> runLightmeterApp(Environment env) async {
runZonedGuarded(
() async {
WidgetsFlutterBinding.ensureInitialized();
if (env.buildType == BuildType.prod) {
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
}
_errorsLogger.init();
final application = ApplicationWrapper(env, child: const Application());
runApp(
env.buildType == BuildType.dev
? IAPProducts(
products: [IAPProduct(storeId: IAPProductType.paidFeatures.storeId)],
child: application,
)
: IAPProductsProvider(child: application),
);
},
_errorsLogger.logCrash,
);
}

View file

@ -7,6 +7,7 @@ import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/data/analytics/analytics.dart';
import 'package:lightmeter/interactors/metering_interactor.dart';
import 'package:lightmeter/platform_config.dart';
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
@ -22,6 +23,7 @@ part 'mock_bloc_container_camera.dart';
class CameraContainerBloc extends EvSourceBlocBase<CameraContainerEvent, CameraContainerState> {
final MeteringInteractor _meteringInteractor;
final LightmeterAnalytics _analytics;
late final _WidgetsBindingObserver _observer;
CameraController? _cameraController;
@ -42,6 +44,7 @@ class CameraContainerBloc extends EvSourceBlocBase<CameraContainerEvent, CameraC
CameraContainerBloc(
this._meteringInteractor,
MeteringCommunicationBloc communicationBloc,
this._analytics,
) : super(
communicationBloc,
const CameraInitState(),
@ -223,8 +226,8 @@ class CameraContainerBloc extends EvSourceBlocBase<CameraContainerEvent, CameraC
Directory(file.path).deleteSync(recursive: true);
return await evFromImage(bytes);
} catch (e) {
log(e.toString());
} catch (e, stackTrace) {
_analytics.logCrash(e, stackTrace);
return null;
}
}

View file

@ -4,6 +4,7 @@ class MockCameraContainerBloc extends CameraContainerBloc {
MockCameraContainerBloc(
super._meteringInteractor,
super.communicationBloc,
super._analytics,
);
@override
@ -72,8 +73,8 @@ class MockCameraContainerBloc extends CameraContainerBloc {
try {
final bytes = (await rootBundle.load(PlatformConfig.cameraStubImage)).buffer.asUint8List();
return await evFromImage(bytes);
} catch (e) {
log(e.toString());
} catch (e, stackTrace) {
log(e.toString(), stackTrace: stackTrace);
return null;
}
}

View file

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/data/models/exposure_pair.dart';
import 'package:lightmeter/platform_config.dart';
import 'package:lightmeter/providers/services_provider.dart';
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
import 'package:lightmeter/screens/metering/components/camera_container/bloc_container_camera.dart';
import 'package:lightmeter/screens/metering/components/camera_container/event_container_camera.dart';
@ -37,10 +38,12 @@ class CameraContainerProvider extends StatelessWidget {
? MockCameraContainerBloc(
MeteringInteractorProvider.of(context),
context.read<MeteringCommunicationBloc>(),
ServicesProvider.of(context).analytics,
)
: CameraContainerBloc(
MeteringInteractorProvider.of(context),
context.read<MeteringCommunicationBloc>(),
ServicesProvider.of(context).analytics,
))
..add(const RequestPermissionEvent()),
child: CameraContainer(

View file

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers/services_provider.dart';
import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/screens/metering/components/shared/readings_container/components/shared/animated_dialog_picker/components/animated_dialog/widget_dialog_animated.dart';
import 'package:lightmeter/screens/shared/transparent_dialog/widget_dialog_transparent.dart';
@ -44,7 +45,12 @@ class ProFeaturesDialog extends StatelessWidget {
),
FilledButton(
onPressed: () {
_close(context).then((_) => IAPProductsProvider.maybeOf(context)?.buy(IAPProductType.paidFeatures));
_close(context).then((_) {
ServicesProvider.maybeOf(context)
?.analytics
.setCustomKey('iap_product_type', IAPProductType.paidFeatures.storeId);
IAPProductsProvider.maybeOf(context)?.buy(IAPProductType.paidFeatures);
});
},
child: Text(S.of(context).unlock),
),

View file

@ -1,27 +1,20 @@
import 'dart:developer';
import 'dart:math' as math;
import 'dart:typed_data';
import 'package:exif/exif.dart';
import 'package:flutter/foundation.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
Future<double?> evFromImage(Uint8List bytes) async {
try {
final tags = await readExifFromBytes(bytes);
final iso = double.tryParse("${tags["EXIF ISOSpeedRatings"]}");
final apertureValueRatio = (tags["EXIF FNumber"]?.values as IfdRatios?)?.ratios.first;
final speedValueRatio = (tags["EXIF ExposureTime"]?.values as IfdRatios?)?.ratios.first;
if (iso == null || apertureValueRatio == null || speedValueRatio == null) {
log('Error parsing EXIF: ${tags.keys}');
return null;
}
final aperture = apertureValueRatio.numerator / apertureValueRatio.denominator;
final speed = speedValueRatio.numerator / speedValueRatio.denominator;
return log2(math.pow(aperture, 2)) - log2(speed) - log2(iso / 100);
} catch (e) {
log(e.toString());
return null;
Future<double> evFromImage(Uint8List bytes) async {
final tags = await readExifFromBytes(bytes);
final iso = double.tryParse("${tags["EXIF ISOSpeedRatings"]}");
final apertureValueRatio = (tags["EXIF FNumber"]?.values as IfdRatios?)?.ratios.first;
final speedValueRatio = (tags["EXIF ExposureTime"]?.values as IfdRatios?)?.ratios.first;
if (iso == null || apertureValueRatio == null || speedValueRatio == null) {
throw FlutterError('Error parsing EXIF: ${tags.keys}');
}
final aperture = apertureValueRatio.numerator / apertureValueRatio.denominator;
final speed = speedValueRatio.numerator / speedValueRatio.denominator;
return log2(math.pow(aperture, 2)) - log2(speed) - log2(iso / 100);
}

View file

@ -28,7 +28,7 @@ dependencies:
m3_lightmeter_iap:
git:
url: "https://github.com/vodemn/m3_lightmeter_iap"
ref: v0.7.1
ref: v0.7.2
m3_lightmeter_resources:
git:
url: "https://github.com/vodemn/m3_lightmeter_resources"

View file

@ -2,6 +2,7 @@ import 'package:bloc_test/bloc_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:lightmeter/data/analytics/analytics.dart';
import 'package:lightmeter/interactors/metering_interactor.dart';
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
import 'package:lightmeter/screens/metering/communication/event_communication_metering.dart' as communication_events;
@ -18,11 +19,14 @@ class _MockMeteringCommunicationBloc
extends MockBloc<communication_events.MeteringCommunicationEvent, communication_states.MeteringCommunicationState>
implements MeteringCommunicationBloc {}
class _MockLightmeterAnalytics extends Mock implements LightmeterAnalytics {}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late _MockMeteringInteractor meteringInteractor;
late _MockMeteringCommunicationBloc communicationBloc;
late _MockLightmeterAnalytics analytics;
late CameraContainerBloc bloc;
const cameraMethodChannel = MethodChannel('plugins.flutter.io/camera');
@ -110,16 +114,21 @@ void main() {
setUpAll(() {
meteringInteractor = _MockMeteringInteractor();
communicationBloc = _MockMeteringCommunicationBloc();
communicationBloc = _MockMeteringCommunicationBloc();
when(() => meteringInteractor.cameraEvCalibration).thenReturn(0.0);
when(meteringInteractor.quickVibration).thenAnswer((_) async {});
analytics = _MockLightmeterAnalytics();
registerFallbackValue(StackTrace.empty);
when(() => analytics.logCrash(any<dynamic>(), any<StackTrace>())).thenAnswer((_) async {});
});
setUp(() {
bloc = CameraContainerBloc(
meteringInteractor,
communicationBloc,
analytics,
);
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(cameraMethodChannel, cameraMethodCallSuccessHandler);

View file

@ -17,7 +17,7 @@ void main() {
'no EXIF',
() {
final bytes = File('assets/launcher_icon_dev_512.png').readAsBytesSync();
expectLater(evFromImage(bytes), completion(null));
expectLater(evFromImage(bytes), throwsFlutterError);
},
);
});