-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Appsec: create top span for process executions
- Loading branch information
1 parent
520f781
commit 9d457e2
Showing
17 changed files
with
512 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
ext { | ||
minJavaVersionForTests = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
muzzle { | ||
pass { | ||
coreJdk() | ||
} | ||
} | ||
|
||
apply from: "$rootDir/gradle/java.gradle" |
42 changes: 42 additions & 0 deletions
42
...ang/src/main/java/datadog/trace/instrumentation/java/lang/ProcessImplInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package datadog.trace.instrumentation.java.lang; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.api.Platform; | ||
import java.util.Map; | ||
|
||
@AutoService(Instrumenter.class) | ||
public class ProcessImplInstrumentation extends Instrumenter.Tracing | ||
implements Instrumenter.ForSingleType, Instrumenter.ForBootstrap { | ||
|
||
public ProcessImplInstrumentation() { | ||
super("java-lang-appsec"); | ||
} | ||
|
||
@Override | ||
public String instrumentedType() { | ||
return "java.lang.ProcessImpl"; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return Platform.isJavaVersionAtLeast(8) && super.isEnabled(); | ||
} | ||
|
||
@Override | ||
public void adviceTransformations(AdviceTransformation transformation) { | ||
transformation.applyAdvice( | ||
named("start") | ||
.and( | ||
takesArguments( | ||
String[].class, | ||
Map.class, | ||
String.class, | ||
ProcessBuilder.Redirect[].class, | ||
boolean.class)), | ||
packageName + ".ProcessImplStartAdvice"); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...a-lang/src/main/java8/datadog/trace/instrumentation/java/lang/ProcessImplStartAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package datadog.trace.instrumentation.java.lang; | ||
|
||
import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentTracer; | ||
import datadog.trace.bootstrap.instrumentation.api.TagContext; | ||
import datadog.trace.bootstrap.instrumentation.api8.java.lang.ProcessImplInstrumentationHelpers; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
class ProcessImplStartAdvice { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static AgentSpan startSpan(@Advice.Argument(0) final String[] command) throws IOException { | ||
if (!ProcessImplInstrumentationHelpers.ONLINE) { | ||
return null; | ||
} | ||
|
||
if (command.length == 0) { | ||
return null; | ||
} | ||
|
||
AgentTracer.TracerAPI tracer = AgentTracer.get(); | ||
|
||
Map<String, String> tags = ProcessImplInstrumentationHelpers.createTags(command); | ||
TagContext tagContext = new TagContext("appsec", tags); | ||
AgentSpan span = tracer.startSpan("command_execution", tagContext, true); | ||
span.setSpanType("system"); | ||
span.setResourceName(ProcessImplInstrumentationHelpers.determineResource(command)); | ||
span.startThreadMigration(); | ||
return span; | ||
} | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void endSpan( | ||
@Advice.Return Process p, @Advice.Enter AgentSpan span, @Advice.Thrown Throwable t) { | ||
if (span == null) { | ||
return; | ||
} | ||
if (t != null) { | ||
span.finishThreadMigration(); | ||
span.setError(true); | ||
span.setErrorMessage(t.getMessage()); | ||
span.finish(); | ||
return; | ||
} | ||
|
||
ProcessImplInstrumentationHelpers.addProcessCompletionHook(p, span); | ||
} | ||
} |
Oops, something went wrong.