Skip to content

Commit bb7e410

Browse files
authored
Merge pull request #6 from intergral/lazy_eval
feat(evaluator): make evaluators lazy
2 parents 6d4e265 + c11f707 commit bb7e410

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed

agent/src/main/java/com/intergral/deep/agent/tracepoint/evaluator/AbstractEvaluator.java renamed to agent-api/src/main/java/com/intergral/deep/agent/api/plugin/AbstractEvaluator.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
*/
1717

18-
package com.intergral.deep.agent.tracepoint.evaluator;
18+
package com.intergral.deep.agent.api.plugin;
1919

20-
import com.intergral.deep.agent.api.plugin.IEvaluator;
2120
import java.util.Map;
2221

22+
/**
23+
* This allows for common handling for object to boolean expressions.
24+
*/
2325
public abstract class AbstractEvaluator implements IEvaluator {
2426

2527
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) 2023 Intergral GmbH
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.intergral.deep.agent.api.plugin;
19+
20+
import java.util.Map;
21+
22+
/**
23+
* This type allows the evaluator to be loaded only if it is needed. ie. if we have no watches or conditions there is no need for an
24+
* evaluator.
25+
*/
26+
public class LazyEvaluator extends AbstractEvaluator {
27+
28+
private static final Exception NO_EVALUATOR_EXCEPTION = new RuntimeException(
29+
"No evaluator available.");
30+
private final IEvaluatorLoader loader;
31+
private IEvaluator evaluator;
32+
33+
public LazyEvaluator(final IEvaluatorLoader loader) {
34+
this.loader = loader;
35+
}
36+
37+
private IEvaluator load() {
38+
if (this.evaluator == null) {
39+
try {
40+
this.evaluator = this.loader.load();
41+
} catch (Exception e) {
42+
this.evaluator = new AbstractEvaluator() {
43+
@Override
44+
public Object evaluateExpression(final String expression, final Map<String, Object> values) throws Throwable {
45+
throw NO_EVALUATOR_EXCEPTION;
46+
}
47+
};
48+
}
49+
}
50+
return this.evaluator;
51+
}
52+
53+
@Override
54+
public Object evaluateExpression(final String expression, final Map<String, Object> values) throws Throwable {
55+
return load().evaluateExpression(expression, values);
56+
}
57+
58+
public interface IEvaluatorLoader {
59+
60+
IEvaluator load();
61+
}
62+
}

agent/src/main/java/com/intergral/deep/agent/tracepoint/cf/CFEvaluator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package com.intergral.deep.agent.tracepoint.cf;
1919

20-
import com.intergral.deep.agent.tracepoint.evaluator.AbstractEvaluator;
20+
import com.intergral.deep.agent.api.plugin.AbstractEvaluator;
2121
import java.lang.reflect.InvocationTargetException;
2222
import java.lang.reflect.Method;
2323
import java.util.Map;

agent/src/main/java/com/intergral/deep/agent/tracepoint/evaluator/NashornReflectEvaluator.java

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.intergral.deep.agent.tracepoint.evaluator;
1919

20+
import com.intergral.deep.agent.api.plugin.AbstractEvaluator;
2021
import com.intergral.deep.agent.api.plugin.IEvaluator;
2122
import java.lang.reflect.Method;
2223
import java.util.HashMap;

agent/src/main/java/com/intergral/deep/agent/tracepoint/handler/Callback.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.intergral.deep.agent.Utils;
2121
import com.intergral.deep.agent.api.plugin.IEvaluator;
22+
import com.intergral.deep.agent.api.plugin.LazyEvaluator;
2223
import com.intergral.deep.agent.push.PushService;
2324
import com.intergral.deep.agent.settings.Settings;
2425
import com.intergral.deep.agent.tracepoint.TracepointConfigService;
@@ -88,7 +89,7 @@ public static void callBackCF(final List<String> bpIds,
8889
final int lineNo,
8990
final Map<String, Object> variables) {
9091
try {
91-
final IEvaluator evaluator = CFUtils.findCfEval(variables);
92+
final IEvaluator evaluator = new LazyEvaluator(() -> CFUtils.findCfEval(variables));
9293
commonCallback(bpIds, filename, lineNo, variables, evaluator, CFFrameProcessor::new);
9394
} catch (Throwable t) {
9495
LOGGER.debug("Unable to process tracepoint {}:{}", filename, lineNo, t);
@@ -109,7 +110,7 @@ public static void callBack(final List<String> bpIds,
109110
final int lineNo,
110111
final Map<String, Object> variables) {
111112
try {
112-
final IEvaluator evaluator = EvaluatorService.createEvaluator();
113+
final IEvaluator evaluator = new LazyEvaluator(EvaluatorService::createEvaluator);
113114
commonCallback(bpIds, filename, lineNo, variables, evaluator, FrameProcessor::new);
114115
} catch (Throwable t) {
115116
LOGGER.debug("Unable to process tracepoint {}:{}", filename, lineNo, t);

0 commit comments

Comments
 (0)