Created
February 16, 2022 21:15
-
-
Save danmurphycp/df3f7ca31c03e590df55918ee89951ba to your computer and use it in GitHub Desktop.
Example implementation of useTransactionIdCapturingDispatcher to configure transaction id forwarding
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
import okhttp3.Dispatcher | |
import okhttp3.OkHttpClient | |
import org.slf4j.MDC | |
import java.util.concurrent.ExecutorService | |
import java.util.concurrent.Executors | |
public fun OkHttpClient.Builder.useTransactionIdCapturingDispatcher( | |
executorService: ExecutorService = Executors.newCachedThreadPool() | |
): OkHttpClient.Builder = dispatcher( | |
Dispatcher(TransactionContextCapturingExecutorService(executorService)) | |
).addInterceptor { | |
val requestBuilder = it.request().newBuilder() | |
val transactionId = MDC.get("transaction_id") | |
if (requestId != null) { | |
requestBuilder.addHeader("x-cp-transaction-id", transactionId) | |
} | |
it.proceed(requestBuilder.build()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment