Skip to content

Commit 2e179f0

Browse files
committed
found place to override Java Agent Vertx Transaction Name
1 parent c2b925a commit 2e179f0

File tree

4 files changed

+160
-41
lines changed

4 files changed

+160
-41
lines changed
Lines changed: 101 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package com.newrelic.instrumentation.quarkus.resteasy.reactive;
22

33
import java.util.Map;
4+
import java.util.logging.Level;
45

6+
import javax.ws.rs.core.UriInfo;
7+
8+
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;
9+
import org.jboss.resteasy.reactive.server.mapping.RuntimeResource;
10+
import org.jboss.resteasy.reactive.server.mapping.URITemplate;
11+
12+
import com.newrelic.api.agent.Logger;
513
import com.newrelic.api.agent.NewRelic;
614
import com.newrelic.api.agent.TransactionNamePriority;
715

@@ -10,45 +18,106 @@
1018

1119
public class Utils {
1220

13-
public static final String NEWRELIC_TOKEN = "NewRelic-Token";
21+
public static final String NEWRELIC_TOKEN = "NewRelic-Token";
1422

15-
public static void addAttribute(Map<String, Object> attributes, String key, Object value) {
16-
if (attributes != null && key != null && !key.isEmpty() && value != null) {
17-
attributes.put(key, value);
23+
public static void addAttribute(Map<String, Object> attributes, String key, Object value) {
24+
if (attributes != null && key != null && !key.isEmpty() && value != null) {
25+
attributes.put(key, value);
26+
}
1827
}
19-
}
2028

21-
public static void addRoute(Map<String, Object> attributes, Route route) {
22-
if (route != null) {
23-
addAttribute(attributes, "Route-Name", route.getName());
24-
addAttribute(attributes, "Route-Path", route.getPath());
29+
public static void addRuntimeResource(Map<String,Object> attributes, RuntimeResource resource) {
30+
if(resource != null) {
31+
Logger logger = NewRelic.getAgent().getLogger();
32+
addAttribute(attributes, "RuntimeResource-HttpMethod", resource.getHttpMethod());
33+
logger.log(Level.FINE, "adding RuntimeResource-HttpMethod: {0}", resource.getHttpMethod());
34+
addAttribute(attributes, "RuntimeResource-JavaMethodName", resource.getJavaMethodName());
35+
logger.log(Level.FINE, "adding RuntimeResource-JavaMethodName: {0}", resource.getJavaMethodName());
36+
URITemplate template = resource.getPath();
37+
logger.log(Level.FINE, "adding RuntimeResource-Path: {0}", resource.getPath());
38+
addAttribute(attributes, "RuntimeResource-Path", template != null ? template.toString() : "null");
39+
template = resource.getClassPath();
40+
logger.log(Level.FINE, "adding RuntimeResource-ClassPath: {0}", resource.getClassPath());
41+
addAttribute(attributes, "RuntimeResource-ClassPath", template != null ? template.toString() : "null");
42+
43+
}
2544
}
26-
}
27-
28-
public static void setTransactionName(Map<String, Object> attributes, Route route) {
29-
if (route != null) {
30-
addAttribute(attributes, "Route-Name", route.getName());
31-
addAttribute(attributes, "Route-Path", route.getPath());
45+
46+
public static void addRequestContext(Map<String, Object> attributes, ResteasyReactiveRequestContext context) {
47+
if(context != null) {
48+
addAttribute(attributes, "ResteasyReactiveRequestContext-AbsoluteURI", context.getAbsoluteURI());
49+
addAttribute(attributes, "ResteasyReactiveRequestContext-Method", context.getMethod());
50+
addAttribute(attributes, "ResteasyReactiveRequestContext-Path", context.getPath());
51+
UriInfo uriInfo = context.getUriInfo();
52+
addUriInfo(attributes, uriInfo);
53+
}
3254
}
33-
}
34-
35-
public static void setTransactionName(RoutingContext request) {
36-
String name = " ";
37-
String route = request.currentRoute().getName();
38-
if (route != null & route.length() > 0) {
39-
name += route;
55+
56+
public static void addUriInfo(Map<String,Object> attributes, UriInfo info) {
57+
if(info != null) {
58+
addAttribute(attributes, "UriInfo-Path", info.getPath());
59+
addAttribute(attributes, "UriInfo-BaseUri", info.getBaseUri());
60+
addAttribute(attributes, "UriInfo-MatchedURIs", info.getMatchedURIs());
61+
addAttribute(attributes, "UriInfo-PathSegments", info.getPathSegments());
62+
}
4063
}
41-
String path = request.currentRoute().getPath();
42-
if (path != null & path.length() > 0) {
43-
name += ":" + path;
64+
65+
public static void addRoute(Map<String, Object> attributes, Route route) {
66+
if (route != null) {
67+
addAttribute(attributes, "Route-Name", route.getName());
68+
addAttribute(attributes, "Route-Path", route.getPath());
69+
}
4470
}
45-
String method = request.request().method().name();
46-
if (method != null & method.length() > 0) {
47-
name += " (" + method + ")";
71+
72+
public static void setTransactionName(Map<String, Object> attributes, Route route) {
73+
if (route != null) {
74+
addAttribute(attributes, "Route-Name", route.getName());
75+
addAttribute(attributes, "Route-Path", route.getPath());
76+
}
77+
}
78+
79+
public static void setTransactionName(RuntimeResource resource) {
80+
if(resource != null) {
81+
URITemplate path = resource.getPath();
82+
URITemplate classpath = resource.getClassPath();
83+
84+
StringBuffer sb = new StringBuffer();
85+
if(classpath != null) {
86+
String template = classpath.template;
87+
if(template != null && !template.isEmpty()) {
88+
sb.append(template);
89+
}
90+
}
91+
if(path != null) {
92+
String template = path.template;
93+
if(template != null && !template.isEmpty()) {
94+
sb.append(template);
95+
}
96+
}
97+
98+
if(sb.length() > 0) {
99+
NewRelic.getAgent().getTransaction().setTransactionName(TransactionNamePriority.CUSTOM_LOW, true, "Quarkus-Resteasy", sb.toString());
100+
}
101+
}
48102
}
49-
System.out.println(name);
50-
NewRelic.getAgent().getTransaction().setTransactionName(TransactionNamePriority.FRAMEWORK_LOW, false, "Quarkus",
51-
"resteasy-reactive", name);
52103

53-
}
104+
public static void setTransactionName(RoutingContext request) {
105+
String name = " ";
106+
String route = request.currentRoute().getName();
107+
if (route != null & route.length() > 0) {
108+
name += route;
109+
}
110+
String path = request.currentRoute().getPath();
111+
if (path != null & path.length() > 0) {
112+
name += ":" + path;
113+
}
114+
String method = request.request().method().name();
115+
if (method != null & method.length() > 0) {
116+
name += " (" + method + ")";
117+
}
118+
System.out.println(name);
119+
NewRelic.getAgent().getTransaction().setTransactionName(TransactionNamePriority.FRAMEWORK_LOW, false, "Quarkus",
120+
"resteasy-reactive", name);
121+
122+
}
54123
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.jboss.resteasy.reactive.server.handlers;
2+
3+
import java.util.HashMap;
4+
5+
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;
6+
import org.jboss.resteasy.reactive.server.mapping.RuntimeResource;
7+
import org.jboss.resteasy.reactive.server.spi.EndpointInvoker;
8+
9+
import com.newrelic.api.agent.NewRelic;
10+
import com.newrelic.api.agent.Trace;
11+
import com.newrelic.api.agent.weaver.Weave;
12+
import com.newrelic.api.agent.weaver.Weaver;
13+
import com.newrelic.instrumentation.quarkus.resteasy.reactive.Utils;
14+
15+
@Weave
16+
public class InvocationHandler {
17+
private final EndpointInvoker invoker = Weaver.callOriginal();
18+
19+
@Trace(dispatcher = true)
20+
public void handle(ResteasyReactiveRequestContext requestContext) {
21+
HashMap<String, Object> attributes = new HashMap<>();
22+
RuntimeResource target = requestContext.getTarget();
23+
24+
Utils.addRuntimeResource(attributes, target);
25+
Utils.addRequestContext(attributes, requestContext);
26+
Utils.addAttribute(attributes, "Invoker", invoker);
27+
NewRelic.getAgent().getTracedMethod().addCustomAttributes(attributes);
28+
Utils.setTransactionName(target);
29+
30+
Weaver.callOriginal();
31+
}
32+
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.jboss.resteasy.reactive.server.spi;
2+
3+
import com.newrelic.api.agent.NewRelic;
4+
import com.newrelic.api.agent.Trace;
5+
import com.newrelic.api.agent.weaver.MatchType;
6+
import com.newrelic.api.agent.weaver.Weave;
7+
import com.newrelic.api.agent.weaver.Weaver;
8+
9+
@Weave(type = MatchType.Interface)
10+
public abstract class EndpointInvoker {
11+
12+
@Trace(dispatcher = true)
13+
public Object invoke(Object instance, Object[] parameters) {
14+
NewRelic.getAgent().getTracedMethod().setMetricName("Custom","Resteasy","Reactive","EndpointInvoker",getClass().getName(),"invoke");
15+
return Weaver.callOriginal();
16+
}
17+
}

quarkus-resteasy-reactive-2.11/src/main/java/org/jboss/resteasy/reactive/server/vertx/ResteasyReactiveVertxHandler.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
@Weave
1414
public abstract class ResteasyReactiveVertxHandler {
1515

16-
@Trace
17-
public void handle(RoutingContext event) {
18-
HashMap<String, Object> attributes = new HashMap<String, Object>();
19-
Utils.addAttribute(attributes, "RoutingContext-NormalizedPath", event.normalizedPath());
20-
Utils.addRoute(attributes, event.currentRoute());
21-
NewRelic.getAgent().getTracedMethod().addCustomAttributes(attributes);
22-
Utils.setTransactionName(event);
23-
Weaver.callOriginal();
24-
}
16+
@Trace
17+
public void handle(RoutingContext event) {
18+
HashMap<String, Object> attributes = new HashMap<String, Object>();
19+
Utils.addAttribute(attributes, "RoutingContext-NormalizedPath", event.normalizedPath());
20+
Utils.addRoute(attributes, event.currentRoute());
21+
NewRelic.getAgent().getTracedMethod().addCustomAttributes(attributes);
22+
Utils.setTransactionName(event);
23+
Weaver.callOriginal();
24+
}
2525
}

0 commit comments

Comments
 (0)