Skip to content

Commit

Permalink
Allow specification of rootNameFilter via regular expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaroslav Tulach committed Apr 25, 2020
1 parent c81c81c commit cb82ab2
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 25 deletions.
2 changes: 1 addition & 1 deletion tools/docs/Insight-Embedding.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ For example following script is going to observe who calls `process.exit`:
```bash
$ curl --data \
'insight.on("enter", (ctx, frame) => { console.log(new Error("call to exit").stack); }, \
{ roots: true, rootNameFilter: n => n === "exit" });' \
{ roots: true, rootNameFilter: "exit" });' \
-X POST http://localhost:9999/
```

Expand Down
12 changes: 6 additions & 6 deletions tools/docs/Insight-Manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ insight.on('enter', function(ctx, frame) {
print('fib for ' + frame.n);
}, {
roots: true,
rootNameFilter: (name) => 'fib' === name
rootNameFilter: 'fib'
});
```

Expand Down Expand Up @@ -458,7 +458,7 @@ insight.on('enter', (ev, frame) => {
}
}, {
roots: true,
rootNameFilter: (n) => n === 'log'
rootNameFilter: 'log'
});
```

Expand Down Expand Up @@ -521,7 +521,7 @@ insight.on('enter', function(ctx, frame) {
}
}, {
roots: true,
rootNameFilter: (n) => n === 'nextNatural'
rootNameFilter: 'nextNatural'
});
```

Expand Down Expand Up @@ -585,7 +585,7 @@ insight.on('enter', (ctx, frame) => {
}
}, {
roots: true,
rootNameFilter: (name) => name === 'Filter'
rootNameFilter: 'Filter'
});

insight.on('return', (ctx, frame) => {
Expand All @@ -594,7 +594,7 @@ insight.on('return', (ctx, frame) => {
max = 0;
}, {
roots: true,
rootNameFilter: (name) => name === 'measure'
rootNameFilter: 'measure'
});
```

Expand Down Expand Up @@ -622,7 +622,7 @@ insight.on('enter', (ctx, frame) => {
}
}, {
roots: true,
rootNameFilter: (name) => name === 'Filter'
rootNameFilter: 'Filter'
});
```

Expand Down
6 changes: 3 additions & 3 deletions tools/docs/Insight-Tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let initialize = function(tracer) {
console.log(`agent: handling #${res.id} request for ${req.url}`);
}, {
roots: true,
rootNameFilter: name => name === 'emit',
rootNameFilter: 'emit',
sourceFilter: src => src.name === 'events.js'
});

Expand All @@ -50,7 +50,7 @@ let initialize = function(tracer) {
}
}, {
roots: true,
rootNameFilter: name => name === 'end',
rootNameFilter: 'end',
sourceFilter: src => src.name === '_http_outgoing.js'
});
console.log('agent: ready');
Expand Down Expand Up @@ -118,7 +118,7 @@ let initializeJaeger = function (ctx, frame) {

insight.on('return', initializeJaeger, {
roots: true,
rootNameFilter: name => name === 'jaegerAvailable'
rootNameFilter: 'jaegerAvailable'
});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class AgentObjectFactory extends ProxyLanguage {

static InsightAPI.OnConfig createConfig(
boolean expressions, boolean statements, boolean roots,
Predicate<String> rootNameFilter, Predicate<SourceInfo> sourceFilter) {
String rootNameFilter, Predicate<SourceInfo> sourceFilter) {
InsightAPI.OnConfig config = new InsightAPI.OnConfig();
config.expressions = expressions;
config.statements = statements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void onEnterCallbackWithFilterOnRootName() throws Exception {
agentAPI.on("enter", (ctx, frame) -> {
assertNull("No function entered yet", functionName[0]);
functionName[0] = ctx.name();
}, AgentObjectFactory.createConfig(false, false, true, (name) -> "foo".equals(name), null));
}, AgentObjectFactory.createConfig(false, false, true, "foo", null));
agentAPI.on("close", () -> {
finished[0] = true;
});
Expand Down Expand Up @@ -587,7 +587,7 @@ public void accessFrameVariables() throws Exception {
assertTrue(names.isEmpty());
names.addAll(frame.keySet());
};
agentAPI.on("enter", captureNames, createConfig(true, false, false, (name) -> "mul".equals(name), null));
agentAPI.on("enter", captureNames, createConfig(true, false, false, "mul.*", null));
c.eval(sampleScript);
agentAPI.off("enter", captureNames);

Expand All @@ -597,7 +597,7 @@ public void accessFrameVariables() throws Exception {
agentAPI.on("enter", (ctx, frame) -> {
values[0] = frame.get("a");
values[1] = frame.get("b");
}, AgentObjectFactory.createConfig(true, false, false, (name) -> "mul".equals(name), null));
}, AgentObjectFactory.createConfig(true, false, false, "mul", null));

Value mul = c.getBindings(InstrumentationTestLanguage.ID).getMember("mul");
assertNotNull("mul function found", mul);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ class OnConfig {
public boolean expressions;
public boolean statements;
public boolean roots;
public Predicate<String> rootNameFilter;

/** String with a regular expression to match name of functions.
* Prior to version 0.6 this had to be a
* {@code Function<String,Boolean>}.
*/
public String rootNameFilter;
/* @since 0.4 */
public Predicate<SourceInfo> sourceFilter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,14 @@ private static SourceSectionFilter createFilter(AgentObject obj, Object[] args)
try {
Object fn = iop.readMember(config, "rootNameFilter");
if (fn != null && !iop.isNull(fn)) {
if (!iop.isExecutable(fn)) {
throw new IllegalArgumentException("rootNameFilter has to be a function!");
if (iop.isString(fn)) {
builder.rootNameIs(new RegexNameFilter(iop.asString(fn)));
} else {
if (!iop.isExecutable(fn)) {
throw new IllegalArgumentException("rootNameFilter should be a string, a regular expression!");
}
builder.rootNameIs(new RootNameFilter(fn));
}
builder.rootNameIs(new RootNameFilter(fn));
}
} catch (UnknownIdentifierException ex) {
// OK
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.tools.agentscript.impl;

import com.oracle.truffle.api.CompilerDirectives;
import java.util.function.Predicate;
import java.util.regex.Pattern;

final class RegexNameFilter implements Predicate<String> {
private final Pattern regex;

RegexNameFilter(String fn) {
this.regex = Pattern.compile(fn);
}

@CompilerDirectives.TruffleBoundary
@Override
public boolean test(String rootName) {
if (rootName == null) {
return false;
}
return regex.matcher(rootName).matches();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ private Insight() {
*
* @since 20.1
*/
public static final String VERSION = "0.5";
public static final String VERSION = "0.6";

}
2 changes: 1 addition & 1 deletion vm/tests/all/agentscript/EmbeddingDoubled.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void main(String... args) throws Exception {
+ "}\n"
+ "insight.on('return', fibreturn, {\n"
+ " roots: true,\n"
+ " rootNameFilter: (n) => n.indexOf('fib') >= 0\n"
+ " rootNameFilter: '.*fib.*'\n"
+ "});\n"
+ "\n";

Expand Down
2 changes: 1 addition & 1 deletion vm/tests/all/agentscript/agent-exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ insight.on('return', function checkLogging(ev, frame) {
}
}, {
roots: true,
rootNameFilter: (n) => n === 'log'
rootNameFilter: 'log'
});
4 changes: 2 additions & 2 deletions vm/tests/all/agentscript/agent-exception.test
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ insight: Unknown attribute misnamedAttribute
>[1] js --jvm --experimental-options --insight=agent-error3.js log.js
insight: Unknown event type 'enterOrLeave'.*
>[7] js --jvm --experimental-options --insight=agent-error.js log.js
Error while initializing {id: "insight", version: "0.5"}
Error while initializing {id: "insight", version: "0.6"}
.*at <js> :anonymous.*agent-error.js.*
>[7] js --experimental-options --insight=agent-error.js log.js
Error while initializing {id: "insight", version: "0.5"}
Error while initializing {id: "insight", version: "0.6"}
.*at <js> :anonymous.*agent-error.js.*
>[7] js --jvm --experimental-options --insight=agent-error4.js -f log.js
Error loading of source log.js.*
Expand Down
2 changes: 1 addition & 1 deletion vm/tests/all/agentscript/agent-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ insight.on('enter', function(ctx, frame) {
}
}, {
roots: true,
rootNameFilter: (n) => n === 'nextNatural'
rootNameFilter: 'n...Natural'
});

2 changes: 1 addition & 1 deletion vm/tests/all/agentscript/agent-opentracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let initializeTracer = function (tracer) {
console.log(`agent: handling #${res.id} request for ${req.url}`);
}, {
roots: true,
rootNameFilter: name => name === 'emit',
rootNameFilter: 'emit',
sourceFilter: src => src.name === 'events.js'
});

Expand Down

0 comments on commit cb82ab2

Please sign in to comment.