Skip to content

Commit

Permalink
Merge pull request #104 from GuanceCloud/liurui
Browse files Browse the repository at this point in the history
add rhinojs (version:1.7.0) #101
  • Loading branch information
lrwh authored Oct 30, 2024
2 parents fa997b0 + fae1011 commit 52b4f39
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
16 changes: 16 additions & 0 deletions dd-java-agent/instrumentation/rhino/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

muzzle {
pass {
group = "org.mozilla"
module = "rhino"
versions = "[1.7.0,)"
assertInverse = true
}
}

apply from: "$rootDir/gradle/java.gradle"

dependencies {
compileOnly group: 'org.mozilla', name: 'rhino', version: '1.7.10'

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package datadog.trace.instrumentation.rhino;

import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import net.bytebuddy.asm.Advice;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.instrumentation.rhino.RhinoDecorator.DECORATOR;

public class RhinoAdvice {


@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope execute( @Advice.AllArguments final Object[] args) {
String script = (String)args[1];
String scriptName = (String)args[2];

AgentSpan span = DECORATOR.createSpan(scriptName,script);
AgentScope agentScope = activateSpan(span);
return agentScope;
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exit(
@Advice.Enter final AgentScope scope, @Advice.Thrown final Throwable throwable) {
if (scope == null) {
return;
}
DECORATOR.onError(scope.span(),throwable);
DECORATOR.beforeFinish(scope.span());
scope.close();
scope.span().finish();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package datadog.trace.instrumentation.rhino;

import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.decorator.BaseDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;

public class RhinoDecorator extends BaseDecorator {
private static final Logger log = LoggerFactory.getLogger(RhinoDecorator.class);
public static final RhinoDecorator DECORATOR = new RhinoDecorator();

public static final String INSTRUMENTATION = "rhino";

@Override
protected String[] instrumentationNames() {
return new String[]{INSTRUMENTATION};
}

@Override
protected CharSequence spanType() {
return INSTRUMENTATION;
}

@Override
protected CharSequence component() {
return INSTRUMENTATION;
}

public AgentSpan createSpan(String operationName,String script) {
log.debug("--------------------- operationName:{},script:{}",operationName,script);
AgentSpan span = startSpan(operationName);
afterStart(span);
span.setResourceName(operationName);
span.setTag("script",script);
return span;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package datadog.trace.instrumentation.rhino;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.instrumentation.rhino.RhinoDecorator.INSTRUMENTATION;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPrivate;

@AutoService(InstrumenterModule.class)
public class RhinoInstrumentation extends InstrumenterModule.Tracing
implements Instrumenter.ForTypeHierarchy {

public RhinoInstrumentation() {
super(INSTRUMENTATION);
}

@Override
public String hierarchyMarkerType() {
return "org.mozilla.javascript.Context";
}

@Override
public ElementMatcher<TypeDescription> hierarchyMatcher() {
return named(hierarchyMarkerType());
}

@Override
public void methodAdvice(MethodTransformer transformer) {
transformer.applyAdvice(
isMethod()
.and(isPrivate())
.and(named("compileImpl")),
packageName + ".RhinoAdvice");
}

@Override
public String[] helperClassNames() {
return new String[]{
packageName + ".RhinoAdvice",
packageName + ".RhinoDecorator",
};
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ include ':dd-java-agent:instrumentation:rediscala-1.8.0'
include ':dd-java-agent:instrumentation:renaissance'
include ':dd-java-agent:instrumentation:resteasy-appsec'
include ':dd-java-agent:instrumentation:restlet-2.2'
include ':dd-java-agent:instrumentation:rhino'
include ':dd-java-agent:instrumentation:rmi'
include ':dd-java-agent:instrumentation:rxjava-1'
include ':dd-java-agent:instrumentation:rxjava-2'
Expand Down

0 comments on commit 52b4f39

Please sign in to comment.