Reputation: 1571
I am trying to pass monitoring/tracing information through all my external calls in my java application. To make it transparent, I'm trying to use byte-buddy but have some troubles getting it to work.
To trace every incoming (http) request, I intercept HttpServlet.service()
, extract the token header from the HttpServletRequest
and put it in a static ThreadLocal
in a class named TokenHolder
.
To trace every outgoing (http) request, I intercept HttpURLConnection
and add the token header I get from the same ThreadLocal
(TokenHolder
).
The problem I have is that TokenHolder
seems to be initialized twice and my 2 interceptors are not writing-to/reading-from the same ThreadLocal
and I can't find a way to do it.
I suppose the problem is that HttpURLConnection
lives in the bootclasspath while the servlet API does not.
Bonus question: is it possible to intercept URL.openConnection()
? That was my first idea but I never could do it because I suppose the URL
class is loaded before the agent (because of URLClassLoader
) but I don't know if there are workarounds to that.
Upvotes: 1
Views: 244
Reputation: 44032
Yes, you can register a RedefinitionStrategy
where Byte Buddy transforms previously loaded classes. To do so, you do however need to avoid adding methods or fields. This can typically be done by using Advice
only.
You are also right that classes need to live on the bootstrap loader. You can inject classes into the bootstrap loader by placing them in a jar and using the designated method in the Instrumentation
interface.
Upvotes: 1