cocomon

Getting started

Initialize Otel, emit traces, metrics, and logs, then shut the SDK down correctly.

Getting started

The smallest useful setup is:

  1. add comon_otel
  2. call await Otel.init(...)
  3. emit a span, metric, or log
  4. flush or shut down on process exit

Install the core package

dart pub add comon_otel

For Flutter the dependency command is the same:

flutter pub add comon_otel

Minimal 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 Resource with service metadata

Common first options

OptionPurpose
serviceNameResource service name
exporterPreset exporter type such as console or otlpHttp
endpointShared OTLP endpoint
samplerSampling strategy
resource / resourceAttributesAdditional 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_orm and should export query spans and metrics.

On this page