Commit | Line | Data |
---|---|---|
d60dfbe4 AM |
1 | ====================== |
2 | Using the Java agent | |
3 | ====================== | |
4 | ||
3707dfc5 | 5 | The agent can be built in three different configurations: |
6f3cecd7 CB |
6 | |
7 | 1) Java agent with JUL support: | |
8 | ||
9 | $ ./configure --enable-java-agent-jul | |
10 | ||
464c4756 | 11 | 2) Java agent with Log4j 1.x support (deprecated): |
6f3cecd7 CB |
12 | |
13 | $ export CLASSPATH=$CLASSPATH:/path/to/log4j.jar | |
14 | $ ./configure --enable-java-agent-log4j | |
15 | ||
464c4756 | 16 | 3) Java agent with Log4j 2.x support: |
6f3cecd7 | 17 | |
464c4756 MJ |
18 | $ export CLASSPATH=$CLASSPATH:/path/to/log4j-core.jar:/path/to/log4j-api.jar |
19 | $ ./configure --enable-java-agent-log4j2 | |
20 | ||
21 | 4) Java agent with JUL + Log4j 1.x + Log4j 2.x support | |
22 | ||
23 | $ export CLASSPATH=$CLASSPATH:/path/to/log4j.jar:/path/to/log4j-core.jar:/path/to/log4j-api.jar | |
6f3cecd7 CB |
24 | $ ./configure --enable-java-agent-all |
25 | ||
26 | To build the agent with log4j support, make sure that the log4j jar | |
27 | is in your Java classpath. | |
28 | ||
29 | The configure script will automatically detect the appropriate Java | |
30 | binaries to use in order to build the Java agent. | |
31 | ||
3707dfc5 | 32 | Enabling the JUL support will build a "lttng-ust-agent-jul.jar" file. Enabling |
464c4756 MJ |
33 | the log4j 1.x support will build a "lttng-ust-agent-log4j.jar" and enabling |
34 | log4j 2.x support will build a "lttng-ust-agent-log4j2.jar". All of these jars | |
3707dfc5 | 35 | depend on a third "lttng-ust-agent-common.jar", which will always be built. |
6f3cecd7 | 36 | |
3707dfc5 AM |
37 | All these archives will be installed in the arch-agnostic "$prefix/share/java" |
38 | path, e.g: "/usr/share/java". You need to make sure the .jar for the logging | |
464c4756 MJ |
39 | API you want to use (either "lttng-ust-agent-jul.jar", |
40 | "lttng-ust-agent-log4j.jar" or "lttng-ust-agent-log4j2.jar") is on your | |
3707dfc5 | 41 | application's classpath. |
6f3cecd7 | 42 | |
464c4756 MJ |
43 | The logging libraries require an architecture-specific shared object, |
44 | "liblttng-ust-jul-jni.so" for JUL and "liblttng-ust-jul-log4j.so" for both | |
45 | Log4j 1.x and 2.x, which are installed by the build system when doing "make | |
46 | install". Make sure that your Java application can find this shared object, by | |
47 | using the "java.library.path" property if necessary. | |
d60dfbe4 AM |
48 | |
49 | In order to use UST tracing in your Java application, you simply need to | |
50 | instantiate a LttngLogHandler or a LttngLogAppender (for JUL or Log4j, | |
51 | respectively), then attach it to a JUL or Log4j Logger class. | |
52 | ||
464c4756 MJ |
53 | Refer to the code examples in "examples/java-jul/", "examples/java-log4j/" and |
54 | "examples/java-log4j2-*/". | |
d60dfbe4 AM |
55 | |
56 | LTTng session daemon agents will be initialized as needed. If no session daemon | |
57 | is available, the execution will continue and the agents will retry connecting | |
58 | every 3 seconds. | |
59 | ||
60 | ||
61 | ============== | |
62 | Object model | |
63 | ============== | |
64 | ||
65 | The object model of the Java agent implementation is as follows: | |
66 | ||
67 | --------- | |
68 | Ownership | |
69 | --------- | |
70 | Log Handlers: LttngLogHandler, LttngLogAppender | |
71 | n handlers/appenders, managed by the application. | |
72 | Can be created programmatically, or via a configuration file, | |
73 | Each one registers to a specific agent singleton (one per logging API) that is loaded on-demand | |
74 | ||
75 | Agent singletons: LttngJulAgent, LttngLog4jAgent | |
76 | Keep track of all handlers/appenders registered to them. | |
77 | Are disposed when last handler deregisters. | |
78 | Each agent instantiates 2 TCP clients, one for the root session daemon, one for the user one. | |
79 | One type of TCP client class for now. TCP client may become a singleton in the future. | |
80 | ||
81 | ------- | |
82 | Control | |
83 | ------- | |
84 | Messages come from the session daemon through the socket connection. | |
85 | Agent passes back-reference to itself to the TCP clients. | |
86 | Clients use this reference to invoke callbacks, which modify the state of the agent (enabling/disabling events, etc.) | |
87 | ||
88 | --------- | |
89 | Data path | |
90 | --------- | |
91 | Log messages are generated by the application and sent to the Logger objects, | |
92 | which then send them to the Handlers. | |
6f3cecd7 | 93 | |
d60dfbe4 AM |
94 | When a log event is received by a Handler (publish(LogRecord)), the handler |
95 | checks with the agent if it should log it or not, via | |
96 | ILttngAgent#isEventEnabled() for example. | |
6f3cecd7 | 97 | |
d60dfbe4 AM |
98 | Events that are logged call the native tracepoint through JNI, which generates |
99 | a UST event. There is one type of tracepoint per domain (Jul or Logj4). | |
6f3cecd7 | 100 | |
78439abe AM |
101 | ----------------------- |
102 | Filtering notifications | |
103 | ----------------------- | |
104 | FilterChangeNotifier is the singleton notifier class. | |
105 | Applications implement an IFilterChangeListener, and register it to the notifier. | |
106 | ||
107 | Whenever new event rules are enabled or disabled, the relevant agent informs the | |
108 | notifier, which then sends notifications to all registered listeners by invoking | |
109 | their callbacks. | |
110 | ||
111 | Upon registration, a new listener will receive notifications for all currently | |
112 | active rules. | |
113 | ||
114 | The notifier keeps track of its own event rule refcounting, to handle the case | |
115 | of multiple sessions or multiple agents enabling identical event rules. | |
116 | ||
117 | The FilterChangeNotifier does not have threads of its own. The listeners's | |
118 | callbacks will be invoked by these threads: | |
119 | * In the case of a notification being received while a listener is already | |
120 | registered, the callback is executed by the TCP client's thread. This | |
121 | effectively blocks the "lttng" command line until all callbacks are processed | |
122 | (assuming no timeouts). | |
123 | * In the case of a listener registering and receiving the currently-active | |
124 | rules, the callbacks will be executed by the application's thread doing the | |
125 | registerListener() call. | |
126 | ||
127 | The notifier is entirely synchronized. This ensure that if a rule is enabled | |
128 | at the same time a listener is registered, that listener does not miss or | |
129 | receive duplicate notifications. |