Custom Instrumentation for Dart

Learn how to capture performance data on any action in your app using the Sentry Dart SDK.

To use the Transaction Mode tab, set options.traceLifecycle = SentryTraceLifecycle.static during SDK initialization.

To instrument certain regions of your code, you can create a transaction (or a service span in stream mode) to capture them.

The following example creates a transaction/service span that contains an expensive operation (for example, processOrderBatch), and sends the result to Sentry:

Copied
import 'package:sentry/sentry.dart';

// Pass parentSpan: null to force a new service span.
// The status is set to error automatically if the callback throws.
await Sentry.startSpan(
  'processOrderBatch()',
  (span) async {
    span.setAttribute('sentry.op', SentryAttribute.string('task'));
    processOrderBatch();
  },
  parentSpan: null,
);

  • Transaction mode: Transactions aren't bound to the scope, so they have to be passed manually as a method parameter to attach nested spans. Keep in mind that each individual span also needs to be manually finished, and spans are sent together with their parent transaction when the transaction is finished.
  • Stream mode: Nested spans attach to the active span automatically — no manual passing needed. When creating a nested span, you can choose its name and sentry.op (see example below). Each span ends automatically when its callback completes, and spans are streamed as they finish.
Copied
import 'package:sentry/sentry.dart';

await Sentry.startSpan(
  'processOrderBatch()',
  (span) async {
    span.setAttribute('sentry.op', SentryAttribute.string('task'));
    await processOrderBatch();
  },
  parentSpan: null,
);

// No need to pass the parent span — nested spans attach to the active span.
Future<void> processOrderBatch() async {
  await Sentry.startSpan(
    'operation',
    (innerSpan) async {
      innerSpan.setAttribute('sentry.op', SentryAttribute.string('task'));
      // omitted code
    },
  );
}

  • Transaction mode: When you want to attach spans to an already ongoing transaction, use Sentry.getSpan(). It returns the running SentryTransaction, or null if there is none.
  • Stream Mode: There's no public API to retrieve the active span. You don't need one: Sentry.startSpan attaches to the currently active span automatically, or starts a new service span if none is active.
Copied
import 'package:sentry/sentry.dart';

// Attaches to the active span, or starts a new service span if none is active.
await Sentry.startSpan('task', (span) async {
  processOrderBatch();
});

Sentry errors can be linked with transactions and spans.

Errors reported to Sentry while a transaction or span bound to the scope is running are linked automatically.

  • Transaction mode: Set bindToScope: true.
  • Stream mode: The active span is bound automatically, so no flag is needed.
Copied
import 'package:sentry/sentry.dart';

// The active span is bound automatically — no bindToScope needed.
await Sentry.startSpan('processOrderBatch()', (span) async {
  try {
    processOrderBatch();
  } catch (exception) {
    Sentry.captureException(exception);
  }
});

Exceptions may be thrown within spans that can finish before the exception gets reported to Sentry.

  • Transaction mode: Link it by calling the throwable setter.
  • Stream mode: An exception thrown inside the callback is recorded on the span and rethrown automatically.
Copied
import 'package:sentry/sentry.dart';

// If the callback throws, the span is marked as errored and the error is rethrown.
await Sentry.startSpan('processOrderBatch()', (span) async {
  processOrderBatch();
});

You can add data attributes to your transactions and spans. This data is visible in the trace explorer in Sentry.

  • Transaction mode: Use setData with String, int, double or bool values, as well as (non-mixed) arrays of these types.
  • Stream mode: Use setAttribute or setAttributes with a typed SentryAttribute factory.

Copied
await Sentry.startSpan(
  'my-transaction',
  (span) async {
    span.setAttribute('sentry.op', SentryAttribute.string('http.server'));
    span.setAttribute('data_attribute_1', SentryAttribute.string('value1'));
    span.setAttribute('data_attribute_2', SentryAttribute.int(42));
    span.setAttribute('data_attribute_3', SentryAttribute.bool(true));

    span.setAttributes({
      'data_attribute_4': SentryAttribute.stringArray(['value1', 'value2']),
      'data_attribute_5': SentryAttribute.intArray([42, 43]),
      'data_attribute_6': SentryAttribute.boolArray([true, false])
    });
  },
  // force the creation of a service span
  parentSpan: null,
);

Copied
await Sentry.startSpan('http.client', (span) async {
  span.setAttribute('data_attribute_1', SentryAttribute.string('value1'));
  span.setAttribute('data_attribute_2', SentryAttribute.int(42));
  span.setAttribute('data_attribute_3', SentryAttribute.bool(true));

  span.setAttributes({
    'data_attribute_4': SentryAttribute.stringArray(['value1', 'value2']),
    'data_attribute_5': SentryAttribute.intArray([42, 43]),
    'data_attribute_6': SentryAttribute.boolArray([true, false])
  });
});
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").