Skip to content

Commit

Permalink
NR-134815 Treat OTel WithSpan annotation as Trace (#1841)
Browse files Browse the repository at this point in the history
* NR-134815 Treat OTel WithSpan annotation as Trace

* Update TraceAnnotationTest.java
  • Loading branch information
sdaubin authored Apr 5, 2024
1 parent 87cc68b commit c3a1f53
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions functional_test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ dependencies {
implementation(project(":agent-bridge-datastore"))
implementation(project(":newrelic-weaver"))

// WithSpan annotation
implementation('io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:1.33.1')

testImplementation(project(":functional_test:weave_test_impl"))

// the newrelic-agent test classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package test.newrelic.test.agent;

import com.newrelic.agent.Agent;
import com.newrelic.agent.AgentHelper;
import com.newrelic.agent.MetricNames;
import com.newrelic.agent.Transaction;
Expand All @@ -29,6 +30,7 @@
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Token;
import com.newrelic.api.agent.Trace;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -267,6 +269,19 @@ public void testDoubleArrayArgs() throws Exception {
AgentHelper.verifyMetrics(metrics, MessageFormat.format("Custom/{0}/doubleArrayArg", Simple.class.getName()));
}

@Test
public void testOTelWithSpan() {
withSpan();

Set<String> metrics = AgentHelper.getMetrics();
AgentHelper.verifyMetrics(metrics, MessageFormat.format("Custom/{0}/withSpan", Simple.class.getName()));
}

@Trace(dispatcher = true)
static void withSpan() {
new Simple().withSpan();
}

@Trace(dispatcher = true)
private void callDoubleArray() {
new Simple().doubleArrayArg(new String[0][0]);
Expand Down Expand Up @@ -305,7 +320,12 @@ private void callCharArray() {
new Simple().charArray(new char[] { 6 });
}

private class Simple {
private static class Simple {
@WithSpan
void withSpan() {
Agent.LOG.info("withSpan");
}

@Trace
private void foo() throws InterruptedException {
Thread.sleep(200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ final class ClassTransformerConfigImpl extends BaseConfig implements ClassTransf
private static final String SYSTEM_PROPERTY_ROOT = "newrelic.config.class_transformer.";

static final String NEW_RELIC_TRACE_TYPE_DESC = "Lcom/newrelic/api/agent/Trace;";
static final String OTEL_WITH_SPAN_TYPE_DESC = "Lio/opentelemetry/instrumentation/annotations/WithSpan;";
static final String DEPRECATED_NEW_RELIC_TRACE_TYPE_DESC = "Lcom/newrelic/agent/Trace;";

// as of JAVA-4824 the yml config file is not required, but still need to match old behavior
Expand Down Expand Up @@ -186,6 +187,7 @@ private AnnotationMatcher initializeTraceAnnotationMatcher(Map<?, ?> props) {
List<AnnotationMatcher> matchers = new ArrayList<>();
matchers.add(new ClassNameAnnotationMatcher(Type.getType(DEPRECATED_NEW_RELIC_TRACE_TYPE_DESC).getDescriptor()));
matchers.add(new ClassNameAnnotationMatcher(Type.getType(NEW_RELIC_TRACE_TYPE_DESC).getDescriptor()));
matchers.add(new ClassNameAnnotationMatcher(Type.getType(OTEL_WITH_SPAN_TYPE_DESC).getDescriptor()));

final Collection<String> traceAnnotationClassNames = getUniqueStrings("trace_annotation_class_name");
if (traceAnnotationClassNames.isEmpty()) {
Expand Down

0 comments on commit c3a1f53

Please sign in to comment.