Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
80 changes: 80 additions & 0 deletions sentry-okhttp3/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import com.novoda.gradle.release.PublishExtension

plugins {
`java-library`
kotlin("jvm")
jacoco
id(Config.QualityPlugins.errorProne)
id(Config.Deploy.novodaBintray)
id(Config.QualityPlugins.gradleVersions)
}

dependencies {
implementation("com.squareup.okhttp3:okhttp:3.14.7")
implementation(project(":sentry-core"))

compileOnly(Config.CompileOnly.nopen)
errorprone(Config.CompileOnly.nopenChecker)
errorprone(Config.CompileOnly.errorprone)
errorproneJavac(Config.CompileOnly.errorProneJavac8)
compileOnly(Config.CompileOnly.jetbrainsAnnotations)

// tests
testImplementation(kotlin(Config.kotlinStdLib))
testImplementation(Config.TestLibs.kotlinTestJunit)
testImplementation(Config.TestLibs.mockitoKotlin)
}

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

jacoco {
toolVersion = Config.QualityPlugins.jacocoVersion
}

tasks.jacocoTestReport {
reports {
xml.isEnabled = true
html.isEnabled = false
}
}

tasks {
jacocoTestCoverageVerification {
violationRules {
// TODO: Raise the minimum to a sensible value.
rule { limit { minimum = BigDecimal.valueOf(0.1) } }
}
}
check {
dependsOn(jacocoTestCoverageVerification)
dependsOn(jacocoTestReport)
}
}

//TODO: move thse blocks to parent gradle file, DRY
configure<PublishExtension> {
userOrg = Config.Sentry.userOrg
groupId = project.group.toString()
publishVersion = project.version.toString()
desc = Config.Sentry.description
website = Config.Sentry.website
repoName = Config.Sentry.repoName
setLicences(Config.Sentry.licence)
setLicenceUrls(Config.Sentry.licenceUrl)
issueTracker = Config.Sentry.issueTracker
repository = Config.Sentry.repository
sign = Config.Deploy.sign
mavenCentralSync = Config.Deploy.mavenCentralSync
artifactId = project.name
uploadName = "${project.group}:${project.name}"
devId = Config.Sentry.userOrg
devName = Config.Sentry.devName
devEmail = Config.Sentry.devEmail
scmConnection = Config.Sentry.scmConnection
scmDevConnection = Config.Sentry.scmDevConnection
scmUrl = Config.Sentry.scmUrl
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.sentry.okhttp3;

import io.sentry.core.Sentry;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

/** Sentry Interceptor for OkHttp v3 */
public final class SentryOkHttpInterceptor implements Interceptor {

@SuppressWarnings("DefaultLocale")
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
final Request request = chain.request();

final long t1 = System.nanoTime();
Sentry.addBreadcrumb(
String.format(
"Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers()));

Response response = chain.proceed(request);

final long t2 = System.nanoTime();
Sentry.addBreadcrumb(
String.format(
"Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));

return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.sentry.okhttp3

import com.nhaarman.mockitokotlin2.mock
import io.sentry.core.Breadcrumb
import io.sentry.core.Sentry
import io.sentry.core.SentryOptions
import java.io.File
import java.nio.file.Files
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response

class SentryOkHttpInterceptorTest {

private lateinit var file: File

@BeforeTest
fun `set up`() {
file = Files.createTempDirectory("sentry-disk-cache-test").toAbsolutePath().toFile()
}

@AfterTest
fun shutdown() {
Files.delete(file.toPath())
}

@Test
fun `when okhttp makes request, we log it`() {
var bcCounter = 0
Sentry.init {
it.cacheDirPath = file.absolutePath
it.beforeBreadcrumb = SentryOptions.BeforeBreadcrumbCallback {
breadcrumb: Breadcrumb, _: Any? ->
bcCounter++
breadcrumb
}
it.dsn = "https://key@sentry.io/proj"
it.setSerializer(mock())
}

// 1) breadcrumb
// Sending request http://www.publicobject.com/helloworld.txt on null
// User-Agent: OkHttp Example
//
// 2) breadcrumb
// Received response for https://publicobject.com/helloworld.txt in 28591.7ms
// Server: nginx/1.10.0 (Ubuntu)
// Date: Mon, 02 Mar 2020 17:42:12 GMT
// Content-Type: text/plain
// Content-Length: 1759
// Last-Modified: Tue, 27 May 2014 02:35:47 GMT
// Connection: keep-alive
// ETag: "5383fa03-6df"
// Accept-Ranges: bytes

// TODO: use https://github.com/square/okhttp/tree/master/mockwebserver so we can test on the JVM
// and test wont be flaky if theres no internet
val client = OkHttpClient.Builder()
.addInterceptor(SentryOkHttpInterceptor())
.build()
val request: Request = Request.Builder()
.url("http://www.publicobject.com/helloworld.txt")
.header("User-Agent", "OkHttp Example")
.build()

val response: Response = client.newCall(request).execute()
response.body()?.close()
assertEquals(2, bcCounter)
}
}
9 changes: 8 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
rootProject.name = "sentry"
rootProject.buildFileName = "build.gradle.kts"

include("sentry-android", "sentry-android-ndk", "sentry-android-core", "sentry-core", "sentry-sample", "sentry-native-sample")
include(
"sentry-android",
"sentry-android-ndk",
"sentry-android-core",
"sentry-core",
"sentry-sample",
"sentry-native-sample",
"sentry-okhttp3")