Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Add dynamic link for byte-buddy-agent in exception message
Browse files Browse the repository at this point in the history
relates to #326
  • Loading branch information
Felix Barnsteiner committed Sep 25, 2017
1 parent 22ca415 commit bca5cd5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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-<version>.jar";
"start of the JVM: -javaagent:/path/to/byte-buddy-agent-<version>.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();
Expand Down
@@ -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);
}
}
@@ -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");
}
}
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit bca5cd5

Please sign in to comment.