|
| 1 | +--- |
| 2 | +title: Ktor Client Integration |
| 3 | +sidebar_order: 45 |
| 4 | +sdk: sentry.java.ktor-client |
| 5 | +description: "Learn how to capture tracing information of the Ktor Client." |
| 6 | +notSupported: |
| 7 | + - java.logback |
| 8 | + - java.log4j2 |
| 9 | + - java.jul |
| 10 | +--- |
| 11 | + |
| 12 | +<Alert> |
| 13 | + |
| 14 | +To be able to capture transactions, you'll need to first <PlatformLink to="/tracing/">set up tracing</PlatformLink>. |
| 15 | + |
| 16 | +</Alert> |
| 17 | + |
| 18 | +The `sentry-ktor-client` library provides [Ktor Client](https://ktor.io/) support for Sentry via the Sentry Ktor Client Plugin. The source can be found [on GitHub](https://github.com/getsentry/sentry-java/tree/main/sentry-ktor-client). |
| 19 | + |
| 20 | +On this page, we get you up and running with Sentry's Ktor Client Integration. |
| 21 | +The integration supports: |
| 22 | +- Adding breadcrumbs for each HTTP request |
| 23 | +- 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) |
| 24 | +- Capturing certain request failures as errors in Sentry. |
| 25 | + |
| 26 | +The Sentry Ktor Client integration supports Ktor Client version `3.x`. |
| 27 | + |
| 28 | +<Alert level="warning"> |
| 29 | + If you're using Ktor Client with OKHttp as the engine, and you're already using the [Sentry OkHttp integration](/platforms/java/tracing/instrumentation/okhttp/), |
| 30 | + you shouldn't use this integration, otherwise the SDK will produce duplicate data, instrumenting each HTTP request twice. |
| 31 | +</Alert> |
| 32 | + |
| 33 | +## Install |
| 34 | + |
| 35 | +To install the integration: |
| 36 | + |
| 37 | +```groovy {tabTitle:Gradle} |
| 38 | +implementation 'io.sentry:sentry-ktor-client:{{@inject packages.version('sentry.java.ktor-client', '8.18.0') }}' |
| 39 | +``` |
| 40 | + |
| 41 | +## Configure |
| 42 | + |
| 43 | +Create the Ktor HTTP Client with your preferred engine, and install the Sentry Ktor Client Plugin: |
| 44 | + |
| 45 | +```kotlin |
| 46 | +import io.ktor.client.* |
| 47 | +import io.ktor.client.engine.java.* |
| 48 | +import io.sentry.ktorClient.SentryKtorClientPlugin |
| 49 | + |
| 50 | +val ktorClient = HttpClient(Java) { |
| 51 | + install(SentryKtorClientPlugin) |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## Verify |
| 56 | + |
| 57 | +This snippet includes a HTTP Request and captures an intentional message, so you can test that everything is working as soon as you set it up: |
| 58 | + |
| 59 | +```kotlin |
| 60 | +import io.ktor.client.* |
| 61 | +import io.ktor.client.engine.java.* |
| 62 | +import io.ktor.client.request.* |
| 63 | +import io.ktor.client.statement.* |
| 64 | +import io.sentry.ktorClient.SentryKtorClientPlugin |
| 65 | +import io.sentry.Sentry |
| 66 | + |
| 67 | +suspend fun run(url: String): String? { |
| 68 | + val ktorClient = HttpClient(Java) { |
| 69 | + install(SentryKtorClientPlugin) |
| 70 | + } |
| 71 | + |
| 72 | + val response = ktorClient.get(url) |
| 73 | + val bodyStr = response.bodyAsText() |
| 74 | + |
| 75 | + Sentry.captureMessage("The Message $bodyStr") |
| 76 | + |
| 77 | + return bodyStr |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +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. |
| 82 | + |
| 83 | +## Customize the Recorded Span |
| 84 | + |
| 85 | +The captured span can be customized or dropped with a `BeforeSpanCallback`: |
| 86 | + |
| 87 | +```kotlin |
| 88 | +import io.ktor.client.* |
| 89 | +import io.ktor.client.engine.java.* |
| 90 | +import io.ktor.client.request.* |
| 91 | +import io.ktor.http.* |
| 92 | +import io.sentry.ISpan |
| 93 | +import io.sentry.ktorClient.SentryKtorClientPlugin |
| 94 | + |
| 95 | +val ktorClient = HttpClient(Java) { |
| 96 | + install(SentryKtorClientPlugin) { |
| 97 | + beforeSpan = { span, request -> |
| 98 | + // Drop spans for admin requests |
| 99 | + if (request.url.toString().contains("/admin")) { |
| 100 | + null |
| 101 | + } else { |
| 102 | + span |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## HTTP Client Errors |
| 110 | + |
| 111 | +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. |
| 112 | + |
| 113 | +HTTP client error capturing is enabled by default. The SDK captures HTTP client errors with a response code between `500` and `599`, but you can change this behavior by configuring the plugin: |
| 114 | + |
| 115 | +```kotlin |
| 116 | +import io.ktor.client.* |
| 117 | +import io.ktor.client.engine.java.* |
| 118 | +import io.sentry.HttpStatusCodeRange |
| 119 | +import io.sentry.ktorClient.SentryKtorClientPlugin |
| 120 | + |
| 121 | +val ktorClient = HttpClient(Java) { |
| 122 | + install(SentryKtorClientPlugin) { |
| 123 | + captureFailedRequests = true |
| 124 | + failedRequestStatusCodes = listOf(HttpStatusCodeRange(400, 599)) |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +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 have to be full matches, meaning the URL of a request is matched when it contains a string provided through the option. |
| 130 | + |
| 131 | +```kotlin |
| 132 | +import io.ktor.client.* |
| 133 | +import io.ktor.client.engine.java.* |
| 134 | +import io.sentry.ktorClient.SentryKtorClientPlugin |
| 135 | + |
| 136 | +val ktorClient = HttpClient(Java) { |
| 137 | + install(SentryKtorClientPlugin) { |
| 138 | + captureFailedRequests = true |
| 139 | + failedRequestTargets = listOf("myapi.com") |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +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`: |
| 145 | + |
| 146 | +```kotlin |
| 147 | +Sentry.init { options -> |
| 148 | + options.isSendDefaultPii = true |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +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. |
| 153 | + |
| 154 | +### Customize or Drop the Error Event |
| 155 | + |
| 156 | +The captured error event can be customized or dropped with a `BeforeSendCallback`: |
| 157 | + |
| 158 | +```kotlin |
| 159 | +import io.sentry.Sentry |
| 160 | +import io.sentry.SentryOptions.BeforeSendCallback |
| 161 | +import io.sentry.TypeCheckHint.KTOR_CLIENT_REQUEST |
| 162 | +import io.sentry.TypeCheckHint.KTOR_CLIENT_RESPONSE |
| 163 | +import io.ktor.client.request.* |
| 164 | +import io.ktor.client.statement.* |
| 165 | + |
| 166 | +Sentry.init { options -> |
| 167 | + // Add a callback that will be used before the event is sent to Sentry. |
| 168 | + // With this callback, you can modify the event or, when returning null, also discard the event. |
| 169 | + options.beforeSend = BeforeSendCallback { event, hint -> |
| 170 | + val request = hint.getAs(KTOR_CLIENT_REQUEST, HttpRequest::class.java) |
| 171 | + val response = hint.getAs(KTOR_CLIENT_RESPONSE, HttpResponse::class.java) |
| 172 | + |
| 173 | + // customize or drop the event |
| 174 | + event |
| 175 | + } |
| 176 | +} |
| 177 | +``` |
0 commit comments