Getting started
Initialize Otel, emit traces, metrics, and logs, then shut the SDK down correctly.
Getting started
The smallest useful setup is:
- add
comon_otel - call
await Otel.init(...) - emit a span, metric, or log
- flush or shut down on process exit
Install the core package
dart pub add comon_otelFor Flutter the dependency command is the same:
flutter pub add comon_otelMinimal bootstrap
import 'package:comon_otel/comon_otel.dart';
Future<void> main() async {
await Otel.init(
serviceName: 'my-app',
exporter: OtelExporter.console,
);
final requests = Otel.instance.meter.createIntCounter('requests.total');
final userId = await Otel.instance.tracer.traceAsync<String>(
'load-user',
fn: () async {
requests.add(1, attributes: const <String, Object>{'route': '/user'});
Otel.instance.logger.info('Loading user');
return '42';
},
);
print('Loaded user: $userId');
await Otel.shutdown();
}What Otel.init() does
- creates the global tracer, meter, and logger providers
- configures exporters and processors
- installs a default propagator
- builds a
Resourcewith service metadata
Common first options
| Option | Purpose |
|---|---|
serviceName | Resource service name |
exporter | Preset exporter type such as console or otlpHttp |
endpoint | Shared OTLP endpoint |
sampler | Sampling strategy |
resource / resourceAttributes | Additional resource metadata |
Clean shutdown
Use forceFlush() before a short-lived process exits unexpectedly early. Use shutdown() during normal termination.
await Otel.forceFlush();
await Otel.shutdown();Next steps
- Read Core for traces, metrics, logs, propagation, and exporters.
- Read Flutter for app-side instrumentation.
- Read Dio if you use Dio as an HTTP client.
- Read Logger OpenTelemetry if the app already uses
comon_logger. - Read ORM OpenTelemetry if the service uses
comon_ormand should export query spans and metrics.