Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[incubator-kie-drools-6053] MetricLogUtils forcibly uses Micrometer w… #6054

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ 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";
private int threshold = Integer.parseInt(getConfig(METRIC_LOGGER_THRESHOLD, "500")); // microseconds

private final ThreadLocal<NodeStats> nodeStats = new ThreadLocal<>();

private static final MetricLogUtils INSTANCE = new MetricLogUtils();
private static MetricLogUtils INSTANCE = new MetricLogUtils();

private static boolean isMicrometerAvailable() {
try {
Expand Down Expand Up @@ -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);
Expand All @@ -105,4 +110,10 @@ public void logAndEndMetrics() {
}
}

/*
* This method is only used for testing purposes.
*/
public static void recreateInstance() {
MetricLogUtils.INSTANCE = new MetricLogUtils();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,4 @@ public void clearMeters() { // Remove meters we inserted without affecting those
MicrometerUtils.INSTANCE.clear();
registry = null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +40,24 @@ public class MetricLogUtilsTest extends AbstractMetricTest {
@Test
public void testJoin() {

runJoinRules();

// 2 nodes expected
Collection<Timer> timers = Search.in(registry)
.name("org.drools.metric.elapsed.time.per.evaluation")
.timers();
assertThat(timers).hasSize(2);
Collection<Timer> timers2 = Search.in(registry)
.name("org.drools.metric.elapsed.time")
.timers();
assertThat(timers2).hasSize(2);
Collection<Counter> 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" +
Expand Down Expand Up @@ -67,20 +86,26 @@ public void testJoin() {
int fired = ksession.fireAllRules();
ksession.dispose();
assertThat(fired).isEqualTo(36);
}

// 2 nodes expected
Collection<Timer> timers = Search.in(registry)
.name("org.drools.metric.elapsed.time.per.evaluation")
.timers();
assertThat(timers).hasSize(2);
Collection<Timer> timers2 = Search.in(registry)
.name("org.drools.metric.elapsed.time")
.timers();
assertThat(timers2).hasSize(2);
Collection<Counter> 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<Timer> 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
Expand Down
2 changes: 1 addition & 1 deletion drools-metric/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<logger name="org.kie" level="warn"/>
<logger name="org.drools" level="warn"/>

<!-- <logger name="org.drools.metric.util.MetricLogUtils" level="trace"/> -->
<logger name="org.drools.metric.util.MetricLogUtils" level="trace"/>

<root level="warn">
<appender-ref ref="consoleAppender" />
Expand Down
Loading