observability_flutter 0.0.1 copy "observability_flutter: ^0.0.1" to clipboard
observability_flutter: ^0.0.1 copied to clipboard

A comprehensive Flutter package for integrating observability tools into your applications. Provides a unified API for logging, error tracking, performance monitoring, and analytics across multiple pr [...]

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:observability_example/observability_providers/sentry_observability_service.dart';
import 'package:observability_flutter/observability_flutter.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await ObservabilityManager.instance.initialize(
    providers: [
      DebugObservabilityService(),
      SentryObservabilityService(
        dns:
            'https://405f4f0ec404408bcc17c2b82aa14474@o4510278257213440.ingest.us.sentry.io/4510796276760576',
        config: {'tracesSampleRate': 1.0, 'enableLogs': true},
      ),
    ],
  );

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Observability Demo',
      theme: ThemeData(colorScheme: .fromSeed(seedColor: Colors.orange)),
      home: const MyHomePage(title: 'Flutter Observability Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _showSnackbar(String message) {
    ScaffoldMessenger.of(
      context,
    ).showSnackBar(SnackBar(content: Text(message)));
  }

  Future<String> _fetchData({bool? forceException = false}) async {
    final transaction = ObservabilityManager.instance.startTransaction(
      name: 'fetchData',
      operation: 'data_fetching',
      tags: {'source': 'button_press'},
    );
    try {
      final result = await Future.delayed(
        const Duration(seconds: 2),
        () => 'Data loaded',
      );
      if (forceException == true) {
        throw Exception('Forced exception for testing');
      }
      transaction.finish();
      return result;
    } catch (e, s) {
      transaction.finishWithError(e, stackTrace: s);
      rethrow;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Wrap(
          spacing: 10,
          runSpacing: 10,
          children: [
            ElevatedButton(
              onPressed: () {
                'This is an info log'.logAsInfo(extras: {'button': 'info_log'});
                _showSnackbar('Info log sent');
              },
              child: const Text('Log Info'),
            ),
            ElevatedButton(
              onPressed: () {
                'This is a debug log'.logAsDebug(
                  extras: {'button': 'debug_log'},
                );
                _showSnackbar('Debug log sent');
              },
              child: const Text('Log Debug'),
            ),
            ElevatedButton(
              onPressed: () {
                'This is a warning log'.logAsWarning(
                  extras: {'button': 'warning_log'},
                );
                _showSnackbar('Warning log sent');
              },
              child: const Text('Log Warning'),
            ),
            ElevatedButton(
              onPressed: () {
                try {
                  _executeError();
                } catch (e, stack) {
                  ObservabilityManager.instance.recordError(
                    e,
                    stackTrace: stack,
                    severity: ErrorSeverity.high,
                    context: ObservabilityContext(
                      tags: {'button': 'error_log'},
                    ),
                  );
                  _showSnackbar('Error recorded');
                }
              },
              child: const Text('Throw Exception'),
            ),
            ElevatedButton(
              onPressed: () async {
                final data = await _fetchData(forceException: false);
                _showSnackbar(data);
              },
              child: const Text('Start Transaction'),
            ),
            ElevatedButton(
              onPressed: () async {
                try {
                  final data = await _fetchData(forceException: true);
                  _showSnackbar(data);
                } catch (e) {
                  _showSnackbar('Transaction failed with error');
                }
              },
              child: const Text('Start Failing Transaction'),
            ),
          ],
        ),
      ),
    );
  }

  void _executeError() {
    throw Exception('This is a test exception for observability logging.');
  }
}
4
likes
150
points
13
downloads

Documentation

API reference

Publisher

verified publishermarcus.brasizza.com

Weekly Downloads

A comprehensive Flutter package for integrating observability tools into your applications. Provides a unified API for logging, error tracking, performance monitoring, and analytics across multiple providers like Firebase, Sentry, and more. Easily manage transactions, breadcrumbs, and custom events with a flexible and extensible architecture.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter

More

Packages that depend on observability_flutter