Skip to content

Commit

Permalink
Fail test if advice threw an exception (#9654)
Browse files Browse the repository at this point in the history
Co-authored-by: Trask Stalnaker <[email protected]>
  • Loading branch information
laurit and trask authored Oct 11, 2023
1 parent b7df46d commit 0de1dcf
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ dependencies {
library("com.amazonaws:aws-lambda-java-core:1.0.0")

testImplementation(project(":instrumentation:aws-lambda:aws-lambda-core-1.0:testing"))
testInstrumentation(project(":instrumentation:aws-lambda:aws-lambda-events-2.2:javaagent"))
}

tasks.withType<Test>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@

package io.opentelemetry.javaagent.instrumentation.awslambdacore.v1_0;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static java.util.Collections.singletonList;
import static net.bytebuddy.matcher.ElementMatchers.not;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import java.util.List;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(InstrumentationModule.class)
public class AwsLambdaInstrumentationModule extends InstrumentationModule {
public AwsLambdaInstrumentationModule() {
super("aws-lambda-core", "aws-lambda-core-1.0", "aws-lambda");
}

@Override
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
// aws-lambda-events-2.2 is used when SQSEvent is present
return not(hasClassesNamed("com.amazonaws.services.lambda.runtime.events.SQSEvent"));
}

@Override
public boolean isHelperClass(String className) {
return className.startsWith("io.opentelemetry.contrib.awsxray.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import net.bytebuddy.asm.Advice;
Expand Down Expand Up @@ -61,7 +62,12 @@ public void transform(TypeTransformer transformer) {
public static class ConstructorMapAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(@Advice.Argument(0) Map<String, Object> config) {
public static void onEnter(
@Advice.Argument(value = 0, readOnly = false) Map<String, Object> config) {
// ensure config is a mutable map
if (config.getClass() != HashMap.class) {
config = new HashMap<>(config);
}
enhanceConfig(config);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import net.bytebuddy.asm.Advice;
Expand Down Expand Up @@ -57,7 +58,12 @@ public void transform(TypeTransformer transformer) {
public static class ConstructorMapAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(@Advice.Argument(0) Map<String, Object> config) {
public static void onEnter(
@Advice.Argument(value = 0, readOnly = false) Map<String, Object> config) {
// ensure config is a mutable map
if (config.getClass() != HashMap.class) {
config = new HashMap<>(config);
}
enhanceConfig(config);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public static void removeHandler(
VirtualField.find(ChannelHandler.class, ChannelHandler.class);
ChannelHandler ourHandler = virtualField.get(handler);
if (ourHandler != null) {
pipeline.remove(ourHandler);
if (pipeline.context(ourHandler) != null) {
pipeline.remove(ourHandler);
}
virtualField.set(handler, null);
}
}
Expand All @@ -92,7 +94,9 @@ public static void removeHandler(
VirtualField.find(ChannelHandler.class, ChannelHandler.class);
ChannelHandler ourHandler = virtualField.get(handler);
if (ourHandler != null) {
pipeline.remove(ourHandler);
if (pipeline.context(ourHandler) != null) {
pipeline.remove(ourHandler);
}
virtualField.set(handler, null);
}
}
Expand All @@ -114,7 +118,9 @@ public static void removeHandler(
VirtualField.find(ChannelHandler.class, ChannelHandler.class);
ChannelHandler ourHandler = virtualField.get(handler);
if (ourHandler != null) {
pipeline.remove(ourHandler);
if (pipeline.context(ourHandler) != null) {
pipeline.remove(ourHandler);
}
virtualField.set(handler, null);
}
}
Expand All @@ -130,7 +136,9 @@ public static void removeHandler(
VirtualField.find(ChannelHandler.class, ChannelHandler.class);
ChannelHandler ourHandler = virtualField.get(handler);
if (ourHandler != null) {
pipeline.remove(ourHandler);
if (pipeline.context(ourHandler) != null) {
pipeline.remove(ourHandler);
}
virtualField.set(handler, null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public String extract(ServletRequestContext<REQUEST> requestContext) {
if (!knownMethods.contains(method)) {
method = "HTTP";
}
if (servletPath.isEmpty()) {
if (servletPath == null || servletPath.isEmpty()) {
return method;
}
String contextPath = accessor.getRequestContextPath(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static java.util.logging.Level.FINE;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

/**
Expand All @@ -17,11 +18,18 @@
public final class ExceptionLogger {

private static final Logger logger = Logger.getLogger(ExceptionLogger.class.getName());
private static final AtomicInteger counter = new AtomicInteger();

/** See {@code io.opentelemetry.javaagent.tooling.ExceptionHandlers} for usages. */
@SuppressWarnings("unused")
public static void logSuppressedError(String message, Throwable error) {
logger.log(FINE, message, error);
counter.incrementAndGet();
}

// only used by tests
public static int getAndReset() {
return counter.getAndSet(0);
}

private ExceptionLogger() {}
Expand Down
1 change: 1 addition & 0 deletions testing-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies {
api("org.slf4j:slf4j-api")

compileOnly(project(":testing:armeria-shaded-for-testing", configuration = "shadow"))
compileOnly(project(":javaagent-bootstrap"))

compileOnly("com.google.auto.value:auto-value-annotations")
annotationProcessor("com.google.auto.value:auto-value")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public void afterTestClass() {
assert TestAgentListenerAccess.getInstrumentationErrorCount() == 0
: TestAgentListenerAccess.getInstrumentationErrorCount()
+ " Instrumentation errors during test";
int adviceFailureCount = TestAgentListenerAccess.getAndResetAdviceFailureCount();
assert adviceFailureCount == 0 : adviceFailureCount + " Advice failures during test";
int muzzleFailureCount = TestAgentListenerAccess.getAndResetMuzzleFailureCount();
assert muzzleFailureCount == 0 : muzzleFailureCount + " Muzzle failures during test";
// additional library ignores are ignored during tests, because they can make it really
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static java.lang.invoke.MethodType.methodType;

import io.opentelemetry.javaagent.bootstrap.ExceptionLogger;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.util.List;
Expand Down Expand Up @@ -105,5 +106,9 @@ public static void addSkipErrorCondition(BiFunction<String, Throwable, Boolean>
}
}

public static int getAndResetAdviceFailureCount() {
return ExceptionLogger.getAndReset();
}

private TestAgentListenerAccess() {}
}

0 comments on commit 0de1dcf

Please sign in to comment.