Skip to content

Instantly share code, notes, and snippets.

@juyeong
Created December 1, 2019 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juyeong/cdca862df4bffd0b375ace6a125afaef to your computer and use it in GitHub Desktop.
Save juyeong/cdca862df4bffd0b375ace6a125afaef to your computer and use it in GitHub Desktop.
OKHttpInterceptor to trace AWS X-Ray
package bot.data
import com.amazonaws.xray.AWSXRay
import com.amazonaws.xray.entities.Namespace
import okhttp3.Interceptor
import okhttp3.Response
class OkHttpXRayInterceptor : Interceptor {
companion object {
private val recorder = AWSXRay.getGlobalRecorder()
}
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val segment = recorder.beginSubsegment("OkHttp")?.apply {
namespace = Namespace.REMOTE.toString()
putHttp("request", mapOf(
"url" to request.url.toString(),
"method" to request.method
))
}
return chain.proceed(request).also { response ->
segment?.run {
isThrottle = response.code == 429
isError = (400 until 500).contains(response.code)
isFault = (500 until 600).contains(response.code)
putHttp("response", mapOf("status" to response.code))
close()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment