captureEvent method

Future<SentryId> captureEvent(
  1. SentryEvent event, {
  2. Scope? scope,
  3. dynamic stackTrace,
  4. Hint? hint,
})

Reports an event to Sentry.io.

Implementation

Future<SentryId> captureEvent(
  SentryEvent event, {
  Scope? scope,
  dynamic stackTrace,
  Hint? hint,
}) async {
  if (_sampleRate()) {
    _recordLostEvent(event, DiscardReason.sampleRate);
    _options.logger(
      SentryLevel.debug,
      'Event ${event.eventId.toString()} was dropped due to sampling decision.',
    );
    return _sentryId;
  }

  SentryEvent? preparedEvent = _prepareEvent(event, stackTrace: stackTrace);

  hint ??= Hint();

  if (scope != null) {
    preparedEvent = await scope.applyToEvent(preparedEvent, hint);
  } else {
    _options.logger(
        SentryLevel.debug, 'No scope to apply on event was provided');
  }

  // dropped by scope event processors
  if (preparedEvent == null) {
    return _sentryId;
  }

  preparedEvent = await _runEventProcessors(
    preparedEvent,
    hint,
    eventProcessors: _options.eventProcessors,
  );

  // dropped by event processors
  if (preparedEvent == null) {
    return _sentryId;
  }

  preparedEvent = await _runBeforeSend(
    preparedEvent,
    hint,
  );

  // dropped by beforeSend
  if (preparedEvent == null) {
    return _sentryId;
  }

  var attachments = List<SentryAttachment>.from(scope?.attachments ?? []);
  attachments.addAll(hint.attachments);
  var screenshot = hint.screenshot;
  if (screenshot != null) {
    attachments.add(screenshot);
  }

  var viewHierarchy = hint.viewHierarchy;
  if (viewHierarchy != null) {
    attachments.add(viewHierarchy);
  }

  var traceContext = scope?.span?.traceContext();
  if (traceContext == null) {
    if (scope?.propagationContext.baggage == null) {
      scope?.propagationContext.baggage =
          SentryBaggage({}, logger: _options.logger);
      scope?.propagationContext.baggage?.setValuesFromScope(scope, _options);
    }
    if (scope != null) {
      traceContext = SentryTraceContextHeader.fromBaggage(
          scope.propagationContext.baggage!);
    }
  }

  final envelope = SentryEnvelope.fromEvent(
    preparedEvent,
    _options.sdk,
    dsn: _options.dsn,
    traceContext: traceContext,
    attachments: attachments.isNotEmpty ? attachments : null,
  );

  final id = await captureEnvelope(envelope);
  return id ?? SentryId.empty();
}