Skip to content
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

Add tracing implementations of listeners #7845

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[versions]
androidx-tracing = "1.1.0"
biz-aQute-bnd = "6.4.0"
checkStyle = "10.11.0"
com-squareup-moshi = "1.15.0"
Expand All @@ -18,6 +19,7 @@ androidx-annotation = "androidx.annotation:annotation:1.6.0"
androidx-espresso-core = "androidx.test.espresso:espresso-core:3.5.1"
androidx-junit = "androidx.test.ext:junit:1.1.5"
androidx-test-runner = "androidx.test:runner:1.5.2"
androidx-tracing-ktx = { module = "androidx.tracing:tracing-ktx", version.ref = "androidx-tracing" }
animalsniffer-annotations = "org.codehaus.mojo:animal-sniffer-annotations:1.23"
aqute-resolve = { module = "biz.aQute.bnd:biz.aQute.resolve", version.ref = "biz-aQute-bnd" }
assertj-core = "org.assertj:assertj-core:3.24.2"
Expand Down
5 changes: 1 addition & 4 deletions okhttp-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import com.vanniktech.maven.publish.JavadocJar

plugins {
id("com.android.library")
kotlin("android")
id("de.mannodermaus.android-junit5")
id("org.jetbrains.dokka")
id("com.vanniktech.maven.publish.base")
id("binary-compatibility-validator")
Expand All @@ -14,7 +11,6 @@ android {

defaultConfig {
minSdk = 21
targetSdk = 31

// Make sure to use the AndroidJUnitRunner (or a sub-class) in order to hook in the JUnit 5 Test Builder
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -47,6 +43,7 @@ dependencies {
debugImplementation(libs.findbugs.jsr305)
compileOnly(libs.animalsniffer.annotations)
compileOnly(libs.robolectric.android)
implementation(libs.androidx.tracing.ktx)

testImplementation(projects.okhttpTestingSupport)
testImplementation(projects.mockwebserver3Junit5)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2022 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.android;

import androidx.tracing.Trace
import java.util.concurrent.atomic.AtomicInteger
import okhttp3.Call
import okhttp3.Connection
import okhttp3.ConnectionListener
import okhttp3.Route
import okio.IOException

/**
* Tracing implementation of ConnectionListener that marks the lifetime of each connection
* in perfetto traces.
*/
class TracingConnectionListener : ConnectionListener() {
private val idGenerator = AtomicInteger(0)

override fun connectStart(route: Route, call: Call) {
Trace.beginAsyncSection(route.tracingTag, idGenerator.incrementAndGet())
}

override fun connectFailed(route: Route, call: Call, failure: IOException) {
Trace.endAsyncSection(route.tracingTag, idGenerator.incrementAndGet())
}

override fun connectionClosed(connection: Connection) {
Trace.endAsyncSection(connection.route().tracingTag, idGenerator.incrementAndGet())
}

val Route.tracingTag: String
get() = this.address.url.host
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package okhttp3.android;

import androidx.tracing.trace
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response

/**
* Tracing implementation of Interceptor that marks each Call in a perfetto
* trace.
*/
class TracingInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
return trace(chain.request().tracingTag) {
chain.proceed(chain.request())
}
}

val Request.tracingTag: String
get() = url.encodedPath.take(127)
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,6 @@ class LoggingEventListener private constructor(
) : EventListener.Factory {
override fun create(call: Call): EventListener = LoggingEventListener(logger)
}

companion object
}