device_diagnostics 0.1.0
device_diagnostics: ^0.1.0 copied to clipboard
A Flutter package for opt-in device diagnostics, performance monitoring, network instrumentation, and PDF diagnostic reports.
π device_diagnostics #
Flutter Device Diagnostics & Performance Monitoring #
Built with β€οΈ by Rajat Das Β· Connect on LinkedIn
A Flutter package for opt-in device diagnostics, performance monitoring, HTTP network instrumentation, route tracking, and PDF diagnostic reports. Designed to help Flutter developers investigate performance issues, understand runtime behavior, and collect useful diagnostic information during debugging and testing.
β¨ Features #
- π± Device information β Model, manufacturer, OS version, and architecture where available.
- β‘ CPU monitoring β Process CPU usage estimates where supported.
- π§ Memory monitoring β Track application memory usage.
- π‘οΈ Thermal monitoring β Inspect device thermal state where supported.
- π HTTP network instrumentation β Record request and response body-byte metrics through the provided diagnostic client.
- ποΈ Frame monitoring β Observe Flutter frame performance and estimate dropped frames.
- π§ Route tracking β Associate diagnostic activity with supported navigation events.
- π PDF reports β Export collected diagnostic sessions for inspection and sharing.
- ποΈ Flexible visibility modes β Configure diagnostics for Debug, Profile, Release, Debug + Release, or all build modes.
- π‘οΈ Opt-in diagnostics β Choose when and how diagnostics are enabled.
Note: Metric availability and accuracy depend on the platform, OS version, and underlying implementation. Some metrics may be estimated or unavailable.
π¦ Installation #
Add device_diagnostics to your pubspec.yaml using the version currently published on pub.dev:
dependencies:
device_diagnostics: ^0.1.0
Then run:
flutter pub get
Check the latest published version before installing.
π Quick Start #
Initialize the package before calling runApp().
import 'package:flutter/material.dart';
import 'package:device_diagnostics/device_diagnostics.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await DeviceDiagnostics.initialize(
config: const DiagnosticConfig(
visibility: DiagnosticVisibility.debug,
),
);
runApp(
DeviceDiagnostics.observability(
child: MaterialApp(
navigatorObservers: [
DeviceDiagnostics.routeObserver,
],
home: const MyHomePage(),
),
),
);
}
How it works #
DeviceDiagnostics.initialize()initializes the diagnostic configuration.DiagnosticConfigcontrols visibility and supported monitoring options.DeviceDiagnostics.observability()wraps your app with the diagnostic UI.DeviceDiagnostics.routeObservertracks supported navigation events.
ποΈ Visibility Modes #
Choose which Flutter build modes can display the diagnostic interface.
| Mode | Debug | Profile | Release |
|---|---|---|---|
debug |
β Visible | β Hidden | β Hidden |
profile |
β Hidden | β Visible | β Hidden |
release |
β Hidden | β Hidden | β Visible |
debugAndRelease |
β Visible | β Hidden | β Visible |
all |
β Visible | β Visible | β Visible |
Examples #
Debug only
const DiagnosticConfig(
visibility: DiagnosticVisibility.debug,
)
Profile only
const DiagnosticConfig(
visibility: DiagnosticVisibility.profile,
)
Release only
const DiagnosticConfig(
visibility: DiagnosticVisibility.release,
)
Debug and Release
const DiagnosticConfig(
visibility: DiagnosticVisibility.debugAndRelease,
)
All build modes
const DiagnosticConfig(
visibility: DiagnosticVisibility.all,
)
β οΈ Production warning: Enabling diagnostic UI in Release builds is intentional and should be evaluated carefully. Visibility settings are not a security boundary or a replacement for access control.
βοΈ Configuration #
Customize diagnostic sampling and monitoring according to your application's needs.
The following example illustrates the available configuration options; verify the option names against the version you have installed.
const DiagnosticConfig(
visibility: DiagnosticVisibility.debug,
sampleInterval: Duration(seconds: 1),
enableThermalMonitoring: true,
enableCpuMonitoring: true,
enableMemoryMonitoring: true,
enableNetworkMonitoring: true,
enableFrameMonitoring: true,
showFloatingButtonWhenIdle: true,
)
| Option | Description |
|---|---|
visibility |
Selects the build modes in which diagnostics are visible. |
sampleInterval |
Controls how frequently metrics are sampled. |
enableThermalMonitoring |
Enables thermal-state monitoring where supported. |
enableCpuMonitoring |
Enables CPU usage sampling where supported. |
enableMemoryMonitoring |
Enables memory monitoring. |
enableNetworkMonitoring |
Controls network diagnostics supported by the implementation. |
enableFrameMonitoring |
Enables Flutter frame-performance monitoring. |
showFloatingButtonWhenIdle |
Controls the floating diagnostic button while a session is idle. |
Sampling interval: Use an interval that satisfies your configuration validation. If your implementation enforces a 250 ms minimum, configure Duration(milliseconds: 250) or longer.
π HTTP Network Instrumentation #
Use DiagnosticHttpClient to record HTTP body-byte metrics for requests passing through the instrumented client.
import 'package:flutter/foundation.dart';
import 'package:device_diagnostics/device_diagnostics.dart';
Future<void> fetchData() async {
final client = DiagnosticHttpClient();
try {
final response = await client.get(
Uri.parse('https://example.com/api'),
);
debugPrint('Status code: ${response.statusCode}');
} finally {
client.close();
}
}
What can be measured? #
- π€ Outgoing request body bytes.
- π₯ Incoming response body bytes.
- π§ Network activity associated with diagnostic routes, subject to the implementation's attribution behavior.
Important limitations #
- These metrics are not necessarily total device network usage.
- HTTP headers, TLS overhead, retransmissions, and traffic from unrelated clients may not be included.
- Requests made through other HTTP clients or networking APIs are not automatically captured by this client.
π Diagnostic Sessions #
Start a session when you want to investigate a performance issue or reproduce a reported bug.
Start a session #
final controller = DeviceDiagnostics.controller;
await controller.start();
Stop and export a PDF #
await controller.stopAndExportPdf(
share: false,
);
Set share: true if you want to invoke the report-sharing flow supported by the package.
Recommended workflow #
- βΆοΈ Start a diagnostic session.
- π± Reproduce the issue on the target device.
- π§ Navigate through the affected screens.
- βΉοΈ Stop the session and generate the report.
- π Review the report and share it when appropriate.
π PDF Diagnostic Reports #
The package can generate PDF reports from collected diagnostic sessions.
Depending on available data and platform support, reports may include:
- Device and operating-system information.
- CPU and memory measurements.
- Frame-performance observations.
- Recorded HTTP body-byte metrics.
- Screen visits and session timing.
- Diagnostic samples and summary statistics.
Review reports before sharing them. They may contain information about application behavior or usage that should not be distributed publicly.
π§ Route Tracking #
Register the diagnostic route observer with your MaterialApp:
MaterialApp(
navigatorObservers: [
DeviceDiagnostics.routeObserver,
],
home: const MyHomePage(),
);
This lets the package observe supported navigation events and associate diagnostic activity with routes.
Tip: Named routes can make route attribution easier to interpret. Behavior may vary when using custom navigation solutions.
π± Platform Support #
The package currently declares Android and iOS support. Individual metrics have different platform limitations.
| Metric | Android | iOS |
|---|---|---|
| Device information | Supported fields | Supported fields |
| Memory usage | Where available | Where available |
| Process CPU estimate | Where available | Where available |
| Network type | Where available | Where available |
| Network byte counters | Where supported | May be unavailable |
| Thermal state | Supported Android versions | Supported iOS versions |
| Frame monitoring | Flutter-side monitoring | Flutter-side monitoring |
| PDF generation | Subject to package/platform support | Subject to package/platform support |
Metric support is not the same as full platform support. Unsupported values should be treated as unavailable rather than assumed to be zero.
ποΈ Architecture Overview #
device_diagnostics separates diagnostic responsibilities into focused components:
- Configuration layer β Visibility modes and monitoring options.
- Diagnostic controller β Session state, sampling, and collected data.
- Native collectors β Platform-specific metrics exposed through a method channel.
- Network instrumentation β HTTP metrics from the provided client.
- Route observer β Supported navigation-event tracking.
- Diagnostic overlay β On-screen diagnostic controls and information.
- PDF report generator β Creates reports from collected session data.
π Privacy and Performance #
- Enable diagnostics intentionally, especially in Release builds.
- Review diagnostic reports before sharing them.
- Use only the instrumented client when you want its HTTP activity recorded.
- Select a sampling interval appropriate for your workload.
- Test metric accuracy and overhead on physical devices.
- Do not use UI visibility settings as access control for sensitive information.
π οΈ Troubleshooting #
The diagnostic overlay is not visible #
- Check whether the selected
DiagnosticVisibilitymatches the current build mode. - Initialize diagnostics before calling
runApp(). - Ensure the application is wrapped with
DeviceDiagnostics.observability(). - Check the
showFloatingButtonWhenIdlesetting if it is supported by your version.
Some metrics are missing #
- Metric support varies by platform and OS version.
- Some native APIs may return unavailable values.
- iOS network byte counters may not be available in the current implementation.
- Test on a physical device if emulator results are incomplete.
No network activity is recorded #
- Send requests through
DiagnosticHttpClient. - Confirm that network monitoring is enabled if your configuration exposes that option.
- Remember that traffic from unrelated clients and networking APIs may not be captured.
π§βπ» Development #
Run the project's checks before submitting changes:
flutter pub get
dart format .
flutter analyze
flutter test
flutter pub publish --dry-run
To validate the example application:
cd example
flutter pub get
flutter analyze
flutter test
flutter build apk
πΊοΈ Roadmap #
Potential future improvements include:
- π Additional diagnostic visualizations.
- π§ͺ Expanded automated tests.
- π± Broader platform-specific metric support.
- π More report customization.
- π More detailed performance insights.
The roadmap is subject to implementation progress and platform limitations.
π€ Contributing #
Contributions, bug reports, and feature suggestions are welcome.
- Fork the GitHub repository.
- Create a feature branch.
- Implement and test your changes.
- Submit a pull request with a clear description.
π¬ Support #
- π Report an issue
- π» GitHub repository
- π¦ Package on pub.dev
- πΌ Connect on LinkedIn
π License #
Distributed under the MIT License.
Built with β€οΈ by Rajat Das
Flutter Developer Β· Package Creator Β· Open-Source Contributor