aiguofer commented on issue #38911:
URL: https://github.com/apache/arrow/issues/38911#issuecomment-1868017220
We currently accomplish this with a gRPC `ServerInterceptor` to add it to
the `RequestContext` (in Kotlin):
```
class RequestContextKeys {
companion object {
const val AUTHORITY = "authority"
const val SCHEME = "scheme"
const val PORT = "port"
const val INCOMING_URL = "incomingUrl"
const val REQUEST_ID = "request_id"
}
}
/*
* Simple interceptor to add more metadata from the call to the
RequestContext.
*/
class RequestContextInterceptor : ServerInterceptor {
override fun <ReqT : Any, RespT : Any> interceptCall(
call: ServerCall<ReqT, RespT>,
headers: Metadata,
next: ServerCallHandler<ReqT, RespT>
): ServerCall.Listener<ReqT> {
RequestContextAdapter.REQUEST_CONTEXT_KEY.get()?.also {
requestContext ->
val authority =
call.authority ?: throw InternalServiceException("Could not
find authority")
val scheme = if (authority.startsWith("localhost")) "http" else
"https"
val port =
when {
authority.contains(":") -> authority.substringAfter(":")
scheme == "https" -> "443"
else -> throw InternalServiceException("Could not infer
port number")
}
val host = authority.substringBefore(":")
requestContext.put(RequestContextKeys.AUTHORITY, authority)
requestContext.put(RequestContextKeys.SCHEME, scheme)
requestContext.put(RequestContextKeys.PORT, port)
requestContext.put(RequestContextKeys.INCOMING_URL,
"$scheme://$host:$port")
}
return next.startCall(call, headers)
}
}
```
We can then access it anywhere using:
```
val requestContext = RequestContextAdapter.REQUEST_CONTEXT_KEY.get()
requestContext.get(RequestContextKeys.INCOMING_URL)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]