Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed Jan 29, 2025
1 parent 143e071 commit e77a9d3
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public byte[] transform(

private boolean skipInstrumentation(ClassLoader loader, String classFilePath) {
if (definitionMatcher.isEmpty()) {
log.warn("No debugger definitions present.");
log.debug("No debugger definitions present.");
return true;
}
if (classFilePath == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public String getSourceFileName() {
public String getTypeName() {
return Strings.getClassName(classNode.name);
}

@Override
public String toString() {
return "MethodInfo{" + "classNode=" + classNode.name + ", methodNode=" + methodNode.desc + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,11 @@ public void buildLocation(MethodInfo methodInfo) {
}
this.location = new ProbeLocation(type, method, file, lines);
}

@Override
public String toString() {
return String.format(
"CodeOriginProbe{probeId=%s, entrySpanProbe=%s, signature=%s, location=%s}",
probeId, entrySpanProbe, signature, location);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import datadog.trace.bootstrap.debugger.DebuggerContext.CodeOriginRecorder
import com.google.common.util.concurrent.MoreExecutors
import datadog.trace.agent.test.naming.VersionedNamingTestBase
import datadog.trace.api.DDSpanTypes
Expand All @@ -13,17 +12,13 @@ import io.grpc.inprocess.InProcessChannelBuilder
import io.grpc.inprocess.InProcessServerBuilder
import io.grpc.stub.StreamObserver

import java.lang.reflect.Method
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicReference

import static datadog.trace.api.config.TraceInstrumentationConfig.*

abstract class GrpcCodeOriginTest extends VersionedNamingTestBase {
def codeOriginRecorder

@Override
final String service() {
return null
Expand Down Expand Up @@ -155,8 +150,7 @@ abstract class GrpcCodeOriginTest extends VersionedNamingTestBase {
}
}.flatten().sort()


assert codeOriginRecorder.invoked
assert DebuggerContext.codeOriginRecorder != null
assertTraces(2) {
trace((hasClientMessageSpans() ? clientMessageCount * serverMessageCount : 0) + 1) {
span {
Expand Down Expand Up @@ -247,26 +241,6 @@ abstract class GrpcCodeOriginTest extends VersionedNamingTestBase {
clientRange = 1..clientMessageCount
serverRange = 1..serverMessageCount
}


void codeOriginSetup() {
injectSysConfig(CODE_ORIGIN_FOR_SPANS_ENABLED, "true", true)
codeOriginRecorder = new CodeOriginRecorder() {
def invoked = false
@Override
String captureCodeOrigin(boolean entry) {
invoked = true
return "done"
}

@Override
String captureCodeOrigin(Method method, boolean entry) {
invoked = true
return "done"
}
}
DebuggerContext.initCodeOrigin(codeOriginRecorder)
}
}

class GrpcCodeOriginForkedTest extends GrpcCodeOriginTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ abstract class KafkaClientTestBase extends VersionedNamingTestBase {
super.configurePreAgent()

injectSysConfig("dd.kafka.e2e.duration.enabled", "true")
codeOriginSetup()
}

public static final LinkedHashMap<String, String> PRODUCER_PATHWAY_EDGE_TAGS
Expand Down Expand Up @@ -152,7 +153,7 @@ abstract class KafkaClientTestBase extends VersionedNamingTestBase {
}

@Flaky
public def "test kafka produce and consume"() {
def "test kafka produce and consume"() {
setup:
// Create and start a Kafka container using Testcontainers
KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:latest")).withEmbeddedZookeeper().withEnv("KAFKA_CREATE_TOPICS", SHARED_TOPIC)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ import datadog.trace.bootstrap.ActiveSubsystems
import datadog.trace.bootstrap.CallDepthThreadLocalMap
import datadog.trace.bootstrap.InstrumentationErrors
import datadog.trace.bootstrap.debugger.DebuggerContext
import datadog.trace.bootstrap.debugger.ProbeImplementation
import datadog.trace.bootstrap.instrumentation.api.AgentDataStreamsMonitoring
import datadog.trace.bootstrap.instrumentation.api.AgentSpan
import datadog.trace.bootstrap.instrumentation.api.AgentTracer
import datadog.trace.bootstrap.instrumentation.api.AgentTracer.TracerAPI
import datadog.trace.common.metrics.EventListener
import datadog.trace.common.metrics.Sink
Expand All @@ -53,6 +55,7 @@ import datadog.trace.core.DDSpan
import datadog.trace.core.PendingTrace
import datadog.trace.core.datastreams.DefaultDataStreamsMonitoring
import datadog.trace.test.util.DDSpecification
import datadog.trace.util.AgentTaskScheduler
import datadog.trace.util.Strings
import de.thetaphi.forbiddenapis.SuppressForbidden
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
Expand Down Expand Up @@ -83,8 +86,11 @@ import static datadog.communication.http.OkHttpUtils.buildHttpClient
import static datadog.trace.api.ConfigDefaults.DEFAULT_AGENT_HOST
import static datadog.trace.api.ConfigDefaults.DEFAULT_AGENT_TIMEOUT
import static datadog.trace.api.ConfigDefaults.DEFAULT_TRACE_AGENT_PORT
import static datadog.trace.api.config.DebuggerConfig.DEBUGGER_ENABLED
import static datadog.trace.api.config.DebuggerConfig.DEBUGGER_VERIFY_BYTECODE
import static datadog.trace.api.config.TraceInstrumentationConfig.CODE_ORIGIN_FOR_SPANS_ENABLED
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.closePrevious
import static datadog.trace.util.AgentThreadFactory.AgentThread.TASK_SCHEDULER
import static org.mockito.Mockito.mock
import static org.mockito.Mockito.when

Expand Down Expand Up @@ -303,37 +309,48 @@ abstract class AgentTestRunner extends DDSpecification implements AgentBuilder.L
((Logger) LoggerFactory.getLogger("org.testcontainers")).setLevel(Level.DEBUG)
}

void codeOriginSetup() {
def codeOriginSetup() {
injectSysConfig(CODE_ORIGIN_FOR_SPANS_ENABLED, "true", true)
injectSysConfig(DEBUGGER_ENABLED, "false", true)
injectSysConfig(DEBUGGER_VERIFY_BYTECODE, "false", true)

def configuration = Configuration.builder()
.setService("grpc code origin test")
.setService("code origin test")
.build()

def config = mock(Config.class)
when(config.isDebuggerEnabled()).thenReturn(true)
when(config.isDebuggerClassFileDumpEnabled()).thenReturn(true)
when(config.isDebuggerVerifyByteCode()).thenReturn(false)
when(config.getFinalDebuggerSnapshotUrl())
.thenReturn("http://localhost:8126/debugger/v1/input")
when(config.getFinalDebuggerSymDBUrl()).thenReturn("http://localhost:8126/symdb/v1/input")
when(config.getDebuggerCodeOriginMaxUserFrames()).thenReturn(8)
rebuildConfig()

def config = Config.get()
def probeStatusSink = mock(ProbeStatusSink.class)

def sink = new DebuggerSink(config, probeStatusSink)
def configurationUpdater = new ConfigurationUpdater(INSTRUMENTATION, DebuggerTransformer::new, config, sink, new ClassesToRetransformFinder())
ConfigurationUpdater configurationUpdater = new ConfigurationUpdater(INSTRUMENTATION, DebuggerTransformer::new, config, sink,
new ClassesToRetransformFinder()) {
@Override
ProbeImplementation resolve(String encodedProbeId) {
AgentSpan span = AgentTracer.activeSpan()
if (span != null) {
span.setTag("code origin span", true)
}

def currentTransformer = new DebuggerTransformer(config, configuration, {
ProbeDefinition definition, InstrumentationResult result ->
}, sink)
INSTRUMENTATION.addTransformer(currentTransformer)
return super.resolve(encodedProbeId)
}
}

INSTRUMENTATION.addTransformer(new DebuggerTransformer(config, configuration, {
ProbeDefinition definition, InstrumentationResult result ->
}, sink))
DebuggerContext.initProbeResolver(configurationUpdater)
DebuggerContext.initClassFilter(new DenyListHelper(null))
DebuggerContext.initValueSerializer(new JsonSnapshotSerializer())

DebuggerContext.initCodeOrigin(new DefaultCodeOriginRecorder(config, configurationUpdater))

DebuggerContext.initCodeOrigin(new DefaultCodeOriginRecorder(config, configurationUpdater, new AgentTaskScheduler(TASK_SCHEDULER) {
@Override
void execute(Runnable target) {
target.run()
}
}))
}

@SuppressForbidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import groovy.transform.stc.SimpleType

import java.util.regex.Pattern

import static datadog.trace.api.DDTags.DD_CODE_ORIGIN_FRAME
import static java.lang.String.format

class TagsAssert {
private final long spanParentId
private final Map<String, Object> tags
Expand Down Expand Up @@ -73,10 +76,26 @@ class TagsAssert {
assertedTags.add(DDTags.DSM_ENABLED)
assertedTags.add(DDTags.DJM_ENABLED)
assertedTags.add(DDTags.PARENT_ID)
assertedTags.add(DDTags.DD_CODE_ORIGIN_TYPE)
assertedTags.add(format(DD_CODE_ORIGIN_FRAME, 0, "file"))
assertedTags.add(format(DD_CODE_ORIGIN_FRAME, 0, "method"))
assertedTags.add(format(DD_CODE_ORIGIN_FRAME, 0, "line"))
assertedTags.add(format(DD_CODE_ORIGIN_FRAME, 0, "type"))
assertedTags.add(format(DD_CODE_ORIGIN_FRAME, 0, "signature"))
assertedTags.add("code origin span")

assert tags["thread.name"] != null
assert tags["thread.id"] != null

if ( tags["code origin span"] != null) {
assert tags[DDTags.DD_CODE_ORIGIN_TYPE] != null
assert tags[format(DD_CODE_ORIGIN_FRAME, 0, "file")] != null
assert tags[format(DD_CODE_ORIGIN_FRAME, 0, "method")] != null
assert tags[format(DD_CODE_ORIGIN_FRAME, 0, "line")] != null
assert tags[format(DD_CODE_ORIGIN_FRAME, 0, "type")] != null
assert tags[format(DD_CODE_ORIGIN_FRAME, 0, "signature")] != null
}

// FIXME: DQH - Too much conditional logic? Maybe create specialized methods for client & server cases

boolean isRoot = (DDSpanId.ZERO == spanParentId)
Expand Down

0 comments on commit e77a9d3

Please sign in to comment.