From bca5cd582908a671b6fee377c63a6bedd49cc656 Mon Sep 17 00:00:00 2001 From: Felix Barnsteiner Date: Mon, 25 Sep 2017 17:37:55 +0200 Subject: [PATCH] Add dynamic link for byte-buddy-agent in exception message relates to #326 --- .../core/instrument/AgentAttacher.java | 11 ++++++- .../stagemonitor/core/util/VersionUtils.java | 24 ++++++++++++++ .../core/util/VersionUtilsTest.java | 32 +++++++++++++++++++ .../filter/HttpRequestMonitorFilter.java | 5 +-- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 stagemonitor-core/src/main/java/org/stagemonitor/core/util/VersionUtils.java create mode 100644 stagemonitor-core/src/test/java/org/stagemonitor/core/util/VersionUtilsTest.java diff --git a/stagemonitor-core/src/main/java/org/stagemonitor/core/instrument/AgentAttacher.java b/stagemonitor-core/src/main/java/org/stagemonitor/core/instrument/AgentAttacher.java index 93aab2d4..6674f8c5 100644 --- a/stagemonitor-core/src/main/java/org/stagemonitor/core/instrument/AgentAttacher.java +++ b/stagemonitor-core/src/main/java/org/stagemonitor/core/instrument/AgentAttacher.java @@ -16,6 +16,7 @@ import org.stagemonitor.core.Stagemonitor; import org.stagemonitor.core.metrics.health.ImmediateResult; import org.stagemonitor.core.util.ClassUtils; +import org.stagemonitor.core.util.VersionUtils; import org.stagemonitor.util.IOUtils; import java.io.File; @@ -122,13 +123,21 @@ private static boolean initInstrumentation() { final String msg = "Failed to perform runtime attachment of the stagemonitor agent. Make sure that you run your " + "application with a JDK (not a JRE)." + "To make stagemonitor work with a JRE, you have to add the following command line argument to the " + - "start of the JVM: -javaagent:/path/to/byte-buddy-agent-.jar"; + "start of the JVM: -javaagent:/path/to/byte-buddy-agent-.jar. " + + "The version of the agent depends on the version of stagemonitor. " + + "You can download the appropriate agent for the stagemonitor version you are using here: " + getByteBuddyAgentDownloadUrl(); healthCheckRegistry.register("Agent attachment", ImmediateResult.of(HealthCheck.Result.unhealthy(msg))); logger.warn(msg, e); return false; } } + private static String getByteBuddyAgentDownloadUrl() { + final String groupId = "net.bytebuddy"; + final String byteBuddyVersion = VersionUtils.getVersionFromPomProperties(ByteBuddy.class, groupId, "byte-buddy"); + return VersionUtils.getMavenCentralDownloadLink(groupId, "byte-buddy-agent", byteBuddyVersion); + } + private static void ensureDispatcherIsAppendedToBootstrapClasspath(Instrumentation instrumentation) throws ClassNotFoundException, IOException { final ClassLoader bootstrapClassloader = ClassLoader.getSystemClassLoader().getParent(); diff --git a/stagemonitor-core/src/main/java/org/stagemonitor/core/util/VersionUtils.java b/stagemonitor-core/src/main/java/org/stagemonitor/core/util/VersionUtils.java new file mode 100644 index 00000000..334baa21 --- /dev/null +++ b/stagemonitor-core/src/main/java/org/stagemonitor/core/util/VersionUtils.java @@ -0,0 +1,24 @@ +package org.stagemonitor.core.util; + +import org.stagemonitor.configuration.source.PropertyFileConfigurationSource; + +import java.util.Properties; + +public final class VersionUtils { + + private VersionUtils() { + } + + public static String getVersionFromPomProperties(Class clazz, String groupId, String artifactId) { + final String classpathLocation = "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties"; + final Properties pomProperties = PropertyFileConfigurationSource.getFromClasspath(classpathLocation, clazz.getClassLoader()); + if (pomProperties != null) { + return pomProperties.getProperty("version"); + } + return null; + } + + public static String getMavenCentralDownloadLink(String groupId, String artifactId, String version) { + return String.format("http://central.maven.org/maven2/%s/%s/%s/%s-%s.jar", groupId.replace('.', '/'), artifactId, version, artifactId, version); + } +} diff --git a/stagemonitor-core/src/test/java/org/stagemonitor/core/util/VersionUtilsTest.java b/stagemonitor-core/src/test/java/org/stagemonitor/core/util/VersionUtilsTest.java new file mode 100644 index 00000000..06ef3ef1 --- /dev/null +++ b/stagemonitor-core/src/test/java/org/stagemonitor/core/util/VersionUtilsTest.java @@ -0,0 +1,32 @@ +package org.stagemonitor.core.util; + +import net.bytebuddy.ByteBuddy; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class VersionUtilsTest { + + private static final String BYTEBUDDY_GROUP = "net.bytebuddy"; + private static final String VERSION_PATTERN = "\\d\\.\\d+\\.\\d+"; + + @Test + public void getVersionFromPomProperties() throws Exception { + assertThat(VersionUtils.getVersionFromPomProperties(ByteBuddy.class, BYTEBUDDY_GROUP, "byte-buddy")).matches(VERSION_PATTERN); + } + + @Test + public void testGetMavenCentralDownloadLink() throws Exception { + assertThat(VersionUtils.getMavenCentralDownloadLink(BYTEBUDDY_GROUP, "byte-buddy-agent", "1.7.5")) + .isEqualTo("http://central.maven.org/maven2/net/bytebuddy/byte-buddy-agent/1.7.5/byte-buddy-agent-1.7.5.jar"); + } + + @Test + public void testByteBuddyDownloadUrl() throws Exception { + final String byteBuddyVersion = VersionUtils.getVersionFromPomProperties(ByteBuddy.class, BYTEBUDDY_GROUP, "byte-buddy"); + final String mavenCentralDownloadLink = VersionUtils.getMavenCentralDownloadLink(BYTEBUDDY_GROUP, "byte-buddy-agent", byteBuddyVersion); + assertThat(mavenCentralDownloadLink) + .isEqualTo("http://central.maven.org/maven2/net/bytebuddy/byte-buddy-agent/" + byteBuddyVersion + "/byte-buddy-agent-" + byteBuddyVersion + ".jar"); + } +} diff --git a/stagemonitor-web-servlet/src/main/java/org/stagemonitor/web/servlet/filter/HttpRequestMonitorFilter.java b/stagemonitor-web-servlet/src/main/java/org/stagemonitor/web/servlet/filter/HttpRequestMonitorFilter.java index 3600e800..90796707 100644 --- a/stagemonitor-web-servlet/src/main/java/org/stagemonitor/web/servlet/filter/HttpRequestMonitorFilter.java +++ b/stagemonitor-web-servlet/src/main/java/org/stagemonitor/web/servlet/filter/HttpRequestMonitorFilter.java @@ -103,11 +103,12 @@ private void doMonitor(HttpServletRequest request, HttpServletResponse response, try { monitorRequest(filterChain, request, responseWrapper); + } catch (Exception e) { + handleException(e); + } finally { if (isInjectContentToHtml(request)) { injectHtml(response, request, httpServletResponseBufferWrapper); } - } catch (Exception e) { - handleException(e); } }