Commit | Line | Data |
---|---|---|
8ab5c06b AM |
1 | /* |
2 | * Copyright (C) 2016 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to | |
6 | * deal in the Software without restriction, including without limitation the | |
7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
8 | * sell copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
20 | * IN THE SOFTWARE. | |
21 | */ | |
22 | ||
23 | import java.io.IOException; | |
24 | import java.util.logging.Handler; | |
25 | import java.util.logging.Logger; | |
26 | ||
27 | import org.lttng.ust.agent.context.ContextInfoManager; | |
28 | import org.lttng.ust.agent.context.IContextInfoRetriever; | |
29 | import org.lttng.ust.agent.jul.LttngLogHandler; | |
30 | ||
31 | /** | |
32 | * Example program defining a application context retriever, which allows | |
33 | * attaching application-defined contexts to trace events. | |
34 | * | |
35 | * FIXME Use custom context names, and several names/types | |
36 | * | |
37 | * <p> | |
38 | * Usage: | |
39 | * <ul> | |
40 | * <li>$ lttng create</li> | |
41 | * <li>$ lttng enable-event -j -a</li> | |
42 | * <li>$ lttng add-context -j -t '$app.myprovider:mystringcontext'</li> | |
43 | * <li>$ lttng add-context -j -t '$app.myprovider:myshortcontext'</li> | |
44 | * <li>$ lttng start</li> | |
45 | * <li>(run this program)</li> | |
46 | * <li>$ lttng stop</li> | |
47 | * <li>$ lttng view</li> | |
48 | * <li>$ lttng destroy</li> | |
49 | * </ul> | |
50 | * </p> | |
51 | * | |
52 | * The events present in the resulting trace should carry the context | |
53 | * information defined in the example retriever. | |
54 | * | |
55 | * @author Alexandre Montplaisir | |
56 | */ | |
57 | public class ApplicationContextExample { | |
58 | ||
59 | /** Class-wide JUL logger object */ | |
60 | private static final Logger LOGGER = Logger.getLogger(ApplicationContextExample.class.getName()); | |
61 | ||
62 | private static final String RETRIEVER_NAME = "myprovider"; | |
63 | private static final String CONTEXT_NAME_STRING = "mystringcontext"; | |
64 | private static final String CONTEXT_NAME_SHORT = "myshortcontext"; | |
65 | ||
66 | private static class ExampleContextInfoRetriever implements IContextInfoRetriever { | |
67 | ||
68 | @Override | |
69 | public Object retrieveContextInfo(String key) { | |
e01dcb20 | 70 | if (CONTEXT_NAME_SHORT.equals(key)) { |
8ab5c06b | 71 | return (short) 42; |
e01dcb20 | 72 | } else if (CONTEXT_NAME_STRING.equals(key)) { |
8ab5c06b | 73 | return "context-value!"; |
e01dcb20 | 74 | } else { |
8ab5c06b AM |
75 | return null; |
76 | } | |
77 | } | |
78 | ||
79 | } | |
80 | ||
81 | /** | |
82 | * Application start | |
83 | * | |
84 | * @param args | |
85 | * Command-line arguments | |
86 | * @throws IOException | |
87 | * @throws InterruptedException | |
88 | */ | |
89 | public static void main(String args[]) throws IOException, InterruptedException { | |
90 | /* Instantiate and attach a logger object */ | |
91 | Handler lttngHandler = new LttngLogHandler(); | |
92 | LOGGER.addHandler(lttngHandler); | |
93 | ||
94 | /* Instantiate and register the context retriever */ | |
95 | IContextInfoRetriever cir = new ExampleContextInfoRetriever(); | |
96 | ContextInfoManager.getInstance().registerContextInfoRetriever(RETRIEVER_NAME, cir); | |
97 | ||
98 | /* | |
99 | * Make sure you have a LTTng session running with the appropriate | |
100 | * events and contexts enabled! See the class Javadoc. | |
101 | */ | |
102 | ||
103 | /* Trigger a tracing event using the JUL Logger created before. */ | |
104 | LOGGER.info("Log event #1"); | |
105 | LOGGER.warning("Log event #2"); | |
106 | LOGGER.severe("Log event #3"); | |
107 | ||
108 | /* Unregister our context retriever, and dispose the log handler */ | |
109 | ContextInfoManager.getInstance().unregisterContextInfoRetriever(RETRIEVER_NAME); | |
110 | lttngHandler.close(); | |
111 | } | |
112 | } |