Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
fix NewRelic reflection logic to avoid shading
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-darkly committed Oct 15, 2019
1 parent ab4cfea commit 4ccd699
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/main/java/com/launchdarkly/client/NewRelicReflector.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.launchdarkly.client;

import com.google.common.base.Joiner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -13,17 +15,24 @@ final class NewRelicReflector {

private static final Logger logger = LoggerFactory.getLogger(NewRelicReflector.class);


static {
try {
newRelic = Class.forName("com.newrelic.api.agent.NewRelic");
newRelic = Class.forName(getNewRelicClassName());
addCustomParameter = newRelic.getDeclaredMethod("addCustomParameter", String.class, String.class);
} catch (ClassNotFoundException | NoSuchMethodException e) {
logger.info("No NewRelic agent detected");
}
}

static void annotateTransaction(String featureKey, String value) {
static String getNewRelicClassName() {
// This ungainly logic is a workaround for the overly aggressive behavior of the Shadow plugin, which
// will transform any class or package names passed to Class.forName() if they are string literals;
// it will even transform the string "com".
String com = Joiner.on("").join(new String[] { "c", "o", "m" });
return Joiner.on(".").join(new String[] { com, "newrelic", "api", "agent", "NewRelic" });
}

static void annotateTransaction(String featureKey, String value) {
if (addCustomParameter != null) {
try {
addCustomParameter.invoke(null, featureKey, value);
Expand All @@ -32,6 +41,5 @@ static void annotateTransaction(String featureKey, String value) {
logger.debug(e.toString(), e);
}
}
}

}
}

0 comments on commit 4ccd699

Please sign in to comment.