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

Commit

Permalink
Merge pull request #141 from launchdarkly/eb/ch57521/newrelic-class
Browse files Browse the repository at this point in the history
fix NewRelic reflection logic to avoid shading
  • Loading branch information
eli-darkly authored Oct 15, 2019
2 parents 70fdb42 + 4ccd699 commit 241d6fd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.launchdarkly.shaded.com.newrelic.api.agent;

// Test to verify fix for https://github.com/launchdarkly/java-server-sdk/issues/171
public class NewRelic {
public static void addCustomParameter(String name, String value) {
System.out.println("NewRelic class reference was shaded! Test app loaded " + NewRelic.class.getName());
System.exit(1); // forces test failure
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.newrelic.api.agent;

// Test to verify fix for https://github.com/launchdarkly/java-server-sdk/issues/171
public class NewRelic {
public static void addCustomParameter(String name, String value) {
System.out.println("NewRelic class reference was correctly resolved without shading");
}
}
3 changes: 3 additions & 0 deletions packaging-test/test-app/src/main/java/testapp/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public static void main(String[] args) throws Exception {
// that provides its own copy of Gson).
JsonPrimitive x = new JsonPrimitive("x");

// Also do a flag evaluation, to ensure that it calls NewRelicReflector.annotateTransaction()
client.boolVariation("flag-key", new LDUser("user-key"), false);

System.out.println("@@@ successfully created LD client @@@");
}
}
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 241d6fd

Please sign in to comment.