Skip to content

Commit d1fd194

Browse files
lciangetsentry-bot
andauthored
Add Ktor client integration (#4527)
Co-authored-by: Sentry Github Bot <[email protected]>
1 parent 70671a5 commit d1fd194

File tree

18 files changed

+1024
-0
lines changed

18 files changed

+1024
-0
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77
- Add `SentryUserFeedbackButton` Composable ([#4559](https://github.com/getsentry/sentry-java/pull/4559))
88
- Also added `Sentry.showUserFeedbackDialog` static method
99
- Add deadlineTimeout option ([#4555](https://github.com/getsentry/sentry-java/pull/4555))
10+
- Add Ktor client integration ([#4527](https://github.com/getsentry/sentry-java/pull/4527))
11+
- To use the integration, add a dependency on `io.sentry:sentry-ktor-client`, then install the `SentryKtorClientPlugin` on your `HttpClient`,
12+
e.g.:
13+
```kotlin
14+
val client =
15+
HttpClient(Java) {
16+
install(io.sentry.ktorClient.SentryKtorClientPlugin) {
17+
captureFailedRequests = true
18+
failedRequestTargets = listOf(".*")
19+
failedRequestStatusCodes = listOf(HttpStatusCodeRange(500, 599))
20+
}
21+
}
22+
```
1023

1124
### Fixes
1225

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Sentry SDK for Java and Android
3737
| 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 |
3838
| 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 |
3939
| 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 |
40+
| 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 |
4041
| 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) | |
4142
| 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) | |
4243
| 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) |

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ apiValidation {
6969
"sentry-samples-spring-boot-jakarta-opentelemetry-noagent",
7070
"sentry-samples-spring-boot-webflux",
7171
"sentry-samples-spring-boot-webflux-jakarta",
72+
"sentry-samples-ktor-client",
7273
"sentry-uitest-android",
7374
"sentry-uitest-android-benchmark",
7475
"sentry-uitest-android-critical",

buildSrc/src/main/java/Config.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ object Config {
7878
val SENTRY_OKHTTP_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.okhttp"
7979
val SENTRY_REACTOR_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.reactor"
8080
val SENTRY_KOTLIN_EXTENSIONS_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.kotlin-extensions"
81+
val SENTRY_KTOR_CLIENT_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.ktor-client"
8182
val group = "io.sentry"
8283
val description = "SDK for sentry.io"
8384
val versionNameProp = "versionName"

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jackson = "2.18.3"
1313
jetbrainsCompose = "1.6.11"
1414
kotlin = "1.9.24"
1515
kotlin-compatible-version = "1.6"
16+
ktorClient = "3.0.0"
1617
logback = "1.2.9"
1718
log4j2 = "2.20.0"
1819
nopen = "1.0.1"
@@ -95,6 +96,8 @@ jetbrains-annotations = { module = "org.jetbrains:annotations", version = "23.0.
9596
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
9697
kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
9798
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
99+
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktorClient" }
100+
ktor-client-java = { module = "io.ktor:ktor-client-java", version.ref = "ktorClient" }
98101
log4j-api = { module = "org.apache.logging.log4j:log4j-api", version.ref = "log4j2" }
99102
log4j-core = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j2" }
100103
leakcanary = { module = "com.squareup.leakcanary:leakcanary-android", version = "2.14" }

sentry-ktor-client/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# sentry-ktor-client
2+
3+
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.
4+
5+
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/).
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public final class io/sentry/ktorClient/BuildConfig {
2+
public static final field SENTRY_KTOR_CLIENT_SDK_NAME Ljava/lang/String;
3+
public static final field VERSION_NAME Ljava/lang/String;
4+
}
5+
6+
public final class io/sentry/ktorClient/SentryKtorClientPluginConfig {
7+
public fun <init> ()V
8+
public final fun getBeforeSpan ()Lio/sentry/ktorClient/SentryKtorClientPluginConfig$BeforeSpanCallback;
9+
public final fun getCaptureFailedRequests ()Z
10+
public final fun getFailedRequestStatusCodes ()Ljava/util/List;
11+
public final fun getFailedRequestTargets ()Ljava/util/List;
12+
public final fun getScopes ()Lio/sentry/IScopes;
13+
public final fun setBeforeSpan (Lio/sentry/ktorClient/SentryKtorClientPluginConfig$BeforeSpanCallback;)V
14+
public final fun setCaptureFailedRequests (Z)V
15+
public final fun setFailedRequestStatusCodes (Ljava/util/List;)V
16+
public final fun setFailedRequestTargets (Ljava/util/List;)V
17+
public final fun setScopes (Lio/sentry/IScopes;)V
18+
}
19+
20+
public abstract interface class io/sentry/ktorClient/SentryKtorClientPluginConfig$BeforeSpanCallback {
21+
public abstract fun execute (Lio/sentry/ISpan;Lio/ktor/client/request/HttpRequest;)Lio/sentry/ISpan;
22+
}
23+
24+
public class io/sentry/ktorClient/SentryKtorClientPluginContextHook : io/ktor/client/plugins/api/ClientHook {
25+
public fun <init> (Lio/sentry/IScopes;)V
26+
protected final fun getScopes ()Lio/sentry/IScopes;
27+
public synthetic fun install (Lio/ktor/client/HttpClient;Ljava/lang/Object;)V
28+
public fun install (Lio/ktor/client/HttpClient;Lkotlin/jvm/functions/Function2;)V
29+
}
30+
31+
public final class io/sentry/ktorClient/SentryKtorClientPluginKt {
32+
public static final fun getSentryKtorClientPlugin ()Lio/ktor/client/plugins/api/ClientPlugin;
33+
}
34+

sentry-ktor-client/build.gradle.kts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import net.ltgt.gradle.errorprone.errorprone
2+
import org.jetbrains.kotlin.config.KotlinCompilerVersion
3+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
4+
5+
plugins {
6+
`java-library`
7+
kotlin("jvm")
8+
jacoco
9+
id("io.sentry.javadoc")
10+
alias(libs.plugins.errorprone)
11+
alias(libs.plugins.gradle.versions)
12+
alias(libs.plugins.buildconfig)
13+
}
14+
15+
tasks.withType<KotlinCompile>().configureEach {
16+
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
17+
}
18+
19+
kotlin { explicitApi() }
20+
21+
dependencies {
22+
api(projects.sentry)
23+
24+
implementation(kotlin(Config.kotlinStdLib, KotlinCompilerVersion.VERSION))
25+
api(projects.sentryKotlinExtensions)
26+
27+
compileOnly(libs.jetbrains.annotations)
28+
compileOnly(libs.nopen.annotations)
29+
compileOnly(libs.ktor.client.core)
30+
errorprone(libs.errorprone.core)
31+
errorprone(libs.nopen.checker)
32+
errorprone(libs.nullaway)
33+
34+
testImplementation(projects.sentryTestSupport)
35+
testImplementation(libs.kotlin.test.junit)
36+
testImplementation(libs.mockito.kotlin)
37+
testImplementation(libs.mockito.inline)
38+
testImplementation(libs.ktor.client.core)
39+
testImplementation(libs.ktor.client.java)
40+
testImplementation(libs.okhttp.mockwebserver)
41+
}
42+
43+
configure<SourceSetContainer> { test { java.srcDir("src/test/java") } }
44+
45+
jacoco { toolVersion = libs.versions.jacoco.get() }
46+
47+
tasks.jacocoTestReport {
48+
reports {
49+
xml.required.set(true)
50+
html.required.set(false)
51+
}
52+
}
53+
54+
tasks {
55+
jacocoTestCoverageVerification {
56+
violationRules { rule { limit { minimum = Config.QualityPlugins.Jacoco.minimumCoverage } } }
57+
}
58+
check {
59+
dependsOn(jacocoTestCoverageVerification)
60+
dependsOn(jacocoTestReport)
61+
}
62+
}
63+
64+
buildConfig {
65+
useJavaOutput()
66+
packageName("io.sentry.ktorClient")
67+
buildConfigField(
68+
"String",
69+
"SENTRY_KTOR_CLIENT_SDK_NAME",
70+
"\"${Config.Sentry.SENTRY_KTOR_CLIENT_SDK_NAME}\"",
71+
)
72+
buildConfigField("String", "VERSION_NAME", "\"${project.version}\"")
73+
}
74+
75+
tasks.withType<JavaCompile>().configureEach {
76+
dependsOn(tasks.generateBuildConfig)
77+
options.errorprone {
78+
check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR)
79+
option("NullAway:AnnotatedPackages", "io.sentry")
80+
}
81+
}
82+
83+
tasks.jar {
84+
manifest {
85+
attributes(
86+
"Sentry-Version-Name" to project.version,
87+
"Sentry-SDK-Name" to Config.Sentry.SENTRY_KTOR_CLIENT_SDK_NAME,
88+
"Sentry-SDK-Package-Name" to "maven:io.sentry:sentry-ktor-client",
89+
"Implementation-Vendor" to "Sentry",
90+
"Implementation-Title" to project.name,
91+
"Implementation-Version" to project.version,
92+
)
93+
}
94+
}

0 commit comments

Comments
 (0)