diff --git a/drools-docs/src/modules/ROOT/pages/language-reference/_performance-tuning-drl-ref.adoc b/drools-docs/src/modules/ROOT/pages/language-reference/_performance-tuning-drl-ref.adoc index ac5f4627fd3..a4368270773 100644 --- a/drools-docs/src/modules/ROOT/pages/language-reference/_performance-tuning-drl-ref.adoc +++ b/drools-docs/src/modules/ROOT/pages/language-reference/_performance-tuning-drl-ref.adoc @@ -143,6 +143,9 @@ Alternatively, you can use `drools-metric` to expose the data using https://asci Metrics.addRegitry(new JmxMeterRegistry(s -> null, Clock.SYSTEM)); ---- ++ +If you want to use logging instead of Micrometer even though Micrometer is available in classpath, you can disable it by setting the system property `drools.metric.micrometer.disabled` to `true`. + + Regardless of whether you want to use logging or Micrometer, you need to enable `MetricLogUtils` by setting the system property `drools.metric.logger.enabled` to `true`. Optionally, you can change the microseconds threshold of metric reporting by setting the `drools.metric.logger.threshold` system property. diff --git a/drools-metric/src/main/java/org/drools/metric/util/MetricLogUtils.java b/drools-metric/src/main/java/org/drools/metric/util/MetricLogUtils.java index 5eef523bf5b..a66bdd83efd 100644 --- a/drools-metric/src/main/java/org/drools/metric/util/MetricLogUtils.java +++ b/drools-metric/src/main/java/org/drools/metric/util/MetricLogUtils.java @@ -30,6 +30,11 @@ public class MetricLogUtils { public static final String METRIC_LOGGER_ENABLED = "drools.metric.logger.enabled"; private boolean enabled = Boolean.parseBoolean(getConfig(METRIC_LOGGER_ENABLED, "false")); + + // Set true when you want to disable Micrometer even if it is available. + public static final String METRIC_MICROMETER_DISABLED = "drools.metric.micrometer.disabled"; + private boolean micrometerDisabled = Boolean.parseBoolean(getConfig(METRIC_MICROMETER_DISABLED, "false")); + private boolean micrometerAvailable = isMicrometerAvailable(); public static final String METRIC_LOGGER_THRESHOLD = "drools.metric.logger.threshold"; @@ -37,7 +42,7 @@ public class MetricLogUtils { private final ThreadLocal nodeStats = new ThreadLocal<>(); - private static final MetricLogUtils INSTANCE = new MetricLogUtils(); + private static MetricLogUtils INSTANCE = new MetricLogUtils(); private static boolean isMicrometerAvailable() { try { @@ -92,7 +97,7 @@ public void logAndEndMetrics() { long elapsedTimeInNanos = (System.nanoTime() - stats.getStartTime()); long elapsedTimeInMicro = elapsedTimeInNanos / 1000; if (evalCount > 0 && elapsedTimeInMicro > threshold) { - if (micrometerAvailable) { + if (micrometerAvailable && !micrometerDisabled) { MicrometerUtils.INSTANCE.triggerMicrometer(stats.getNode(), evalCount, elapsedTimeInNanos); } else { // Only log when Micrometer is not enabled. logger.trace("{}, evalCount:{}, elapsedMicro:{}", stats.getNode(), evalCount, elapsedTimeInMicro); @@ -105,4 +110,10 @@ public void logAndEndMetrics() { } } + /* + * This method is only used for testing purposes. + */ + public static void recreateInstance() { + MetricLogUtils.INSTANCE = new MetricLogUtils(); + } } diff --git a/drools-metric/src/test/java/org/drools/metric/AbstractMetricTest.java b/drools-metric/src/test/java/org/drools/metric/AbstractMetricTest.java index e09541f5ec3..94afb25f899 100644 --- a/drools-metric/src/test/java/org/drools/metric/AbstractMetricTest.java +++ b/drools-metric/src/test/java/org/drools/metric/AbstractMetricTest.java @@ -47,6 +47,4 @@ public void clearMeters() { // Remove meters we inserted without affecting those MicrometerUtils.INSTANCE.clear(); registry = null; } - - } diff --git a/drools-metric/src/test/java/org/drools/metric/MetricLogUtilsTest.java b/drools-metric/src/test/java/org/drools/metric/MetricLogUtilsTest.java index fc9c9edd275..3262031bbec 100644 --- a/drools-metric/src/test/java/org/drools/metric/MetricLogUtilsTest.java +++ b/drools-metric/src/test/java/org/drools/metric/MetricLogUtilsTest.java @@ -26,6 +26,7 @@ import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.Timer; import io.micrometer.core.instrument.search.Search; +import org.drools.metric.util.MetricLogUtils; import org.drools.mvel.compiler.Address; import org.drools.mvel.compiler.Person; import org.junit.Test; @@ -39,6 +40,24 @@ public class MetricLogUtilsTest extends AbstractMetricTest { @Test public void testJoin() { + runJoinRules(); + + // 2 nodes expected + Collection timers = Search.in(registry) + .name("org.drools.metric.elapsed.time.per.evaluation") + .timers(); + assertThat(timers).hasSize(2); + Collection timers2 = Search.in(registry) + .name("org.drools.metric.elapsed.time") + .timers(); + assertThat(timers2).hasSize(2); + Collection counters = Search.in(registry) + .name("org.drools.metric.evaluation.count") + .counters(); + assertThat(counters).hasSize(2); + } + + private void runJoinRules() { String str = "import " + Address.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + @@ -67,20 +86,26 @@ public void testJoin() { int fired = ksession.fireAllRules(); ksession.dispose(); assertThat(fired).isEqualTo(36); + } - // 2 nodes expected - Collection timers = Search.in(registry) - .name("org.drools.metric.elapsed.time.per.evaluation") - .timers(); - assertThat(timers).hasSize(2); - Collection timers2 = Search.in(registry) - .name("org.drools.metric.elapsed.time") - .timers(); - assertThat(timers2).hasSize(2); - Collection counters = Search.in(registry) - .name("org.drools.metric.evaluation.count") - .counters(); - assertThat(counters).hasSize(2); + @Test + public void micrometerDisabled() { + + try { + System.setProperty(MetricLogUtils.METRIC_MICROMETER_DISABLED, "true"); + MetricLogUtils.recreateInstance(); + + runJoinRules(); + + // Micrometer is disabled + Collection timers = Search.in(registry) + .name("org.drools.metric.elapsed.time.per.evaluation") + .timers(); + assertThat(timers).isEmpty(); + } finally { + System.clearProperty(MetricLogUtils.METRIC_MICROMETER_DISABLED); // default is false + MetricLogUtils.recreateInstance(); + } } @Test diff --git a/drools-metric/src/test/resources/logback-test.xml b/drools-metric/src/test/resources/logback-test.xml index 87319242399..90bb41ab1db 100644 --- a/drools-metric/src/test/resources/logback-test.xml +++ b/drools-metric/src/test/resources/logback-test.xml @@ -30,7 +30,7 @@ - +