Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package datadog.trace.agent.test.assertions;

import static datadog.trace.agent.test.assertions.Matchers.assertValue;
import static datadog.trace.agent.test.assertions.Matchers.is;
import static datadog.trace.bootstrap.instrumentation.api.AgentSpanLink.DEFAULT_FLAGS;
import static datadog.trace.bootstrap.instrumentation.api.SpanAttributes.EMPTY;
import static datadog.trace.junit.utils.assertions.Matchers.assertValue;
import static datadog.trace.junit.utils.assertions.Matchers.is;

import datadog.trace.api.DDTraceId;
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
import datadog.trace.bootstrap.instrumentation.api.AgentSpanLink;
import datadog.trace.bootstrap.instrumentation.api.SpanAttributes;
import datadog.trace.core.DDSpan;
import datadog.trace.junit.utils.assertions.Matcher;
import datadog.trace.junit.utils.assertions.Matchers;

/**
* Provides matchers for span links based on their properties such as trace and span IDs links refer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package datadog.trace.agent.test.assertions;

import static datadog.trace.agent.test.assertions.Matchers.assertValue;
import static datadog.trace.agent.test.assertions.Matchers.is;
import static datadog.trace.agent.test.assertions.Matchers.isFalse;
import static datadog.trace.agent.test.assertions.Matchers.isNonNull;
import static datadog.trace.agent.test.assertions.Matchers.isNull;
import static datadog.trace.agent.test.assertions.Matchers.isTrue;
import static datadog.trace.agent.test.assertions.Matchers.matches;
import static datadog.trace.agent.test.assertions.Matchers.validates;
import static datadog.trace.core.DDSpanAccessor.spanLinks;
import static datadog.trace.junit.utils.assertions.Matchers.assertValue;
import static datadog.trace.junit.utils.assertions.Matchers.is;
import static datadog.trace.junit.utils.assertions.Matchers.isFalse;
import static datadog.trace.junit.utils.assertions.Matchers.isNonNull;
import static datadog.trace.junit.utils.assertions.Matchers.isNull;
import static datadog.trace.junit.utils.assertions.Matchers.isTrue;
import static datadog.trace.junit.utils.assertions.Matchers.matches;
import static datadog.trace.junit.utils.assertions.Matchers.validates;
import static java.time.Duration.ofNanos;
import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure;

import datadog.trace.api.DDTraceId;
import datadog.trace.api.TagMap;
import datadog.trace.bootstrap.instrumentation.api.AgentSpanLink;
import datadog.trace.core.DDSpan;
import datadog.trace.junit.utils.assertions.Any;
import datadog.trace.junit.utils.assertions.IsNull;
import datadog.trace.junit.utils.assertions.Matcher;
import datadog.trace.junit.utils.assertions.Matchers;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -45,6 +49,8 @@
* #durationLongerThan(Duration)}
* <li>span type with {@link #type(String)}
* <li>span error status with {@link #error()} and {@link #error(boolean)}
* <li>span measured status with {@link #measured()} and {@link #measured(boolean)}
* <li>span top-level status with {@link #topLevel()} and {@link #topLevel(boolean)}
* <li>span tags with {@link #tags(TagsMatcher...)}
* <li>span links with {@link #links(SpanLinkMatcher...)}
* </ul>
Expand All @@ -60,6 +66,8 @@ public final class SpanMatcher {
private Matcher<Duration> durationMatcher;
private Matcher<String> typeMatcher;
private Matcher<Boolean> errorMatcher;
private Matcher<Boolean> measuredMatcher;
private Matcher<Boolean> topLevelMatcher;
private TagsMatcher[] tagMatchers;
private SpanLinkMatcher[] linkMatchers;

Expand Down Expand Up @@ -300,6 +308,50 @@ public SpanMatcher error(boolean errored) {
return this;
}

/**
* Checks the span is measured.
*
* @return The current {@link SpanMatcher} instance updated with the specified measured
* constraint.
*/
public SpanMatcher measured() {
return measured(true);
}

/**
* Checks the span measured status matches the given value.
*
* @param measured The expected measured status.
* @return The current {@link SpanMatcher} instance updated with the specified measured
* constraint.
*/
public SpanMatcher measured(boolean measured) {
this.measuredMatcher = measured ? isTrue() : isFalse();
return this;
}

/**
* Checks the span is a top-level span.
*
* @return The current {@link SpanMatcher} instance updated with the specified top-level
* constraint.
*/
public SpanMatcher topLevel() {
return topLevel(true);
}

/**
* Checks the span top-level status matches the given value.
*
* @param topLevel The expected top-level status.
* @return The current {@link SpanMatcher} instance updated with the specified top-level
* constraint.
*/
public SpanMatcher topLevel(boolean topLevel) {
this.topLevelMatcher = topLevel ? isTrue() : isFalse();
return this;
}

public SpanMatcher tags(TagsMatcher... matchers) {
this.tagMatchers = matchers;
return this;
Expand Down Expand Up @@ -341,6 +393,8 @@ else if (this.parentIdMatcher == CHILD_OF_PREVIOUS_MATCHER) {
assertValue(this.durationMatcher, ofNanos(span.getDurationNano()), "Unexpected duration");
assertValue(this.typeMatcher, span.getSpanType(), "Unexpected span type");
assertValue(this.errorMatcher, span.isError(), "Unexpected error status");
assertValue(this.measuredMatcher, span.isMeasured(), "Unexpected measured status");
assertValue(this.topLevelMatcher, span.isTopLevel(), "Unexpected top-level status");
assertSpanTags(span.getTags());
assertSpanLinks(spanLinks(span));
}
Expand Down Expand Up @@ -399,7 +453,7 @@ private void assertSpanLinks(List<AgentSpanLink> links) {
.buildAndThrow();
}
for (int i = 0; i < expectedLinkCount; i++) {
SpanLinkMatcher linkMatcher = this.linkMatchers[expectedLinkCount];
SpanLinkMatcher linkMatcher = this.linkMatchers[i];
AgentSpanLink link = links.get(i);
linkMatcher.assertLink(link);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package datadog.trace.agent.test.assertions;

import static datadog.trace.agent.test.assertions.Matchers.any;
import static datadog.trace.agent.test.assertions.Matchers.is;
import static datadog.trace.agent.test.assertions.Matchers.isNonNull;
import static datadog.trace.api.DDTags.BASE_SERVICE;
import static datadog.trace.api.DDTags.DD_INTEGRATION;
import static datadog.trace.api.DDTags.DJM_ENABLED;
Expand All @@ -24,7 +21,13 @@
import static datadog.trace.api.DDTags.TRACER_HOST;
import static datadog.trace.common.sampling.RateByServiceTraceSampler.SAMPLING_AGENT_RATE;
import static datadog.trace.common.writer.ddagent.TraceMapper.SAMPLING_PRIORITY_KEY;
import static datadog.trace.core.DDSpanContext.SAMPLE_RATE_KEY;
import static datadog.trace.junit.utils.assertions.Matchers.any;
import static datadog.trace.junit.utils.assertions.Matchers.is;
import static datadog.trace.junit.utils.assertions.Matchers.isNonNull;

import datadog.trace.junit.utils.assertions.Matcher;
import datadog.trace.junit.utils.assertions.Matchers;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -43,7 +46,7 @@ public static TagsMatcher defaultTags() {
tagMatchers.put(LANGUAGE_TAG_KEY, any());
tagMatchers.put(SAMPLING_AGENT_RATE, any());
tagMatchers.put(SAMPLING_PRIORITY_KEY.toString(), any());
tagMatchers.put("_sample_rate", any());
tagMatchers.put(SAMPLE_RATE_KEY, any());
tagMatchers.put(PID_TAG, any());
tagMatchers.put(SCHEMA_VERSION_TAG_KEY, any());
tagMatchers.put(PROFILING_ENABLED, any());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package opentelemetry14;

import static datadog.trace.agent.test.assertions.Matchers.is;
import static datadog.trace.agent.test.assertions.SpanMatcher.span;
import static datadog.trace.agent.test.assertions.TagsMatcher.defaultTags;
import static datadog.trace.agent.test.assertions.TagsMatcher.tag;
Expand All @@ -9,6 +8,7 @@
import static datadog.trace.api.DDTags.DD_SVC_SRC;
import static datadog.trace.bootstrap.instrumentation.api.Tags.HTTP_STATUS;
import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND;
import static datadog.trace.junit.utils.assertions.Matchers.is;
import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.api.trace.SpanKind.CLIENT;
Expand All @@ -20,10 +20,10 @@
import static org.junit.jupiter.params.provider.Arguments.arguments;

import datadog.opentelemetry.shim.trace.OtelConventions;
import datadog.trace.agent.test.assertions.Matcher;
import datadog.trace.agent.test.assertions.Matchers;
import datadog.trace.agent.test.assertions.TagsMatcher;
import datadog.trace.bootstrap.instrumentation.api.ServiceNameSources;
import datadog.trace.junit.utils.assertions.Matcher;
import datadog.trace.junit.utils.assertions.Matchers;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanKind;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package opentelemetry14;

import static datadog.opentelemetry.shim.trace.OtelSpanEventTestHelper.stringifyErrorStack;
import static datadog.trace.agent.test.assertions.Matchers.any;
import static datadog.trace.agent.test.assertions.Matchers.is;
import static datadog.trace.agent.test.assertions.SpanMatcher.span;
import static datadog.trace.agent.test.assertions.TagsMatcher.defaultTags;
import static datadog.trace.agent.test.assertions.TagsMatcher.tag;
Expand All @@ -17,6 +15,8 @@
import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_CONSUMER;
import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_PRODUCER;
import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_SERVER;
import static datadog.trace.junit.utils.assertions.Matchers.any;
import static datadog.trace.junit.utils.assertions.Matchers.is;
import static io.opentelemetry.api.common.AttributeKey.booleanArrayKey;
import static io.opentelemetry.api.common.AttributeKey.doubleArrayKey;
import static io.opentelemetry.api.common.AttributeKey.longArrayKey;
Expand All @@ -43,15 +43,15 @@
import static org.junit.jupiter.params.provider.Arguments.arguments;

import datadog.opentelemetry.shim.trace.OtelSpanEvent;
import datadog.trace.agent.test.assertions.Matcher;
import datadog.trace.agent.test.assertions.Matchers;
import datadog.trace.agent.test.assertions.TagsMatcher;
import datadog.trace.api.DDSpanId;
import datadog.trace.api.DDTags;
import datadog.trace.api.DDTraceId;
import datadog.trace.api.time.ControllableTimeSource;
import datadog.trace.bootstrap.instrumentation.api.WithAgentSpan;
import datadog.trace.core.DDSpan;
import datadog.trace.junit.utils.assertions.Matcher;
import datadog.trace.junit.utils.assertions.Matchers;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package testdog.trace.instrumentation.rxjava3;

import static datadog.trace.agent.test.assertions.Matchers.validates;
import static datadog.trace.agent.test.assertions.SpanMatcher.span;
import static datadog.trace.agent.test.assertions.TagsMatcher.defaultTags;
import static datadog.trace.agent.test.assertions.TagsMatcher.tag;
import static datadog.trace.agent.test.assertions.TraceMatcher.SORT_BY_START_TIME;
import static datadog.trace.agent.test.assertions.TraceMatcher.trace;
import static datadog.trace.junit.utils.assertions.Matchers.validates;
import static org.junit.jupiter.api.Assertions.assertEquals;

import datadog.trace.agent.test.AbstractInstrumentationTest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package testdog.trace.instrumentation.rxjava3;

import static datadog.trace.agent.test.assertions.Matchers.validates;
import static datadog.trace.agent.test.assertions.SpanMatcher.span;
import static datadog.trace.agent.test.assertions.TagsMatcher.defaultTags;
import static datadog.trace.agent.test.assertions.TagsMatcher.error;
import static datadog.trace.agent.test.assertions.TagsMatcher.tag;
import static datadog.trace.agent.test.assertions.TraceMatcher.trace;
import static datadog.trace.junit.utils.assertions.Matchers.validates;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package testdog.trace.instrumentation.rxjava3;

import static datadog.trace.agent.test.assertions.Matchers.validates;
import static datadog.trace.agent.test.assertions.SpanMatcher.span;
import static datadog.trace.agent.test.assertions.TagsMatcher.defaultTags;
import static datadog.trace.agent.test.assertions.TagsMatcher.error;
Expand All @@ -10,6 +9,7 @@
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.junit.utils.assertions.Matchers.validates;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down
3 changes: 2 additions & 1 deletion dd-trace-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ dependencies {
testImplementation group: 'com.amazonaws', name: 'aws-lambda-java-events', version:'3.11.0'
testImplementation group: 'com.google.protobuf', name: 'protobuf-java', version: '3.14.0'
testImplementation libs.testcontainers
testImplementation project(':utils:junit-utils')
testImplementation project(':utils:test-junit-utils')
testImplementation project(':utils:test-junit-converter-utils')

traceAgentTestImplementation libs.testcontainers
}
Expand Down
9 changes: 6 additions & 3 deletions dd-trace-ot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ dependencies {
implementation(project(":dd-trace-ot:correlation-id-injection"))

testImplementation(project(":dd-java-agent:testing"))
testImplementation(project(":utils:junit-utils"))
testImplementation(project(":utils:test-junit-utils"))
testImplementation(project(":utils:test-junit-converter-utils"))
testImplementation(libs.bundles.mockito)

add("ot31CompatibilityTestImplementation", project(":utils:junit-utils"))
add("ot33CompatibilityTestImplementation", project(":utils:junit-utils"))
add("ot31CompatibilityTestImplementation", project(":utils:test-junit-utils"))
add("ot31CompatibilityTestImplementation", project(":utils:test-junit-converter-utils"))
add("ot33CompatibilityTestImplementation", project(":utils:test-junit-utils"))
add("ot33CompatibilityTestImplementation", project(":utils:test-junit-converter-utils"))

// Kotlin accessors not generated if not coming from plugin
add("ot33CompatibilityTestImplementation", "io.opentracing:opentracing-api") {
Expand Down
2 changes: 1 addition & 1 deletion docs/how_to_test_with_junit.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ When a cell references a symbolic constant like `Long.MAX_VALUE` or `DDSpanId.MA
Prefer a shared class so converters are reused across tests:

```java
// utils/junit-utils - shared across modules
// utils/test-junit-converter-utils - shared across modules
public final class TableTestTypeConverters {
@TypeConverter
public static long toLong(String value) {
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ include(
":dd-java-agent:testing",
":utils:config-utils",
":utils:container-utils",
":utils:junit-utils",
":utils:test-junit-utils",
":utils:test-junit-converter-utils",
":utils:filesystem-utils",
":utils:flare-utils",
":utils:logging-utils",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ plugins {
apply(from = "$rootDir/gradle/java.gradle")

dependencies {
api(libs.forbiddenapis)
api(project(":components:environment"))

implementation(project(":dd-trace-api"))
implementation(project(":internal-api"))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This is a Gradle generated file for dependency locking.
# Manual edits can break the build and are not advised.
# This file is expected to be part of source control.
# To regenerate this file, run: ./gradlew :utils:junit-utils:dependencies --write-locks
# To regenerate this file, run: ./gradlew :utils:test-junit-converter-utils:dependencies --write-locks
ch.qos.logback:logback-classic:1.2.13=testCompileClasspath,testRuntimeClasspath
ch.qos.logback:logback-core:1.2.13=testCompileClasspath,testRuntimeClasspath
com.datadoghq:dd-javac-plugin-client:0.2.2=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
Expand All @@ -14,7 +14,7 @@ com.google.code.gson:gson:2.13.2=spotbugs
com.google.errorprone:error_prone_annotations:2.41.0=spotbugs
com.thoughtworks.qdox:qdox:1.12.1=codenarc
commons-io:commons-io:2.20.0=spotbugs
de.thetaphi:forbiddenapis:3.10=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
de.thetaphi:forbiddenapis:3.10=compileClasspath
io.leangen.geantyref:geantyref:1.3.16=testRuntimeClasspath
jaxen:jaxen:2.0.0=spotbugs
net.bytebuddy:byte-buddy-agent:1.12.8=testRuntimeClasspath
Expand All @@ -27,7 +27,7 @@ org.apache.commons:commons-lang3:3.19.0=spotbugs
org.apache.commons:commons-text:1.14.0=spotbugs
org.apache.logging.log4j:log4j-api:2.25.2=spotbugs
org.apache.logging.log4j:log4j-core:2.25.2=spotbugs
org.apiguardian:apiguardian-api:1.1.2=compileClasspath,testCompileClasspath
org.apiguardian:apiguardian-api:1.1.2=compileClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-ant:3.0.23=codenarc
org.codehaus.groovy:groovy-docgenerator:3.0.23=codenarc
org.codehaus.groovy:groovy-groovydoc:3.0.23=codenarc
Expand Down
12 changes: 12 additions & 0 deletions utils/test-junit-utils/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
`java-library`
}

apply(from = "$rootDir/gradle/java.gradle")

dependencies {
api(libs.forbiddenapis)
api(project(":components:environment"))

compileOnly(libs.junit.jupiter)
}
Loading