Skip to content

Commit

Permalink
Fix latest dep tests 1.33.x (#11945)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Aug 15, 2024
1 parent 4872249 commit a74a6fe
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,27 @@ dependencies {
testInstrumentation(project(":instrumentation:netty:netty-4.1:javaagent"))
}

otelJava {
// AHC uses Unsafe and so does not run on later java version
maxJavaVersionForTests.set(JavaVersion.VERSION_1_8)
val latestDepTest = findProperty("testLatestDeps") as Boolean
val testJavaVersion =
gradle.startParameter.projectProperties["testJavaVersion"]?.let(JavaVersion::toVersion)
?: JavaVersion.current()

if (!latestDepTest) {
otelJava {
// AHC uses Unsafe and so does not run on later java version
maxJavaVersionForTests.set(JavaVersion.VERSION_1_8)
}
}

tasks.withType<Test>().configureEach {
systemProperty("testLatestDeps", latestDepTest)
// async-http-client 3.0 requires java 11
// We are not using minJavaVersionSupported for latestDepTest because that way the instrumentation
// gets compiled with java 11 when running latestDepTest. This causes play-mvc-2.4 latest dep tests
// to fail because they require java 8 and instrumentation compiled with java 11 won't apply.
if (latestDepTest && testJavaVersion.isJava8) {
enabled = false
}
}

tasks {
Expand All @@ -35,7 +53,7 @@ tasks {

// async-http-client 2.0.0 does not work with Netty versions newer than this due to referencing an
// internal file.
if (!(findProperty("testLatestDeps") as Boolean)) {
if (!latestDepTest) {
configurations.configureEach {
if (!name.contains("muzzle")) {
resolutionStrategy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
import java.lang.reflect.Method;
import java.net.URI;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.asynchttpclient.AsyncCompletionHandler;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
import org.asynchttpclient.Dsl;
import org.asynchttpclient.Request;
import org.asynchttpclient.RequestBuilder;
Expand All @@ -31,11 +34,26 @@ class AsyncHttpClientTest extends AbstractHttpClientTest<Request> {
private static final int CONNECTION_TIMEOUT_MS = (int) CONNECTION_TIMEOUT.toMillis();

// request timeout is needed in addition to connect timeout on async-http-client versions 2.1.0+
private static final AsyncHttpClient client =
Dsl.asyncHttpClient(
Dsl.config()
.setConnectTimeout(CONNECTION_TIMEOUT_MS)
.setRequestTimeout(CONNECTION_TIMEOUT_MS));
private static final AsyncHttpClient client = Dsl.asyncHttpClient(configureTimeout(Dsl.config()));

private static DefaultAsyncHttpClientConfig.Builder configureTimeout(
DefaultAsyncHttpClientConfig.Builder builder) {
setTimeout(builder, "setConnectTimeout", CONNECTION_TIMEOUT_MS);
setTimeout(builder, "setRequestTimeout", CONNECTION_TIMEOUT_MS);
return builder;
}

private static void setTimeout(
DefaultAsyncHttpClientConfig.Builder builder, String methodName, int timeout) {
boolean testLatestDeps = Boolean.getBoolean("testLatestDeps");
try {
Method method =
builder.getClass().getMethod(methodName, testLatestDeps ? Duration.class : int.class);
method.invoke(builder, testLatestDeps ? Duration.ofMillis(timeout) : timeout);
} catch (Exception exception) {
throw new IllegalStateException("Failed to set timeout " + methodName, exception);
}
}

@Override
public Request buildRequest(String method, URI uri, Map<String, String> headers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ class LettuceAsyncClientTest extends AbstractLettuceAsyncClientTest implements A
RedisClient createClient(String uri) {
return RedisClient.create(uri)
}

@Override
boolean connectHasSpans() {
Boolean.getBoolean("testLatestDeps")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ abstract class AbstractLettuceAsyncClientTest extends InstrumentationSpecificati
return true
}

boolean connectHasSpans() {
false
}

def <T> T runWithCallbackSpan(String spanName, Closure callback) {
if (testCallback()) {
return runWithSpan(spanName, callback)
Expand All @@ -117,6 +121,10 @@ abstract class AbstractLettuceAsyncClientTest extends InstrumentationSpecificati

then:
connection != null
if (connectHasSpans()) {
// ignore CLIENT SETINFO traces
ignoreTracesAndClear(2)
}
// Lettuce tracing does not trace connect
assertTraces(0) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
package io.opentelemetry.instrumentation.lettuce.v5_1

import io.lettuce.core.RedisClient
import io.lettuce.core.RedisCommandExecutionException
import io.opentelemetry.instrumentation.test.InstrumentationSpecification
import io.opentelemetry.semconv.SemanticAttributes
import org.testcontainers.containers.GenericContainer
import spock.lang.Shared

import static io.opentelemetry.api.trace.SpanKind.CLIENT
import static io.opentelemetry.api.trace.StatusCode.ERROR

abstract class AbstractLettuceSyncClientAuthTest extends InstrumentationSpecification {
public static final int DB_INDEX = 0
Expand Down Expand Up @@ -60,8 +62,40 @@ abstract class AbstractLettuceSyncClientAuthTest extends InstrumentationSpecific

expect:
res == "OK"
assertTraces(1) {
trace(0, 1) {
assertTraces(Boolean.getBoolean("testLatestDeps") ? 3 : 1) {
if (Boolean.getBoolean("testLatestDeps")) {
trace(0, 1) {
span(0) {
name "CLIENT"
kind CLIENT
status ERROR
attributes {
"$SemanticAttributes.NET_SOCK_PEER_ADDR" "127.0.0.1"
"$SemanticAttributes.NET_SOCK_PEER_NAME" expectedHostAttributeValue
"$SemanticAttributes.NET_SOCK_PEER_PORT" port
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "CLIENT SETINFO lib-name Lettuce"
}
errorEvent(RedisCommandExecutionException, "NOAUTH Authentication required.")
}
}
trace(1, 1) {
span(0) {
name "CLIENT"
kind CLIENT
status ERROR
attributes {
"$SemanticAttributes.NET_SOCK_PEER_ADDR" "127.0.0.1"
"$SemanticAttributes.NET_SOCK_PEER_NAME" expectedHostAttributeValue
"$SemanticAttributes.NET_SOCK_PEER_PORT" port
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" { it.startsWith("CLIENT SETINFO lib-ver") }
}
errorEvent(RedisCommandExecutionException, "NOAUTH Authentication required.")
}
}
}
trace(Boolean.getBoolean("testLatestDeps") ? 2 : 0, 1) {
span(0) {
name "AUTH"
kind CLIENT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -22,13 +21,6 @@
abstract class AbstractLettuceClientTest {
protected static final Logger logger = LoggerFactory.getLogger(AbstractLettuceClientTest.class);

@RegisterExtension
protected static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

public InstrumentationExtension getInstrumentationExtension() {
return testing;
}

@RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();

protected static final int DB_INDEX = 0;
Expand All @@ -40,15 +32,16 @@ public InstrumentationExtension getInstrumentationExtension() {
.waitingFor(Wait.forLogMessage(".*Ready to accept connections.*", 1));

protected static RedisClient redisClient;

protected static StatefulRedisConnection<String, String> connection;

protected abstract RedisClient createClient(String uri);

protected static String host;
protected static String ip;
protected static int port;
protected static String embeddedDbUri;

protected abstract RedisClient createClient(String uri);

protected abstract InstrumentationExtension getInstrumentationExtension();

protected ContainerConnection newContainerConnection() {
GenericContainer<?> server =
new GenericContainer<>("redis:6.2.3-alpine")
Expand Down
Loading

0 comments on commit a74a6fe

Please sign in to comment.