Skip to content

Commit f288643

Browse files
authored
Merge pull request #7 from shunyy/feature/okhttp
Add OkHttp integration
2 parents d83865e + 62748ef commit f288643

File tree

9 files changed

+154
-3
lines changed

9 files changed

+154
-3
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44
## Unreleased
55
-->
66

7+
## Release notes - 1.0.4 (2020-08-07)
8+
9+
## Added
10+
11+
- OkHttp integration
12+
13+
714
## Release notes - 1.0.3 (2020-08-04)
815

916
### Breaking Change
1017

11-
- apache-httpclient: to refer an instance of `M3TracingHttpInterceptor`, use the `INSTANCE` static field instaed of `getInstance` static method
18+
- apache-httpclient: to refer an instance of `M3TracingHttpInterceptor`, use the `INSTANCE` static field instaed of `getInstance` static method
1219

1320
### Added
1421

@@ -55,4 +62,4 @@ First release
5562
## Fixed
5663
5764
## Security
58-
-->
65+
-->

jvm/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212

1313
allprojects {
1414
group = "com.m3.tracing"
15-
version = "1.0.3"
15+
version = "1.0.4"
1616

1717
repositories {
1818
jcenter()
@@ -52,6 +52,7 @@ subprojects {
5252
val springBootVersion by extra { "2.1.4.RELEASE" }
5353
val springVersion by extra { "5.1.6.RELEASE" }
5454
val apacheHttpClientVersion by extra { "4.5.8" }
55+
val okHttpClientVersion by extra { "4.8.0" }
5556

5657
tasks.withType<Test> {
5758
useJUnitPlatform()

jvm/okhttp/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# OkHttp integration
2+
3+
## How to setup
4+
5+
Add `M3TracingInterceptor` as interceptor of OkHttpClient.
6+
7+
### Kotlin
8+
9+
```kotlin
10+
OkHttpClient.Builder().addInterceptor(M3TracingInterceptor(tracer)).build()
11+
```
12+
13+
### Java
14+
15+
```java
16+
new OkHttpClient.Builder().addInterceptor(new M3TracingInterceptor(tracer)).build()
17+
```

jvm/okhttp/build.gradle.kts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
plugins {
2+
kotlin("jvm")
3+
}
4+
5+
dependencies {
6+
api(project(":core"))
7+
8+
implementation("com.squareup.okhttp3:okhttp:${project.extra["okHttpClientVersion"]}")
9+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.m3.tracing.okhttp
2+
3+
import com.m3.tracing.M3Tracer
4+
import com.m3.tracing.M3TracerFactory
5+
import com.m3.tracing.http.HttpRequestMetadataKey
6+
import okhttp3.Interceptor
7+
import okhttp3.Response
8+
import org.slf4j.LoggerFactory
9+
10+
/**
11+
* Interceptor for OkHttp.
12+
*/
13+
open class M3TracingInterceptor(
14+
private val tracer: M3Tracer
15+
) : Interceptor {
16+
companion object {
17+
private val logger = LoggerFactory.getLogger(M3TracingInterceptor::class.java)
18+
}
19+
20+
constructor() : this(M3TracerFactory.get())
21+
22+
override fun intercept(chain: Interceptor.Chain): Response {
23+
val requestInfo = OkHttpMutableHttpRequestInfo(chain.request())
24+
25+
return tracer.processOutgoingHttpRequest(requestInfo).use { span ->
26+
doQuietly {
27+
span["client"] = "m3-tracing:okhttp"
28+
span["method"] = requestInfo.tryGetMetadata(HttpRequestMetadataKey.Method)
29+
span["path"] = requestInfo.tryGetMetadata(HttpRequestMetadataKey.Path)
30+
}
31+
32+
val response = chain.proceed(requestInfo.build())
33+
34+
doQuietly {
35+
span["status"] = response.code
36+
}
37+
38+
response
39+
}
40+
}
41+
42+
private fun doQuietly(action: () -> Unit) {
43+
try {
44+
action()
45+
} catch (e: Throwable) {
46+
logger.error("Failed to update Span.", e)
47+
}
48+
}
49+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.m3.tracing.okhttp
2+
3+
import com.m3.tracing.http.HttpRequestInfo
4+
import com.m3.tracing.http.HttpRequestMetadataKey
5+
import okhttp3.Request
6+
7+
open class OkHttpMutableHttpRequestInfo(
8+
private val baseReq: Request
9+
) : HttpRequestInfo {
10+
private val builder: Request.Builder = baseReq.newBuilder()
11+
12+
override fun tryGetHeader(name: String): String? = baseReq.header(name)
13+
override fun trySetHeader(name: String, value: String) {
14+
builder.addHeader(name, value)
15+
}
16+
17+
@Suppress("UNCHECKED_CAST", "IMPLICIT_ANY")
18+
override fun <T> tryGetMetadata(key: HttpRequestMetadataKey<T>): T? = when (key) {
19+
HttpRequestMetadataKey.Method -> baseReq.method as T?
20+
HttpRequestMetadataKey.Path -> baseReq.url.encodedPath as T?
21+
HttpRequestMetadataKey.Host -> baseReq.url.host as T?
22+
HttpRequestMetadataKey.Url -> baseReq.url.toString() as T?
23+
24+
else -> null
25+
}
26+
27+
fun build(): Request = builder.build()
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.m3.tracing.okhttp
2+
3+
import com.google.common.truth.Truth
4+
import com.m3.tracing.http.HttpRequestMetadataKey
5+
import okhttp3.Request
6+
import org.junit.jupiter.api.Test
7+
8+
class OkHttpMutableHttpRequestInfoTest {
9+
@Test
10+
fun `each attribute is set properly for Request`() {
11+
val request = Request.Builder()
12+
.get()
13+
.url("http://example.com/foo/bar.html")
14+
.build()
15+
16+
val req = OkHttpMutableHttpRequestInfo(request)
17+
18+
Truth.assertThat(req.tryGetMetadata(HttpRequestMetadataKey.Host)).isEqualTo("example.com")
19+
Truth.assertThat(req.tryGetMetadata(HttpRequestMetadataKey.Method)).isEqualTo("GET")
20+
Truth.assertThat(req.tryGetMetadata(HttpRequestMetadataKey.Path)).isEqualTo("/foo/bar.html")
21+
Truth.assertThat(req.tryGetMetadata(HttpRequestMetadataKey.Url)).isEqualTo("http://example.com/foo/bar.html")
22+
}
23+
24+
@Test
25+
fun `header is set properly to Request`() {
26+
val request = Request.Builder()
27+
.get()
28+
.url("http://example.com/foo/bar.html")
29+
.build()
30+
31+
val req = OkHttpMutableHttpRequestInfo(request)
32+
33+
req.trySetHeader("hoge", "fuga")
34+
35+
Truth.assertThat(req.build().header("hoge")).isEqualTo("fuga")
36+
}
37+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
handlers=org.slf4j.bridge.SLF4JBridgeHandler
2+
.level=INFO

jvm/settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
include(
22
"apache-httpclient",
3+
"okhttp",
34
"core",
45
"jdbc-p6spy",
56
"opencensus",

0 commit comments

Comments
 (0)