All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mantledillusion.metrics.trail.Slf4JMetricsTrailSupport Maven / Gradle / Ivy

There is a newer version: 2.2.2
Show newest version
package com.mantledillusion.metrics.trail;

import org.slf4j.MDC;

/**
 * Support for adding a {@link Thread} {@link MetricsTrail}'s ID to the SLF4J {@link MDC} using the key {@link #DEFAULT_KEY}.
 * 

* Can be activated using {@link #activatePublishToMdc()} on applications startup. */ public class Slf4JMetricsTrailSupport { private static class MDCTrailListener implements MetricsTrailListener { private String key = DEFAULT_KEY; @Override public void announce(MetricsTrail trail, EventType eventType) throws Exception { if (eventType.isTrailActive()) { MDC.put(key, trail.getTrailId().toString()); } else { MDC.remove(key); } } } private static final MDCTrailListener METRICS_TRAIL_LISTENER = new MDCTrailListener(); public static final String DEFAULT_KEY = "correlationId"; private Slf4JMetricsTrailSupport() {} /** * Will cause every {@link Thread} based {@link MetricsTrail}'s ID to automatically be added to SLF4J's {@link MDC} * when the trail is begun and removed once the trail ends. *

* Uses the {@value #DEFAULT_KEY} as MDC key. */ public static void activatePublishToMdc() { activatePublishToMdc(DEFAULT_KEY); } /** * Will cause every {@link Thread} based {@link MetricsTrail}'s ID to automatically be added to SLF4J's {@link MDC} * when the trail is begun and removed once the trail ends. * * @param mdcKey The key to use in the MDC for the trail ID; might not be null. */ public static void activatePublishToMdc(String mdcKey) { if (mdcKey == null) { throw new IllegalArgumentException("Cannot put the trail into the MDC using a null key"); } synchronized (METRICS_TRAIL_LISTENER) { METRICS_TRAIL_LISTENER.key = mdcKey; MetricsTrailSupport.addListener(METRICS_TRAIL_LISTENER, MetricsTrailListener.ReferenceMode.WEAK); } } /** * Will stop {@link Thread} based {@link MetricsTrail}'s ID to added to SLF4J's {@link MDC}. */ public static void deactivatePublishToMdc() { synchronized (METRICS_TRAIL_LISTENER) { MetricsTrailSupport.removeListener(METRICS_TRAIL_LISTENER); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy