Skip to content

Instantly share code, notes, and snippets.

@danmurphycp
Created February 16, 2022 21:15
Show Gist options
  • Save danmurphycp/df3f7ca31c03e590df55918ee89951ba to your computer and use it in GitHub Desktop.
Save danmurphycp/df3f7ca31c03e590df55918ee89951ba to your computer and use it in GitHub Desktop.
Example implementation of useTransactionIdCapturingDispatcher to configure transaction id forwarding
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