/** * Factory method that creates a new ThreadContext initialized to also update Log4j's MDC. */ public static ThreadContext newMDCEnabledContext() { return new ThreadContext(new Log4jMdcBridge()); }
/** * Clears all values for the current thread. */ public void clear() { /** * Unfortunately log4j's MDC historically never had a mechanism to block remove keys, * so we're forced to do this one by one. */ for (String key : allKeys()) { mdcBridge.remove(key); } PER_THREAD_CONTEXTS.getUnchecked(Thread.currentThread()).clear(); }
public void testClearRemovesMDCValue() { ThreadContext log4jThreadContext = ThreadContext.newMDCEnabledContext(); Hashtable mdcContext = MDC.getContext(); assertTrue(mdcContext == null || mdcContext.isEmpty()); final String testKey = "painter"; log4jThreadContext.put(testKey, 7); mdcContext = MDC.getContext(); assertEquals(1, mdcContext.size()); mdcContext.clear(); assertEquals(0, mdcContext.size()); log4jThreadContext.clear(); assertNull("get() after clear should return null", log4jThreadContext.get(testKey)); } }
@Test public void testValuesConvertedToStringsForExport() { ThreadContext context = new ThreadContext(); context.put("stringvalue", "floop"); context.put("intvalue", 99); Timeable t = new Timeable() { @Override public void setEventTimer(EventTimer timer) { } }; EventTimer timer = new EventTimer("foo", new MonitorableRegistry(), ThreadMetricSuite.blank(), false, false, Collections.<StepMeasurementSink>singletonList(new LoggerSink())); timer.registerTimeable(t, "foo"); timer.getCollector().startTiming(t, "stuff"); InProgressSnapshot snapshot = InProgressSnapshot.capture(timer, context); TabularData data = JmxInProgressMonitor.TO_TABULAR_DATA.apply(snapshot); assertEquals(SimpleType.STRING, data.getTabularType().getRowType().getType("stringvalue")); assertEquals(SimpleType.STRING, data.getTabularType().getRowType().getType("intvalue")); }
@Override public void run() { EventMetricCollector collector = timer.getCollector(); for (int i = 1; i < 30; i++) { try { context.put("Name", randomName()); context.put("Company", randomCompany()); collector.startTiming(this, action); doJob(i); collector.pauseForForward(); collector.startTiming(this, "frog"); collector.stopTiming(); collector.resumeAfterForward(); collector.stopTiming(); context.remove("Name"); context.remove("Company"); } catch (Exception e) { throw new RuntimeException(e); } } }
public void run() { context.put(testKey, testValue); finished.countDown(); } });
: context.allKeys(); keyedValues.put(contextEntry, String.valueOf(context.getForThread(entry .getKey(), contextEntry)));
public void testGetsEmptyContextForUnknownThread() { assertEquals(0, context.forThread(new Thread()).size()); }
public void testGetOfUnusedKeyReturnsNull() { final String testKey = "handy"; assertNull("get() of unused key should return null", context.get(testKey)); }
public void testCanAccessOtherThreadsContext() throws InterruptedException { final String testKey = "vanity"; final Object testValue = new Object(); final CountDownLatch finished = new CountDownLatch(1); final Thread otherThread = new Thread(new Runnable() { public void run() { context.put(testKey, testValue); finished.countDown(); } }); otherThread.start(); finished.await(); Map<String, Object> forThread = context.forThread(otherThread); assertEquals(1, forThread.size()); assertEquals(testValue, forThread.get(testKey)); }
/** * Factory method that creates a new ThreadContext initialised to also update SLF4J's MDC */ public static ThreadContext newSLF4JEnabledContext() { return new ThreadContext(new Slf4jMDCBridge()); }
public void setUp() { context = new ThreadContext(); Hashtable hashtable = MDC.getContext(); if (hashtable != null) { hashtable.clear(); } }
public static void main(String[] args) throws InterruptedException { ThreadMetricSuite suite = ThreadMetricSuite.blank(); suite.addMetric(StandardThreadMetrics.CLOCK_TIME); suite.addMetric(StandardThreadMetrics.BLOCKED_TIME); suite.addMetric(StandardThreadMetrics.WAITED_TIME); suite.addMetric(StandardThreadMetrics.USER_CPU_TIME); EventTimer timer = new EventTimer("blah", new MonitorableRegistry(), suite, true, true, Collections.<StepMeasurementSink>singletonList(new LoggerSink())); ThreadContext context = new ThreadContext(); EmailSender sender = new EmailSender(context); CheckoutBuyer buyer = new CheckoutBuyer(context); timer.registerTimeable(sender, "sendEmail"); timer.registerTimeable(buyer, "buySomething"); Thread t1 = new Thread(sender); Thread t2 = new Thread(buyer); t1.start(); t2.start(); InProgressExporter exporter = new InProgressExporter(timer, context); for (int i = 1; i <= 5; i++) { Thread.sleep(5000); InProgressSnapshot snapshot = exporter.getSnapshot(); System.out.println(snapshot.asFormattedString()); } t1.join(); t2.join(); }