|
Packit |
c04fcb |
/*
|
|
Packit |
c04fcb |
* Copyright (C) 2016 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
Packit |
c04fcb |
* of this software and associated documentation files (the "Software"), to
|
|
Packit |
c04fcb |
* deal in the Software without restriction, including without limitation the
|
|
Packit |
c04fcb |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
Packit |
c04fcb |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
Packit |
c04fcb |
* furnished to do so, subject to the following conditions:
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* The above copyright notice and this permission notice shall be included in
|
|
Packit |
c04fcb |
* all copies or substantial portions of the Software.
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
Packit |
c04fcb |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
Packit |
c04fcb |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
Packit |
c04fcb |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
Packit |
c04fcb |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
Packit |
c04fcb |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
Packit |
c04fcb |
* IN THE SOFTWARE.
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
import java.io.IOException;
|
|
Packit |
c04fcb |
import java.util.logging.Handler;
|
|
Packit |
c04fcb |
import java.util.logging.Logger;
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
import org.lttng.ust.agent.context.ContextInfoManager;
|
|
Packit |
c04fcb |
import org.lttng.ust.agent.context.IContextInfoRetriever;
|
|
Packit |
c04fcb |
import org.lttng.ust.agent.jul.LttngLogHandler;
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/**
|
|
Packit |
c04fcb |
* Example program defining a application context retriever, which allows
|
|
Packit |
c04fcb |
* attaching application-defined contexts to trace events.
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* FIXME Use custom context names, and several names/types
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* Usage:
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* $ lttng create
|
|
Packit |
c04fcb |
* $ lttng enable-event -j -a
|
|
Packit |
c04fcb |
* $ lttng add-context -j -t '$app.myprovider:mystringcontext'
|
|
Packit |
c04fcb |
* $ lttng add-context -j -t '$app.myprovider:myshortcontext'
|
|
Packit |
c04fcb |
* $ lttng start
|
|
Packit |
c04fcb |
* (run this program)
|
|
Packit |
c04fcb |
* $ lttng stop
|
|
Packit |
c04fcb |
* $ lttng view
|
|
Packit |
c04fcb |
* $ lttng destroy
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* The events present in the resulting trace should carry the context
|
|
Packit |
c04fcb |
* information defined in the example retriever.
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* @author Alexandre Montplaisir
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
public class ApplicationContextExample {
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/** Class-wide JUL logger object */
|
|
Packit |
c04fcb |
private static final Logger LOGGER = Logger.getLogger(ApplicationContextExample.class.getName());
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
private static final String RETRIEVER_NAME = "myprovider";
|
|
Packit |
c04fcb |
private static final String CONTEXT_NAME_STRING = "mystringcontext";
|
|
Packit |
c04fcb |
private static final String CONTEXT_NAME_SHORT = "myshortcontext";
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
private static class ExampleContextInfoRetriever implements IContextInfoRetriever {
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
@Override
|
|
Packit |
c04fcb |
public Object retrieveContextInfo(String key) {
|
|
Packit |
c04fcb |
if (CONTEXT_NAME_SHORT.equals(key)) {
|
|
Packit |
c04fcb |
return (short) 42;
|
|
Packit |
c04fcb |
} else if (CONTEXT_NAME_STRING.equals(key)) {
|
|
Packit |
c04fcb |
return "context-value!";
|
|
Packit |
c04fcb |
} else {
|
|
Packit |
c04fcb |
return null;
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/**
|
|
Packit |
c04fcb |
* Application start
|
|
Packit |
c04fcb |
*
|
|
Packit |
c04fcb |
* @param args
|
|
Packit |
c04fcb |
* Command-line arguments
|
|
Packit |
c04fcb |
* @throws IOException
|
|
Packit |
c04fcb |
* @throws InterruptedException
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
public static void main(String args[]) throws IOException, InterruptedException {
|
|
Packit |
c04fcb |
/* Instantiate and attach a logger object */
|
|
Packit |
c04fcb |
Handler lttngHandler = new LttngLogHandler();
|
|
Packit |
c04fcb |
LOGGER.addHandler(lttngHandler);
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/* Instantiate and register the context retriever */
|
|
Packit |
c04fcb |
IContextInfoRetriever cir = new ExampleContextInfoRetriever();
|
|
Packit |
c04fcb |
ContextInfoManager.getInstance().registerContextInfoRetriever(RETRIEVER_NAME, cir);
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/*
|
|
Packit |
c04fcb |
* Make sure you have a LTTng session running with the appropriate
|
|
Packit |
c04fcb |
* events and contexts enabled! See the class Javadoc.
|
|
Packit |
c04fcb |
*/
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/* Trigger a tracing event using the JUL Logger created before. */
|
|
Packit |
c04fcb |
LOGGER.info("Log event #1");
|
|
Packit |
c04fcb |
LOGGER.warning("Log event #2");
|
|
Packit |
c04fcb |
LOGGER.severe("Log event #3");
|
|
Packit |
c04fcb |
|
|
Packit |
c04fcb |
/* Unregister our context retriever, and dispose the log handler */
|
|
Packit |
c04fcb |
ContextInfoManager.getInstance().unregisterContextInfoRetriever(RETRIEVER_NAME);
|
|
Packit |
c04fcb |
lttngHandler.close();
|
|
Packit |
c04fcb |
}
|
|
Packit |
c04fcb |
}
|