Search in sources :

Example 1 with Sampler

use of com.google.monitoring.runtime.instrumentation.Sampler in project logging-log4j2 by apache.

the class GcFreeLoggingTestUtil method executeLogging.

public static void executeLogging(final String configurationFile, final Class<?> testClass) throws Exception {
    System.setProperty("log4j2.enable.threadlocals", "true");
    System.setProperty("log4j2.enable.direct.encoders", "true");
    System.setProperty("log4j2.is.webapp", "false");
    System.setProperty("log4j.configurationFile", configurationFile);
    assertTrue("Constants.ENABLE_THREADLOCALS", Constants.ENABLE_THREADLOCALS);
    assertTrue("Constants.ENABLE_DIRECT_ENCODERS", Constants.ENABLE_DIRECT_ENCODERS);
    assertFalse("Constants.IS_WEB_APP", Constants.IS_WEB_APP);
    final MyCharSeq myCharSeq = new MyCharSeq();
    final Marker testGrandParent = MarkerManager.getMarker("testGrandParent");
    final Marker testParent = MarkerManager.getMarker("testParent").setParents(testGrandParent);
    // initial creation, value is cached
    final Marker test = MarkerManager.getMarker("test").setParents(testParent);
    // initialize LoggerContext etc.
    // This is not steady-state logging and will allocate objects.
    ThreadContext.put("aKey", "value1");
    ThreadContext.put("key2", "value2");
    final org.apache.logging.log4j.Logger logger = LogManager.getLogger(testClass.getName());
    logger.debug("debug not set");
    logger.fatal(test, "This message is logged to the console");
    logger.error("Sample error message");
    logger.error("Test parameterized message {}", "param");
    // initialize GelfLayout's messageStringBuilder
    logger.error(new MapMessage().with("eventId", "Login"));
    for (int i = 0; i < 256; i++) {
        // allocate MutableLogEvent.messageText
        logger.debug("ensure all ringbuffer slots have been used once");
    }
    ThreadContext.remove("aKey");
    ThreadContext.remove("key2");
    // BlockingWaitStrategy uses ReentrantLock which allocates Node objects. Ignore this.
    final String[] exclude = new String[] { //
    "java/util/concurrent/locks/AbstractQueuedSynchronizer$Node", //
    "com/google/monitoring/runtime/instrumentation/Sampler" };
    final AtomicBoolean samplingEnabled = new AtomicBoolean(true);
    final Sampler sampler = new Sampler() {

        @Override
        public void sampleAllocation(final int count, final String desc, final Object newObj, final long size) {
            if (!samplingEnabled.get()) {
                return;
            }
            for (int i = 0; i < exclude.length; i++) {
                if (exclude[i].equals(desc)) {
                    // exclude
                    return;
                }
            }
            System.err.println("I just allocated the object " + newObj + " of type " + desc + " whose size is " + size);
            if (count != -1) {
                System.err.println("It's an array of size " + count);
            }
            // show a stack trace to see which line caused allocation
            new RuntimeException().printStackTrace();
        }
    };
    Thread.sleep(500);
    final MapMessage mapMessage = new MapMessage().with("eventId", "Login");
    AllocationRecorder.addSampler(sampler);
    // now do some steady-state logging
    ThreadContext.put("aKey", "value1");
    ThreadContext.put("key2", "value2");
    final int ITERATIONS = 5;
    for (int i = 0; i < ITERATIONS; i++) {
        logger.error(myCharSeq);
        logger.error(MarkerManager.getMarker("test"), myCharSeq);
        logger.error("Test message");
        logger.error("Test parameterized message {}", "param");
        logger.error("Test parameterized message {}{}", "param", "param2");
        logger.error("Test parameterized message {}{}{}", "param", "param2", "abc");
        // LOG4J2-1683
        logger.error(mapMessage);
        ThreadContext.remove("aKey");
        ThreadContext.put("aKey", "value1");
    }
    Thread.sleep(50);
    // reliably ignore all allocations from now on
    samplingEnabled.set(false);
    AllocationRecorder.removeSampler(sampler);
    Thread.sleep(100);
}
Also used : MapMessage(org.apache.logging.log4j.message.MapMessage) Marker(org.apache.logging.log4j.Marker) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Sampler(com.google.monitoring.runtime.instrumentation.Sampler)

Aggregations

Sampler (com.google.monitoring.runtime.instrumentation.Sampler)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Marker (org.apache.logging.log4j.Marker)1 MapMessage (org.apache.logging.log4j.message.MapMessage)1