com.jkoolcloud.tnt4j.examples.TNT4JTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tnt4j-log4j Show documentation
Show all versions of tnt4j-log4j Show documentation
Log4j Appender over TNT4J API
The newest version!
/*
* Copyright 2014-2023 JKOOL, LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jkoolcloud.tnt4j.examples;
import java.util.Date;
import java.util.Random;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.jkoolcloud.tnt4j.TrackingLogger;
import com.jkoolcloud.tnt4j.config.DefaultConfigFactory;
import com.jkoolcloud.tnt4j.config.TrackerConfig;
import com.jkoolcloud.tnt4j.core.*;
import com.jkoolcloud.tnt4j.dump.*;
import com.jkoolcloud.tnt4j.selector.TrackingSelector;
import com.jkoolcloud.tnt4j.sink.SinkError;
import com.jkoolcloud.tnt4j.sink.SinkErrorListener;
import com.jkoolcloud.tnt4j.sink.SinkLogEvent;
import com.jkoolcloud.tnt4j.sink.SinkLogEventListener;
import com.jkoolcloud.tnt4j.tracker.TrackingActivity;
import com.jkoolcloud.tnt4j.tracker.TrackingEvent;
import com.jkoolcloud.tnt4j.utils.TimeService;
import com.jkoolcloud.tnt4j.utils.Utils;
/**
*
* jKool TNT4J Test application that exercises TNT4J API. This application generates a simulated activity with 10
* tracking events. Usage: app-name server-name event-msg correlator operation-name location
*
*
*
* @version $Revision: 30 $
*
* @see TrackingActivity
* @see TrackingEvent
*/
public class TNT4JTest {
private static final Logger logger = LogManager.getLogger(TNT4JTest.class);
private static final Random rand = new Random(System.currentTimeMillis());
protected static int activityCount = 0, eventCount = 0;
private static TrackingLogger tlogger = null;
/**
* Run TNT4J Test application and generate simulated activity
*
* @param args
* Usage: app-name server-name event-msg correlator operation-name
*/
public static void main(String[] args) {
if (args.length < 6) {
System.out.println("Usage: appl server msg correlator opname location");
System.exit(-1);
}
System.out.println("Current call=" + Utils.getCurrentStackFrame() + ", caller=" + Utils.getCallingStackFrame());
System.out.println("Time overhead: " + TimeService.getOverheadNanos());
// register with the TNT4J framework
TrackerConfig config = DefaultConfigFactory.getInstance().getConfig(args[0]);
config.setSinkLogEventListener(new MySinkLogHandler());
config.setActivityListener(new MyActivityHandler());
tlogger = TrackingLogger.getInstance(config.build());
tlogger.addSinkErrorListener(new MySinkErrorHandler());
Snapshot begin = tlogger.newSnapshot("Start", "TNT4JTest");
begin.add("time-overhead-nanos", TimeService.getOverheadNanos());
tlogger.tnt(begin);
// optionally register application state dump
// by default dumps are generated on JVM shutdown
TrackingLogger.addDumpListener(new DumpNotify());
TrackingLogger.addDumpProvider(new MyDumpProvider(args[0], "ApplRuntime"));
// create and start an activity
TrackingActivity activity = tlogger.newActivity(OpLevel.INFO, "LoggingBenchmark");
TrackingLogger.addDumpProvider(new ObjectDumpProvider(args[0], activity));
String[] cids = args[3].split(":");
activityCount++;
activity.start();
for (int i = 0; i < 10; i++) {
TrackingEvent event = tlogger.newEvent(OpLevel.DEBUG, "runSampleActivity", cids[0], "Running sample={0}",
i);
event.setCorrelator(cids);
eventCount++;
event.start(); // start timing current event
try {
runSampleActivity(args[2], cids, args[4], args[5]);
} finally {
event.stop();
activity.tnt(event); // associate current event with the current activity
}
}
activity.stop(); // stop activity timing
tlogger.tnt(activity); // log and report activity
System.out.println("Logging stats: " + tlogger.getStats());
System.out.println("Registered loggers: size=" + TrackingLogger.getAllTrackers().size());
Snapshot end = tlogger.newSnapshot("End", "TNT4JTest");
end.add("loggers-size", TrackingLogger.getAllTrackers().size());
end.add("stack-size", tlogger.getStackSize());
end.setParentId(activity);
tlogger.tnt(end);
for (TrackingLogger tl : TrackingLogger.getAllTrackers()) {
Utils.printStackTrace("Tracker stack trace", tl.getStackTrace(), System.out);
}
tlogger.close(); // deregister and release all logging resources
System.out.println("Registered loggers: size=" + TrackingLogger.getAllTrackers().size() + ", list="
+ TrackingLogger.getAllTrackers());
}
static private TrackingActivity runSampleActivity(String msg, String[] cids, String opName, String location) {
TrackingActivity activity = tlogger.newActivity(OpLevel.INFO, "runSampleActivity");
activityCount++;
activity.start();
int runs = rand.nextInt(50);
int sev = rand.nextInt(OpLevel.values().length);
for (int i = 0; i < runs; i++) {
int limit = rand.nextInt(10000000);
TrackingEvent ev4j = runTNT4JEvent(msg, opName, OpLevel.valueOf(sev), cids, location, limit);
ev4j.setLocation(location);
TrackingEvent log4j = runLog4JEvent(msg, opName, OpLevel.valueOf(sev), cids, location, limit);
log4j.setLocation(location);
if (tlogger.isSet(OpLevel.INFO, "tnt4j.test.location", location)) {
activity.tnt(ev4j);
activity.tnt(log4j);
}
}
activity.stop();
tlogger.tnt(activity);
return activity;
}
static private TrackingEvent runTNT4JEvent(String msg, String opName, OpLevel sev, String[] cids, String location,
int limit) {
TrackingEvent event = tlogger.newEvent(sev, opName, cids[0], msg);
event.setCorrelator(cids);
eventCount++;
TrackingSelector selector = tlogger.getTracker().getTrackingSelector();
try {
event.setTag(String.valueOf(Utils.getVMName()));
event.setMessage("{0}, tnt4j.run.count={1}", msg, limit);
event.start();
for (int i = 0; i < limit; i++) {
selector.isSet(OpLevel.INFO, "tnt4j.test.location", location);
}
} finally {
event.stop();
tlogger.info("runTNT4JEvent: runs=\"{0}\", elapsed.nsec={1}, nsec/call={2}", limit,
event.getOperation().getElapsedTimeNano(), (event.getOperation().getElapsedTimeNano() / limit));
}
return event;
}
static private TrackingEvent runLog4JEvent(String msg, String opName, OpLevel sev, String[] cids, String location,
int limit) {
TrackingEvent event = tlogger.newEvent(sev, opName, cids[0], msg);
event.setCorrelator(cids);
eventCount++;
try {
event.setTag(String.valueOf(Utils.getVMName()));
event.setMessage("{0}, log4j.run.count={1}", msg, limit);
event.start();
for (int i = 0; i < limit; i++) {
logger.isDebugEnabled();
}
} finally {
event.stop();
tlogger.info("runLog4JEvent: runs={0}, elapsed.nsec={1}, nsec/call={2}, method={3}, caller={4}", limit,
event.getOperation().getElapsedTimeNano(), (event.getOperation().getElapsedTimeNano() / limit),
Utils.getCurrentStackFrame(), Utils.getCallingStackFrame());
}
return event;
}
}
class MyActivityHandler implements ActivityListener {
@Override
public void started(Activity activity) {
System.out.println("activity.id=" + activity.getTrackingId() + ", activity.name=" + activity.getName()
+ ", started=" + activity.getStartTime());
}
@Override
public void stopped(Activity activity) {
// post-processing of activity: enrich activity with application metrics
PropertySnapshot snapshot = new PropertySnapshot("TestApp", "APPL_METRICS");
snapshot.add("appl.activity.count", TNT4JTest.activityCount);
snapshot.add("appl.event.count", TNT4JTest.eventCount);
activity.add(snapshot); // add property snapshot to activity
System.out.println("activity.id=" + activity.getTrackingId() + ", activity.name=" + activity.getName()
+ ", elapsed.usec=" + activity.getElapsedTimeUsec() + ", snap.count=" + activity.getSnapshotCount()
+ ", id.count=" + activity.getIdCount());
}
}
class MySinkErrorHandler implements SinkErrorListener {
@Override
public void sinkError(SinkError event) {
System.out.println("onSinkError: " + event);
if (event.getCause() != null) {
event.getCause().printStackTrace();
}
}
}
class MySinkLogHandler implements SinkLogEventListener {
@Override
public void sinkLogEvent(SinkLogEvent event) {
// System.out.println("sink.LOG: sev=" + event.getSeverity() + ", source=" + event.getSource() + ", msg=" +
// event.getSinkObject());
}
}
class MyDumpProvider extends DefaultDumpProvider {
private long startTime = 0;
public MyDumpProvider(String name, String cat) {
super(name, cat);
startTime = TimeService.currentTimeMillis();
}
@Override
public DumpCollection getDump() {
Dump dump = new Dump("runtimeMetrics", this);
dump.add("appl.start.time", new Date(startTime));
dump.add("appl.elapsed.ms", (TimeService.currentTimeMillis() - startTime));
dump.add("appl.activity.count", TNT4JTest.activityCount);
dump.add("appl.event.count", TNT4JTest.eventCount);
return dump;
}
}
class DumpNotify implements DumpListener {
@Override
public void onDumpEvent(DumpEvent event) {
switch (event.getType()) {
case DumpProvider.DUMP_BEFORE:
case DumpProvider.DUMP_AFTER:
case DumpProvider.DUMP_COMPLETE:
case DumpProvider.DUMP_ERROR:
System.out.println("onDump: " + event);
if (event.getCause() != null) {
event.getCause().printStackTrace();
}
}
}
}