Replies: 2 comments 17 replies
-
|
If it's more straightforward with "something similar to Go labelling", go ahead, but such a mechanism should not be tied to JFR, but be an API that third-party libraries and applications can use to provide context on JVMs and JDK images that don't support JFR. Labels should be accessible through means other than JFR, for example, in logs or in a debugger. Without this capability, it is unlikely that the API will be widely adopted. It would just be an unnecessary large API surface to maintain. Since its inception, the JFR approach has been to provide contexts using events. Events that occur in the same thread between the start and end of a context event are associated with that context. Such events are already in use today, but what we have lacked is means to mark those events as special so that tools like JMC or the 'jfr tool' can offer contextual visualization. For example, this could include a context view where events are organized per context, or displaying context values in an event tooltip or a property page. Currently, there is the As a part of a |
Beta Was this translation helpful? Give feedback.
-
|
Virtual Threads/Carrier Threads Invariants What would happen if a contextual event is used across different threads, for example, begin() called in Thread A and end() called Thread B?
I'm fine with leaving the topic for now, but I think we should think it over from a design perspective (doesn't mean we need to implement it) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Quick links
Rationale
It is proved to be beneficial to be able to connect JFR events with custom context. What the context actually is depends on the application or library and is usually quite domain specific.
From the point of view of a distributed tracer provider the context could be a combination of trace id and span id. But it as well may be a REST endpoint or SQL call, or a subsystem ID, or really anything that can help making sense out of the huge volumes of data we are collecting nowadays.
Design
While the most straightforward design is to use something similar to Go labelling, where an arbitrary list of key-value tuples representing the context is attached to each thread it might be possible to achieve the similar effect using more JFR-like approach.
@egahlin has provided a good writeup in JDK-8325465
Minimal implementation
The writeup is opening up a possibility of more enhanced event filtering but to keep things simple, let's start with a very minimal implementation.
There should be a possibility to annotate a JFR event type as 'context carrier' - all fields defined for that event will be considered as the context. Because context can be 'set' for very short periods this could lead to swamping the recording with context events without any additional value - they would not be associated with any 'regular' events. In order to prevent this swamping the context carrier events should emitted only if there is at least one other event committed between the calls to
begin()andend()of the context carrier event.This implementation will require introducing a new annotation to designate the context carrier event type.
The proposed annotation name
@ContextCarrierand the annotation will be injdk.jfr.experimentalpackaget until it is stable enough that it can be considered to be promoted to public API. Thejdk.jfr.experimentalpackage will not be exported and as such, in order to use this new annotation the user will have to make sure the proper visibility and reachability by eg. command line arguments.Context stack
The context is attached to a particular thread. This also means that there might be more 'contexts' set for a thread - these contexts will form a stack and will live in the JFR thread local instance, already available from a
JavaThread.For the minimal implementation, the context will be represented by a tuple of the context carrier event type id and a boolean flag determining whether another event was committed while the context was active. Let's call this tuple the 'context frame'.
Therefore, a call to
begin()for an event marked@ContextCarrierannotation will effectively push the context frame on stack and, similarly, a call toend()will pop the context frame from the stack.The assumption is that the context stack depth will be rather small, but we might add some safety bars to eg. stop tracking context when reaching certain depth.
Emitting context
The context stack will be integrated with the event writers - both, the Java event writer and the native writer. When an event is written, the context stack will be inspected and if not empty, the top frame will be modified such that boolean part is
true, meaning the context iswith events.There are a few native event types which are related to other thread than the one on which they are being written (eg.
ExecutionSampleetc.) and these need to handled separately such that the proper thread context stack is used.When the
end()method is called for the context carrier event, it will pop the stack frame and store the value of the 'with events' boolean in an implicit event field. This field will then be checked inshouldCommit()method, effectively preventing committing the context event if it was notwith events.Evolvability
The proposed design and implementation leaves the doors open for the further evolution.
Making certain event types context aware and not emit when there is no context present is quite trivial - these events would just need to have their
shouldCommit()method to check for the context stack not being empty.Similarly, the context carrier events could be made to respond only to certain event types by defining an event type ids to check against when setting the 'with events' flag.
However, I would propose postponing the implementation of the more advanced filterings after we have the initial implementation done - the additional flexibility also means additional perf hit and the benefit/cost ratio must be evaluated.
Beta Was this translation helpful? Give feedback.
All reactions