I have a complex stats system to monitor the behavior of our app.
One of the things I want to do is track the name of a thread/task calling a component.
So I have a component called a Caller which I inject so that when a component is created, I can see who called it.
The problem is, how do you change the caller at runtime?
It should be possible to do this with thread locals and a @Singleton.
So basically you have a Task, which has a Caller dependency... then that task just does
String defaultCaller = caller.get();
try {
caller.set( "my-task" );
mytask.run();
} finally {
caller.set( defaultCaller );
}
... now I'm not super happy with thread locals, but this doesn't seem amazingly ugly.
Is there a better way to do this?