-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,11 @@ | ||
# Apache HttpClient integration | ||
|
||
## How to setup | ||
|
||
Add `M3TracingHttpInterceptor` as request/response interceptor of HttpClient. | ||
|
||
```java | ||
// CAUTION: Must setup as BOTH interceptor otherwise it may cause memory leak. | ||
httpclient.addRequestInterceptor(M3TracingHttpInterceptor.INSTANCE); | ||
httpclient.addResponseInterceptor(M3TracingHttpInterceptor.INSTANCE); | ||
``` |
This file contains 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("org.apache.httpcomponents:httpclient:${project.extra["apacheHttpClientVersion"]}") | ||
} |
73 changes: 73 additions & 0 deletions
73
...e-httpclient/src/main/kotlin/com/m3/tracing/apache/httpclient/M3TracingHttpInterceptor.kt
This file contains 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,73 @@ | ||
package com.m3.tracing.apache.httpclient | ||
|
||
import com.m3.tracing.M3Tracer | ||
import com.m3.tracing.M3TracerFactory | ||
import com.m3.tracing.TraceSpan | ||
import org.apache.http.HttpRequest | ||
import org.apache.http.HttpRequestInterceptor | ||
import org.apache.http.HttpResponse | ||
import org.apache.http.HttpResponseInterceptor | ||
import org.apache.http.client.methods.HttpUriRequest | ||
import org.apache.http.protocol.HttpContext | ||
import org.slf4j.LoggerFactory | ||
|
||
/** | ||
* Interceptor for Apache HTTP Client. | ||
* | ||
*/ | ||
open class M3TracingHttpInterceptor( | ||
protected val tracer: M3Tracer | ||
) : HttpRequestInterceptor, HttpResponseInterceptor { | ||
companion object { | ||
@JvmStatic | ||
public val INSTANCE = M3TracingHttpInterceptor() | ||
|
||
private val currentSpan = ThreadLocal<TraceSpan>() | ||
private val logger = LoggerFactory.getLogger(M3TracingHttpInterceptor::class.java) | ||
} | ||
|
||
constructor(): this(M3TracerFactory.get()) | ||
|
||
override fun process(request: HttpRequest, context: HttpContext) { | ||
val span = tracer.startSpan(createSpanName(request)) | ||
currentSpan.set(span) // Set to ThreadLocal ASAP to prevent leak | ||
|
||
doQuietly { | ||
span["method"] = request.requestLine.method | ||
span["uri"] = request.requestLine.uri | ||
} | ||
} | ||
|
||
override fun process(response: HttpResponse, context: HttpContext) { | ||
val span = currentSpan.get() ?: return | ||
currentSpan.set(null) // Prevent ClassLoader leak | ||
|
||
// Must continue to span.close() statement to prevent memory leak | ||
doQuietly { | ||
span["status"] = response.statusLine.statusCode | ||
} | ||
span.close() | ||
} | ||
|
||
|
||
private fun createSpanName(request: HttpRequest): String { | ||
// Intentionally excluded queryString because it might contain dynamic string | ||
// Dynamic span name makes runningSpan table so huge | ||
return if (request is HttpUriRequest) { | ||
"HTTP ${request.method} ${request.uri.host}" | ||
} else { | ||
"HTTP ${request.requestLine.method}" | ||
} | ||
} | ||
|
||
/** | ||
* `On Error Resume Next` in 21st century. | ||
*/ | ||
protected fun doQuietly(action: () -> Unit) { | ||
try { | ||
action() | ||
} catch (e: Throwable) { | ||
logger.error("Failed to update Span.", e) | ||
} | ||
} | ||
} |
This file contains 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 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 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 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,4 +1,5 @@ | ||
include( | ||
"apache-httpclient", | ||
"core", | ||
"jdbc-p6spy", | ||
"opencensus", | ||
|