# Dio Integration | Sentry for Dart

The `sentry_dio` library provides [Dio](https://pub.dev/packages/dio.md) support for Sentry using the [HttpClientAdapter](https://pub.dev/documentation/dio/latest/dio/HttpClientAdapter-class.html). It is able to collect breadcrumbs, run tracing for HTTP requests, and capture events for failed requests.

## [Install](https://docs.sentry.io/.md#install)

To add the Dio integration, add the `sentry_dio` dependency.

`pubspec.yaml`

```yml
dependencies:
  sentry: ^9.5.0
  sentry_dio: ^9.5.0
  dio: ^4.0.0
```

## [Configure](https://docs.sentry.io/.md#configure)

Configuration should happen as early as possible in your application's lifecycle.

```dart
import 'package:sentry_dio/sentry_dio.dart';
import 'package:sentry/sentry.dart';

Future<void> main() async {
  await Sentry.init(
    (options) {
      options.dsn = 'https://examplePublicKey@o0.ingest.sentry.io/0';
    },
    appRunner: initApp, // Init your App.
  );
}

final dio = Dio();

// This *must* be the last initialization step of the Dio setup.
dio.addSentry(...);
```

## [Reporting Bad HTTP Requests as Errors](https://docs.sentry.io/.md#reporting-bad-http-requests-as-errors)

The `Interceptors` can also catch exceptions that may occur during requests — for example [DioError](https://pub.dev/documentation/dio2/latest/dio2/DioError-class.html).

```dart
import 'package:sentry_dio/sentry_dio.dart';

final dio = Dio();

dio.addSentry();

final response = await dio.get<String>('https://wrong-url.dev/');
```

This is an opt-out feature. The following example shows how to disable it:

```dart
await Sentry.init((options) {

  options.captureFailedRequests = false;

});
```

## [Tracing for HTTP Requests](https://docs.sentry.io/.md#tracing-for-http-requests)

The Dio integration also provides insight into tracing for your HTTP requests done with Dio.

### [Instrumentation Behaviour](https://docs.sentry.io/.md#instrumentation-behaviour)

* The created spans will be attached to the transaction on the scope - if no transaction is on the scope the span will not be sent to Sentry.
* The SDK sets the span operation to `http.client` and the description to request `$METHOD $url`. For example, `GET https://sentry.io`.
* The span finishes once the request has been executed.
* The span status depends on either the HTTP response code or `SpanStatus.internalError()` if the code does not match any of Sentry's SpanStatus options.
* When the HTTP request throws an Exception, Sentry's SDK associates this exception to the running span. If you haven't set the SDK to swallow the exception and capture it, the span and `SentryEvent` will be linked when viewing it on the Issue Details page in sentry.io.

### [Prerequisites](https://docs.sentry.io/.md#prerequisites)

Before starting, ensure:

1. The Sentry Flutter SDK is initialized. Learn more [here](https://docs.sentry.io/platforms/dart/guides/flutter.md#configure).
2. Tracing is set up. Learn more [here](https://docs.sentry.io/platforms/dart/guides/flutter/tracing.md).

### [Configure](https://docs.sentry.io/.md#configure-1)

Call `addSentry()` on your instance of \`Dio:

```dart
import 'package:sentry_dio/sentry_dio.dart';

final dio = Dio();

dio.addSentry();
```

### [Verify](https://docs.sentry.io/.md#verify)

#### [1. Execute the Code](https://docs.sentry.io/.md#1-execute-the-code)

```dart
import 'package:sentry_dio/sentry_dio.dart';

Future<void> makeWebRequestWithDio() async {
  final dio = Dio();
  dio.addSentry();

  // If there is no active transaction, start one
  final transaction = Sentry.startTransaction(
    'dio-web-request',
    'request',
    bindToScope: true,
  );
  final span = transaction.startChild(
    'dio',
    description: 'desc',
  );
  Response<String>? response;
  try {
    response = await dio.get<String>(exampleUrl);
      span.status = const SpanStatus.ok();
    } catch (exception, stackTrace) {
      span.throwable = exception;
      span.status = const SpanStatus.internalError();
      await Sentry.captureException(exception, stackTrace: stackTrace);
    } finally {
      await span.finish();
    }
}
```

#### [2. View the Transaction on Sentry.io](https://docs.sentry.io/.md#2-view-the-transaction-on-sentryio)

To view the recorded transaction, log into [sentry.io](https://sentry.io/.md) and open your project. Clicking **Performance** will open a page with transactions, where you can select the just recorded transaction with the name `dio-web-request`.
