Skip to content
Merged
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
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
## Unreleased
-->

## Release notes - 1.0.4 (2020-08-07)

## Added

- OkHttp integration


## Release notes - 1.0.3 (2020-08-04)

### Breaking Change

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

### Added

Expand Down Expand Up @@ -55,4 +62,4 @@ First release
## Fixed

## Security
-->
-->
3 changes: 2 additions & 1 deletion jvm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {

allprojects {
group = "com.m3.tracing"
version = "1.0.3"
version = "1.0.4"

repositories {
jcenter()
Expand Down Expand Up @@ -52,6 +52,7 @@ subprojects {
val springBootVersion by extra { "2.1.4.RELEASE" }
val springVersion by extra { "5.1.6.RELEASE" }
val apacheHttpClientVersion by extra { "4.5.8" }
val okHttpClientVersion by extra { "4.8.0" }

tasks.withType<Test> {
useJUnitPlatform()
Expand Down
17 changes: 17 additions & 0 deletions jvm/okhttp/README.md
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()
```
9 changes: 9 additions & 0 deletions jvm/okhttp/build.gradle.kts
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"]}")
}
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)
}
}
}
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()
}
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")
}
}
2 changes: 2 additions & 0 deletions jvm/okhttp/src/test/resources/logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
handlers=org.slf4j.bridge.SLF4JBridgeHandler
.level=INFO
1 change: 1 addition & 0 deletions jvm/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include(
"apache-httpclient",
"okhttp",
"core",
"jdbc-p6spy",
"opencensus",
Expand Down