Created
December 1, 2019 06:02
-
-
Save juyeong/cdca862df4bffd0b375ace6a125afaef to your computer and use it in GitHub Desktop.
OKHttpInterceptor to trace AWS X-Ray
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
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