|
| 1 | +--- |
| 2 | +title: Ktor Client |
| 3 | +caseStyle: camelCase |
| 4 | +supportLevel: production |
| 5 | +sdk: sentry.java.ktor-client |
| 6 | +description: "Learn more about the Sentry Ktor Client integration for the Android SDK." |
| 7 | +categories: |
| 8 | + - mobile |
| 9 | +--- |
| 10 | + |
| 11 | +The `sentry-ktor-client` library provides [Ktor Client](https://ktor.io/) support for Sentry via the Sentry Ktor Client Plugin. |
| 12 | + |
| 13 | +On this page, we get you up and running with Sentry's Ktor Client Integration. |
| 14 | +The integration supports: |
| 15 | +- Adding breadcrumbs for each HTTP request. |
| 16 | +- Capturing spans for each HTTP Request, with support for distributed tracing. The span will be created as a child of the current span bound to the scope (if any). |
| 17 | +- Capturing certain request failures as errors in Sentry. |
| 18 | + |
| 19 | +The Sentry Ktor Client integration supports Ktor Client version `3.x`. |
| 20 | + |
| 21 | +<Alert level="warning"> |
| 22 | + If you're using Ktor Client with OKHttp as the engine, and you're already using the [Sentry OkHttp integration](/platforms/android/integrations/okhttp/), |
| 23 | + either with manual or automatic installation (via the Sentry Android Gradle Plugin), you should avoid using this integration, otherwise the SDK will produce duplicate data, instrumenting each HTTP request twice. |
| 24 | + Use the [Sentry OkHttp integration](/platforms/android/integrations/okhttp/) instead, as it provides more detailed information about HTTP request. |
| 25 | +</Alert> |
| 26 | + |
| 27 | +## Install |
| 28 | + |
| 29 | +Add a dependency to the Sentry SDK and to the Sentry Ktor Client Plugin: |
| 30 | + |
| 31 | +```groovy |
| 32 | +implementation 'io.sentry:sentry-android:{{@inject packages.version('sentry.java.android', '8.18.0') }}' |
| 33 | +implementation 'io.sentry:sentry-ktor-client:{{@inject packages.version('sentry.java.ktor-client', '8.18.0') }}' |
| 34 | +``` |
| 35 | + |
| 36 | +## Configure |
| 37 | + |
| 38 | +Create the Ktor HTTP Client with your preferred engine, and install the Sentry Ktor Client Plugin: |
| 39 | + |
| 40 | + |
| 41 | +```kotlin |
| 42 | +import io.ktor.client.* |
| 43 | +import io.ktor.client.engine.android.* |
| 44 | +import io.sentry.ktorClient.SentryKtorClientPlugin |
| 45 | + |
| 46 | +val ktorClient = HttpClient(Android) { |
| 47 | + install(SentryKtorClientPlugin) |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +## Verify |
| 52 | + |
| 53 | +This snippet includes an HTTP Request and captures an intentional message, so you can verify that everything is working as soon as you set it up: |
| 54 | + |
| 55 | +```kotlin |
| 56 | +import io.ktor.client.* |
| 57 | +import io.ktor.client.engine.android.* |
| 58 | +import io.ktor.client.request.* |
| 59 | +import io.ktor.client.statement.* |
| 60 | +import io.sentry.ktorClient.SentryKtorClientPlugin |
| 61 | +import io.sentry.Sentry |
| 62 | + |
| 63 | +suspend fun run(url: String): String? { |
| 64 | + val ktorClient = HttpClient(Android) { |
| 65 | + install(SentryKtorClientPlugin) |
| 66 | + } |
| 67 | + |
| 68 | + val response = ktorClient.get(url) |
| 69 | + val bodyStr = response.bodyAsText() |
| 70 | + |
| 71 | + Sentry.captureMessage("The Message $bodyStr") |
| 72 | + |
| 73 | + return bodyStr |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +<Alert> |
| 78 | + |
| 79 | + Learn more about manually capturing an error or message in our <PlatformLink to="/usage/">Usage documentation</PlatformLink>. |
| 80 | + |
| 81 | +</Alert> |
| 82 | + |
| 83 | +To view and resolve the recorded message, log into [sentry.io](https://sentry.io) and open your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved. |
| 84 | + |
| 85 | +## Customize the Recorded Span |
| 86 | + |
| 87 | +The captured span can be customized or dropped with a `BeforeSpanCallback`: |
| 88 | + |
| 89 | +```kotlin |
| 90 | +import io.ktor.client.* |
| 91 | +import io.ktor.client.engine.android.* |
| 92 | +import io.ktor.client.request.* |
| 93 | +import io.ktor.http.* |
| 94 | +import io.sentry.ISpan |
| 95 | +import io.sentry.ktorClient.SentryKtorClientPlugin |
| 96 | + |
| 97 | +val ktorClient = HttpClient(Android) { |
| 98 | + install(SentryKtorClientPlugin) { |
| 99 | + beforeSpan = { span, request -> |
| 100 | + // Drop spans for admin requests |
| 101 | + if (request.url.toString().contains("/admin")) { |
| 102 | + null |
| 103 | + } else { |
| 104 | + span |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## HTTP Client Errors |
| 112 | + |
| 113 | +This feature automatically captures HTTP client errors (like bad response codes) as error events and reports them to Sentry. The error event will contain the `request` and `response` data, including the `url`, `status_code`, and so on. |
| 114 | + |
| 115 | +HTTP client error capturing is enabled by default. The SDK captures HTTP client errors with a response code between `500` and `599` by default, but you can change this behavior by configuring the plugin: |
| 116 | + |
| 117 | +```kotlin |
| 118 | +import io.ktor.client.* |
| 119 | +import io.ktor.client.engine.android.* |
| 120 | +import io.sentry.HttpStatusCodeRange |
| 121 | +import io.sentry.ktorClient.SentryKtorClientPlugin |
| 122 | + |
| 123 | +val ktorClient = HttpClient(Android) { |
| 124 | + install(SentryKtorClientPlugin) { |
| 125 | + captureFailedRequests = true |
| 126 | + failedRequestStatusCodes = listOf(HttpStatusCodeRange(400, 599)) |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +HTTP client errors from every target (`.*` regular expression) are automatically captured, but you can change this behavior by setting the `failedRequestTargets` option with either a regular expression or a plain `String`. A plain string must contain at least one of the items from the list. Plain strings don’t need to match the entire URL — a match occurs as long as the URL contains the string provided in the option. |
| 132 | + |
| 133 | +```kotlin |
| 134 | +import io.ktor.client.* |
| 135 | +import io.ktor.client.engine.android.* |
| 136 | +import io.sentry.ktorClient.SentryKtorClientPlugin |
| 137 | + |
| 138 | +val ktorClient = HttpClient(Android) { |
| 139 | + install(SentryKtorClientPlugin) { |
| 140 | + captureFailedRequests = true |
| 141 | + failedRequestTargets = listOf("myapi.com") |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +By default, error events won't contain any PII data, such as `Headers` and `Cookies`, but you can change this behavior by setting the `sendDefaultPii` option to `true`: |
| 147 | + |
| 148 | +```xml {filename:AndroidManifest.xml} |
| 149 | +<application> |
| 150 | + <meta-data android:name="io.sentry.send-default-pii" android:value="true" /> |
| 151 | +</application> |
| 152 | +``` |
| 153 | + |
| 154 | +Those events are searchable and you can set alerts on them if you use the `http.url` and `http.status_code` properties. Learn more in our full [Searchable Properties](/concepts/search/searchable-properties/) documentation. |
| 155 | + |
| 156 | +### Customize or Drop the Error Event |
| 157 | + |
| 158 | +To customize or drop the error event, you need to do a [manual initialization](/platforms/android/configuration/manual-init/#manual-initialization) of the Sentry Android SDK. |
| 159 | + |
| 160 | +The captured error event can be customized or dropped with a `BeforeSendCallback`: |
| 161 | + |
| 162 | +```kotlin |
| 163 | +import io.sentry.android.core.SentryAndroid |
| 164 | +import io.sentry.SentryOptions.BeforeSendCallback |
| 165 | +import io.sentry.TypeCheckHint.KTOR_CLIENT_REQUEST |
| 166 | +import io.sentry.TypeCheckHint.KTOR_CLIENT_RESPONSE |
| 167 | +import io.ktor.client.request.* |
| 168 | +import io.ktor.client.statement.* |
| 169 | + |
| 170 | +SentryAndroid.init(this) { options -> |
| 171 | + // Add a callback that will be used before the event is sent to Sentry. |
| 172 | + // With this callback, you can modify the event or, when returning null, also discard the event. |
| 173 | + options.beforeSend = BeforeSendCallback { event, hint -> |
| 174 | + val request = hint.getAs(KTOR_CLIENT_REQUEST, HttpRequest::class.java) |
| 175 | + val response = hint.getAs(KTOR_CLIENT_RESPONSE, HttpResponse::class.java) |
| 176 | + |
| 177 | + // customize or drop the event |
| 178 | + event |
| 179 | + } |
| 180 | +} |
| 181 | +``` |
0 commit comments