diff --git a/instrumentation/vaadin-14.2/javaagent/build.gradle.kts b/instrumentation/vaadin-14.2/javaagent/build.gradle.kts index e6c2eb00d93a..096ee9f17c05 100644 --- a/instrumentation/vaadin-14.2/javaagent/build.gradle.kts +++ b/instrumentation/vaadin-14.2/javaagent/build.gradle.kts @@ -49,6 +49,10 @@ tasks { } usesService(gradle.sharedServices.registrations["testcontainersBuildService"].getService()) } + + named("latestDepTest") { + jvmArgs("-Dotel.instrumentation.vaadin.test-mode=true") + } } dependencies { diff --git a/instrumentation/vaadin-14.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vaadin/NodeUpdaterInstrumentation.java b/instrumentation/vaadin-14.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vaadin/NodeUpdaterInstrumentation.java new file mode 100644 index 000000000000..4c65ad5e1a05 --- /dev/null +++ b/instrumentation/vaadin-14.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vaadin/NodeUpdaterInstrumentation.java @@ -0,0 +1,49 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.vaadin; + +import static net.bytebuddy.matcher.ElementMatchers.named; + +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import java.util.Map; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; + +/** + * This advice is used only when running tests. Works around an issue running testLatestDeps. + * Currently the latest vaadin release is 20, it is likely that this advice can be deleted when + * there is a new version. + */ +public class NodeUpdaterInstrumentation implements TypeInstrumentation { + + @Override + public ElementMatcher typeMatcher() { + return named("com.vaadin.flow.server.frontend.NodeUpdater"); + } + + @Override + public void transform(TypeTransformer transformer) { + transformer.applyAdviceToMethod( + named("getDefaultDevDependencies"), + NodeUpdaterInstrumentation.class.getName() + "$FixDependenciesAdvice"); + } + + @SuppressWarnings("unused") + public static class FixDependenciesAdvice { + + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) + public static void onExit(@Advice.Return Map dependencies) { + // If there is a dependency to workbox-webpack-plugin:6.1.0 add a dependency to + // workbox-build:6.1.0. This is needed because workbox-build:6.2.0 that gets chosen without + // having an explicit dependency to workbox-build here is incompatible with 6.1.0. + if ("6.1.0".equals(dependencies.get("workbox-webpack-plugin"))) { + dependencies.put("workbox-build", "6.1.0"); + } + } + } +} diff --git a/instrumentation/vaadin-14.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vaadin/VaadinInstrumentationModule.java b/instrumentation/vaadin-14.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vaadin/VaadinInstrumentationModule.java index c680f9739f14..41ddd9e90fac 100644 --- a/instrumentation/vaadin-14.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vaadin/VaadinInstrumentationModule.java +++ b/instrumentation/vaadin-14.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vaadin/VaadinInstrumentationModule.java @@ -9,13 +9,18 @@ import static java.util.Arrays.asList; import com.google.auto.service.AutoService; +import io.opentelemetry.instrumentation.api.config.Config; import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import java.util.ArrayList; import java.util.List; import net.bytebuddy.matcher.ElementMatcher; @AutoService(InstrumentationModule.class) public class VaadinInstrumentationModule extends InstrumentationModule { + // special flag set only when running tests + private static final boolean TEST_MODE = + Config.get().getBooleanProperty("otel.instrumentation.vaadin.test-mode", false); public VaadinInstrumentationModule() { super("vaadin", "vaadin-14.2"); @@ -29,13 +34,19 @@ public ElementMatcher.Junction classLoaderMatcher() { @Override public List typeInstrumentations() { - return asList( - new VaadinServiceInstrumentation(), - new RequestHandlerInstrumentation(), - new UiInstrumentation(), - new RouterInstrumentation(), - new JavaScriptBootstrapUiInstrumentation(), - new RpcInvocationHandlerInstrumentation(), - new ClientCallableRpcInstrumentation()); + List instrumentations = + asList( + new VaadinServiceInstrumentation(), + new RequestHandlerInstrumentation(), + new UiInstrumentation(), + new RouterInstrumentation(), + new JavaScriptBootstrapUiInstrumentation(), + new RpcInvocationHandlerInstrumentation(), + new ClientCallableRpcInstrumentation()); + if (TEST_MODE) { + instrumentations = new ArrayList<>(instrumentations); + instrumentations.add(new NodeUpdaterInstrumentation()); + } + return instrumentations; } }