-
Notifications
You must be signed in to change notification settings - Fork 4
Add OkHttp integration #7
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # OkHttp integration | ||
|
|
||
| ## How to setup | ||
|
|
||
| Add `M3TracingInterceptor` as interceptor of OkHttpClient. | ||
|
|
||
| ### Kotlin | ||
|
|
||
| ```kotlin | ||
| OkHttpClient.Builder().addInterceptor(M3TracingInterceptor(tracer)).build() | ||
| ``` | ||
|
|
||
| ### Java | ||
|
|
||
| ```java | ||
| new OkHttpClient.Builder().addInterceptor(new M3TracingInterceptor(tracer)).build() | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| plugins { | ||
| kotlin("jvm") | ||
| } | ||
|
|
||
| dependencies { | ||
| api(project(":core")) | ||
|
|
||
| implementation("com.squareup.okhttp3:okhttp:${project.extra["okHttpClientVersion"]}") | ||
| } |
49 changes: 49 additions & 0 deletions
49
jvm/okhttp/src/main/kotlin/com/m3/tracing/okhttp/M3TracingInterceptor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.m3.tracing.okhttp | ||
|
|
||
| import com.m3.tracing.M3Tracer | ||
| import com.m3.tracing.M3TracerFactory | ||
| import com.m3.tracing.http.HttpRequestMetadataKey | ||
| import okhttp3.Interceptor | ||
| import okhttp3.Response | ||
| import org.slf4j.LoggerFactory | ||
|
|
||
| /** | ||
| * Interceptor for OkHttp. | ||
| */ | ||
| open class M3TracingInterceptor( | ||
| private val tracer: M3Tracer | ||
| ) : Interceptor { | ||
| companion object { | ||
| private val logger = LoggerFactory.getLogger(M3TracingInterceptor::class.java) | ||
| } | ||
|
|
||
| constructor() : this(M3TracerFactory.get()) | ||
|
|
||
| override fun intercept(chain: Interceptor.Chain): Response { | ||
| val requestInfo = OkHttpMutableHttpRequestInfo(chain.request()) | ||
|
|
||
| return tracer.processOutgoingHttpRequest(requestInfo).use { span -> | ||
| doQuietly { | ||
| span["client"] = "m3-tracing:okhttp" | ||
| span["method"] = requestInfo.tryGetMetadata(HttpRequestMetadataKey.Method) | ||
| span["path"] = requestInfo.tryGetMetadata(HttpRequestMetadataKey.Path) | ||
| } | ||
|
|
||
| val response = chain.proceed(requestInfo.build()) | ||
|
|
||
| doQuietly { | ||
| span["status"] = response.code | ||
| } | ||
|
|
||
| response | ||
| } | ||
| } | ||
|
|
||
| private fun doQuietly(action: () -> Unit) { | ||
| try { | ||
| action() | ||
| } catch (e: Throwable) { | ||
| logger.error("Failed to update Span.", e) | ||
| } | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
jvm/okhttp/src/main/kotlin/com/m3/tracing/okhttp/OkHttpMutableHttpRequestInfo.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.m3.tracing.okhttp | ||
|
|
||
| import com.m3.tracing.http.HttpRequestInfo | ||
| import com.m3.tracing.http.HttpRequestMetadataKey | ||
| import okhttp3.Request | ||
|
|
||
| open class OkHttpMutableHttpRequestInfo( | ||
| private val baseReq: Request | ||
| ) : HttpRequestInfo { | ||
| private val builder: Request.Builder = baseReq.newBuilder() | ||
|
|
||
| override fun tryGetHeader(name: String): String? = baseReq.header(name) | ||
| override fun trySetHeader(name: String, value: String) { | ||
| builder.addHeader(name, value) | ||
| } | ||
|
|
||
| @Suppress("UNCHECKED_CAST", "IMPLICIT_ANY") | ||
| override fun <T> tryGetMetadata(key: HttpRequestMetadataKey<T>): T? = when (key) { | ||
| HttpRequestMetadataKey.Method -> baseReq.method as T? | ||
| HttpRequestMetadataKey.Path -> baseReq.url.encodedPath as T? | ||
| HttpRequestMetadataKey.Host -> baseReq.url.host as T? | ||
| HttpRequestMetadataKey.Url -> baseReq.url.toString() as T? | ||
|
|
||
| else -> null | ||
| } | ||
|
|
||
| fun build(): Request = builder.build() | ||
| } |
37 changes: 37 additions & 0 deletions
37
jvm/okhttp/src/test/kotlin/com/m3/tracing/okhttp/OkHttpMutableHttpRequestInfoTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.m3.tracing.okhttp | ||
|
|
||
| import com.google.common.truth.Truth | ||
| import com.m3.tracing.http.HttpRequestMetadataKey | ||
| import okhttp3.Request | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| class OkHttpMutableHttpRequestInfoTest { | ||
| @Test | ||
| fun `each attribute is set properly for Request`() { | ||
| val request = Request.Builder() | ||
| .get() | ||
| .url("http://example.com/foo/bar.html") | ||
| .build() | ||
|
|
||
| val req = OkHttpMutableHttpRequestInfo(request) | ||
|
|
||
| Truth.assertThat(req.tryGetMetadata(HttpRequestMetadataKey.Host)).isEqualTo("example.com") | ||
| Truth.assertThat(req.tryGetMetadata(HttpRequestMetadataKey.Method)).isEqualTo("GET") | ||
| Truth.assertThat(req.tryGetMetadata(HttpRequestMetadataKey.Path)).isEqualTo("/foo/bar.html") | ||
| Truth.assertThat(req.tryGetMetadata(HttpRequestMetadataKey.Url)).isEqualTo("http://example.com/foo/bar.html") | ||
| } | ||
|
|
||
| @Test | ||
| fun `header is set properly to Request`() { | ||
| val request = Request.Builder() | ||
| .get() | ||
| .url("http://example.com/foo/bar.html") | ||
| .build() | ||
|
|
||
| val req = OkHttpMutableHttpRequestInfo(request) | ||
|
|
||
| req.trySetHeader("hoge", "fuga") | ||
|
|
||
| Truth.assertThat(req.build().header("hoge")).isEqualTo("fuga") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| handlers=org.slf4j.bridge.SLF4JBridgeHandler | ||
| .level=INFO |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| include( | ||
| "apache-httpclient", | ||
| "okhttp", | ||
| "core", | ||
| "jdbc-p6spy", | ||
| "opencensus", | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.