Skip to content

Add Ktor client integration #4527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 37 commits into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
5645889
Add Ktor client integration
lcian Jun 27, 2025
93b4a83
tracing
lcian Jun 27, 2025
f9538d5
improve
lcian Jun 27, 2025
26372fe
fix scopes and stuff
lcian Jun 30, 2025
c01a756
Format code
getsentry-bot Jun 30, 2025
ecaf243
improve
lcian Jun 30, 2025
61e714e
work
lcian Jul 1, 2025
82c54aa
improve
lcian Jul 2, 2025
ad4abc9
improve
lcian Jul 2, 2025
eb3db76
improve
lcian Jul 2, 2025
7d8dcd1
improve
lcian Jul 2, 2025
0c58628
Merge branch 'main' into lcian/feat/ktor-client
lcian Jul 3, 2025
80fb52f
changelog
lcian Jul 3, 2025
665b6ec
improve
lcian Jul 3, 2025
d0a408b
improve
lcian Jul 3, 2025
0765227
improve
lcian Jul 3, 2025
26a7e07
Merge branch 'main' into lcian/feat/ktor-client
lcian Jul 3, 2025
236ea9e
Merge branch 'main' into lcian/feat/ktor-client
lcian Jul 7, 2025
b6aeec5
changelog
lcian Jul 7, 2025
8b75851
address feedback from code review
lcian Jul 15, 2025
8816597
Merge branch 'main' into lcian/feat/ktor-client
lcian Jul 15, 2025
5b439dc
improve
lcian Jul 15, 2025
8499b5d
improve
lcian Jul 15, 2025
e9a5b48
improve
lcian Jul 15, 2025
d050c7b
improve
lcian Jul 15, 2025
998f52c
improve
lcian Jul 15, 2025
2aae940
address Seer
lcian Jul 17, 2025
ae6ec68
revert non-working OkHttp detection, and finish addressing Seer
lcian Jul 17, 2025
8a42315
okhttp detection
lcian Jul 17, 2025
b0f5803
improve
lcian Jul 17, 2025
38220c9
remove okhttp instrumentation detection
lcian Jul 24, 2025
e8671f2
Merge branch 'main' into lcian/feat/ktor-client
lcian Jul 24, 2025
f3044e1
improve
lcian Jul 24, 2025
5382d70
improve
lcian Jul 24, 2025
3c650dd
name
lcian Jul 25, 2025
c0027c2
readme
lcian Jul 25, 2025
8763d70
change scopes
lcian Jul 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
- Add `SentryUserFeedbackButton` Composable ([#4559](https://github.com/getsentry/sentry-java/pull/4559))
- Also added `Sentry.showUserFeedbackDialog` static method
- Add deadlineTimeout option ([#4555](https://github.com/getsentry/sentry-java/pull/4555))
- Add Ktor client integration ([#4527](https://github.com/getsentry/sentry-java/pull/4527))
- To use the integration, add a dependency on `io.sentry:sentry-ktor-client`, then install the `SentryKtorClientPlugin` on your `HttpClient`,
e.g.:
```kotlin
val client =
HttpClient(Java) {
install(io.sentry.ktorClient.SentryKtorClientPlugin) {
captureFailedRequests = true
failedRequestTargets = listOf(".*")
failedRequestStatusCodes = listOf(HttpStatusCodeRange(500, 599))
}
}
```

### Fixes

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Sentry SDK for Java and Android
| sentry-apollo-3 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-apollo-3/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-apollo-3) | 21 |
| sentry-apollo-4 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-apollo-4/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-apollo-4) | 21 |
| sentry-kotlin-extensions | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-kotlin-extensions/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-kotlin-extensions) | 21 |
| sentry-ktor-client | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-ktor-client/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-ktor-client) | 21 |
| sentry-servlet | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-servlet/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-servlet) | |
| sentry-servlet-jakarta | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-servlet-jakarta/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-servlet-jakarta) | |
| sentry-spring-boot | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-spring-boot/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.sentry/sentry-spring-boot) |
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ apiValidation {
"sentry-samples-spring-boot-jakarta-opentelemetry-noagent",
"sentry-samples-spring-boot-webflux",
"sentry-samples-spring-boot-webflux-jakarta",
"sentry-samples-ktor-client",
"sentry-uitest-android",
"sentry-uitest-android-benchmark",
"sentry-uitest-android-critical",
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/java/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ object Config {
val SENTRY_OKHTTP_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.okhttp"
val SENTRY_REACTOR_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.reactor"
val SENTRY_KOTLIN_EXTENSIONS_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.kotlin-extensions"
val SENTRY_KTOR_CLIENT_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.ktor-client"
val group = "io.sentry"
val description = "SDK for sentry.io"
val versionNameProp = "versionName"
Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jackson = "2.18.3"
jetbrainsCompose = "1.6.11"
kotlin = "1.9.24"
kotlin-compatible-version = "1.6"
ktorClient = "3.0.0"
logback = "1.2.9"
log4j2 = "2.20.0"
nopen = "1.0.1"
Expand Down Expand Up @@ -95,6 +96,8 @@ jetbrains-annotations = { module = "org.jetbrains:annotations", version = "23.0.
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktorClient" }
ktor-client-java = { module = "io.ktor:ktor-client-java", version.ref = "ktorClient" }
log4j-api = { module = "org.apache.logging.log4j:log4j-api", version.ref = "log4j2" }
log4j-core = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j2" }
leakcanary = { module = "com.squareup.leakcanary:leakcanary-android", version = "2.14" }
Expand Down
5 changes: 5 additions & 0 deletions sentry-ktor-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# sentry-ktor-client

This module provides a plugin for automatic instrumentation of [Ktor HTTP Client](https://ktor.io/docs/getting-started-ktor-client.html), including error capturing, request/response breadcrumbs, and distributed tracing.

Please consult the documentation on how to install and use this integration in the Sentry Docs for [Android](https://docs.sentry.io/platforms/android/integrations/ktor-client/) or [Java](https://docs.sentry.io/platforms/java/tracing/instrumentation/ktor-client/).
34 changes: 34 additions & 0 deletions sentry-ktor-client/api/sentry-ktor-client.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public final class io/sentry/ktorClient/BuildConfig {
public static final field SENTRY_KTOR_CLIENT_SDK_NAME Ljava/lang/String;
public static final field VERSION_NAME Ljava/lang/String;
}

public final class io/sentry/ktorClient/SentryKtorClientPluginConfig {
public fun <init> ()V
public final fun getBeforeSpan ()Lio/sentry/ktorClient/SentryKtorClientPluginConfig$BeforeSpanCallback;
public final fun getCaptureFailedRequests ()Z
public final fun getFailedRequestStatusCodes ()Ljava/util/List;
public final fun getFailedRequestTargets ()Ljava/util/List;
public final fun getScopes ()Lio/sentry/IScopes;
public final fun setBeforeSpan (Lio/sentry/ktorClient/SentryKtorClientPluginConfig$BeforeSpanCallback;)V
public final fun setCaptureFailedRequests (Z)V
public final fun setFailedRequestStatusCodes (Ljava/util/List;)V
public final fun setFailedRequestTargets (Ljava/util/List;)V
public final fun setScopes (Lio/sentry/IScopes;)V
}

public abstract interface class io/sentry/ktorClient/SentryKtorClientPluginConfig$BeforeSpanCallback {
public abstract fun execute (Lio/sentry/ISpan;Lio/ktor/client/request/HttpRequest;)Lio/sentry/ISpan;
}

public class io/sentry/ktorClient/SentryKtorClientPluginContextHook : io/ktor/client/plugins/api/ClientHook {
public fun <init> (Lio/sentry/IScopes;)V
protected final fun getScopes ()Lio/sentry/IScopes;
public synthetic fun install (Lio/ktor/client/HttpClient;Ljava/lang/Object;)V
public fun install (Lio/ktor/client/HttpClient;Lkotlin/jvm/functions/Function2;)V
}

public final class io/sentry/ktorClient/SentryKtorClientPluginKt {
public static final fun getSentryKtorClientPlugin ()Lio/ktor/client/plugins/api/ClientPlugin;
}

94 changes: 94 additions & 0 deletions sentry-ktor-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import net.ltgt.gradle.errorprone.errorprone
import org.jetbrains.kotlin.config.KotlinCompilerVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
`java-library`
kotlin("jvm")
jacoco
id("io.sentry.javadoc")
alias(libs.plugins.errorprone)
alias(libs.plugins.gradle.versions)
alias(libs.plugins.buildconfig)
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
}

kotlin { explicitApi() }

dependencies {
api(projects.sentry)

implementation(kotlin(Config.kotlinStdLib, KotlinCompilerVersion.VERSION))
api(projects.sentryKotlinExtensions)

compileOnly(libs.jetbrains.annotations)
compileOnly(libs.nopen.annotations)
compileOnly(libs.ktor.client.core)
errorprone(libs.errorprone.core)
errorprone(libs.nopen.checker)
errorprone(libs.nullaway)

testImplementation(projects.sentryTestSupport)
testImplementation(libs.kotlin.test.junit)
testImplementation(libs.mockito.kotlin)
testImplementation(libs.mockito.inline)
testImplementation(libs.ktor.client.core)
testImplementation(libs.ktor.client.java)
testImplementation(libs.okhttp.mockwebserver)
}

configure<SourceSetContainer> { test { java.srcDir("src/test/java") } }

jacoco { toolVersion = libs.versions.jacoco.get() }

tasks.jacocoTestReport {
reports {
xml.required.set(true)
html.required.set(false)
}
}

tasks {
jacocoTestCoverageVerification {
violationRules { rule { limit { minimum = Config.QualityPlugins.Jacoco.minimumCoverage } } }
}
check {
dependsOn(jacocoTestCoverageVerification)
dependsOn(jacocoTestReport)
}
}

buildConfig {
useJavaOutput()
packageName("io.sentry.ktorClient")
buildConfigField(
"String",
"SENTRY_KTOR_CLIENT_SDK_NAME",
"\"${Config.Sentry.SENTRY_KTOR_CLIENT_SDK_NAME}\"",
)
buildConfigField("String", "VERSION_NAME", "\"${project.version}\"")
}

tasks.withType<JavaCompile>().configureEach {
dependsOn(tasks.generateBuildConfig)
options.errorprone {
check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR)
option("NullAway:AnnotatedPackages", "io.sentry")
}
}

tasks.jar {
manifest {
attributes(
"Sentry-Version-Name" to project.version,
"Sentry-SDK-Name" to Config.Sentry.SENTRY_KTOR_CLIENT_SDK_NAME,
"Sentry-SDK-Package-Name" to "maven:io.sentry:sentry-ktor-client",
"Implementation-Vendor" to "Sentry",
"Implementation-Title" to project.name,
"Implementation-Version" to project.version,
)
}
}
Loading
Loading