Custom Instrumentation

On this page you will learn how to manually propagate trace information into and out of your Dart application.

To obtain a trace header from the span, use ISentrySpan#toSentryTrace() method. Then pass it to the downstream service. If the communication happens over HTTP, it is recommended that you set the value to the sentry-trace HTTP header.

To create a span as a continuation of the trace retrieved from the upstream service, pass the sentry-trace header value to the transaction context:

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

final header = request.headers['sentry-trace'];
final sentryTraceHeader = SentryTraceHeader.fromTraceHeader(header);
try {
  final transaction = Sentry.startTransactionWithContext(SentryTransactionContext.fromSentryTrace('name', 'operation', sentryTraceHeader));
} catch (error) {
  // handle [InvalidSentryTraceHeaderException] if invalid `sentry-trace` header.
}

The SentryHttpClient instrumentation handles trace continuations automatically when enabling the performance feature.

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").