Skip to content

Commit 77175d5

Browse files
committed
added external services and distributed tracing support
1 parent ae002b7 commit 77175d5

16 files changed

+438
-170
lines changed

spring-cloud-gateway/build.gradle

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ jar {
2626
}
2727

2828
verifyInstrumentation {
29-
passes("org.springframework.cloud:spring-cloud-gateway-core:[2.2.6.RELEASE,)") {
30-
compile 'org.springframework:spring-web:5.3.2'
31-
compile 'org.springframework:spring-web:5.3.2'
32-
33-
}
29+
// passes("org.springframework.cloud:spring-cloud-gateway-core:[2.2.6.RELEASE,)") {
30+
// compile 'org.springframework:spring-web:5.3.2'
3431

32+
// }
33+
passes("org.springframework.cloud:spring-cloud-gateway-server:[2.2.6.RELEASE,)") {
34+
compile 'org.springframework:spring-web:5.3.2'
35+
}
3536
}
3637

spring-cloud-gateway/src/main/java/com/newrelic/instrumentation/labs/spring/cloud/gw/FilteringWebHandler_Instrumentation.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

spring-cloud-gateway/src/main/java/com/newrelic/instrumentation/labs/spring/cloud/gw/GatewayFilter_Instrumentation.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

spring-cloud-gateway/src/main/java/com/newrelic/instrumentation/labs/spring/cloud/gw/GlobalFilter_Instrumentation.java

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.newrelic.instrumentation.labs.spring.cloud.gw;
2+
3+
import java.util.function.Consumer;
4+
5+
public class NRCompletionConsumer implements Consumer<Void> {
6+
7+
private NRHolder holder = null;
8+
9+
public NRCompletionConsumer(NRHolder h) {
10+
holder = h;
11+
}
12+
13+
14+
@Override
15+
public void accept(Void t) {
16+
if(holder != null) {
17+
holder.end();
18+
}
19+
}
20+
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.newrelic.instrumentation.labs.spring.cloud.gw;
2+
3+
import java.util.function.Consumer;
4+
5+
import com.newrelic.api.agent.NewRelic;
6+
7+
public class NRErrorConsumer implements Consumer<Throwable> {
8+
9+
private NRHolder holder = null;
10+
11+
public NRErrorConsumer(NRHolder h) {
12+
holder = h;
13+
}
14+
15+
16+
@Override
17+
public void accept(Throwable t) {
18+
if(t != null) {
19+
NewRelic.noticeError(t);
20+
}
21+
if(holder != null) {
22+
holder.ignore();
23+
}
24+
}
25+
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.newrelic.instrumentation.labs.spring.cloud.gw;
2+
3+
import com.newrelic.api.agent.ExternalParameters;
4+
import com.newrelic.api.agent.Segment;
5+
6+
public class NRHolder {
7+
8+
private Segment segment = null;
9+
10+
public NRHolder(Segment seg) {
11+
segment = seg;
12+
}
13+
14+
public boolean reportAsExternal(ExternalParameters params) {
15+
if(segment != null) {
16+
segment.reportAsExternal(params);
17+
return true;
18+
}
19+
return false;
20+
}
21+
22+
public void ignore() {
23+
if(segment != null) {
24+
segment.ignore();
25+
segment = null;
26+
}
27+
}
28+
29+
public void end() {
30+
if(segment != null) {
31+
segment.end();
32+
segment = null;
33+
}
34+
}
35+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
3+
package com.newrelic.instrumentation.labs.spring.cloud.gw;
4+
5+
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
6+
7+
import java.net.URI;
8+
import java.util.Map;
9+
import java.util.regex.Pattern;
10+
11+
import org.springframework.http.HttpMethod;
12+
import org.springframework.http.server.reactive.ServerHttpRequest;
13+
import org.springframework.web.server.ServerWebExchange;
14+
15+
import com.newrelic.api.agent.HttpParameters;
16+
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.isAlreadyRouted;
17+
18+
public class SpringCloudUtils {
19+
20+
private static final Pattern versionPattern = Pattern.compile("[vV][0-9]{1,}");
21+
private static final Pattern idPattern = Pattern.compile("^(?=[^\\s]*?[0-9])[-{}().:_|0-9]+$");
22+
private static final Pattern codPattern = Pattern.compile("^(?=[^\\s]*?[0-9])(?=[^\\s]*?[a-zA-Z])(?!\\{id\\}).*$");
23+
24+
25+
26+
public static void recordValue(Map<String,Object> attributes, String key, Object value) {
27+
if(key != null && !key.isEmpty() && value != null) {
28+
attributes.put(key, value);
29+
}
30+
}
31+
32+
public static HttpParameters getParams(ServerWebExchange exchange) {
33+
try {
34+
URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
35+
ServerHttpRequest request = exchange.getRequest();
36+
HttpMethod method = request.getMethod();
37+
HttpParameters params = HttpParameters.library("Spring-Cloud").uri(requestUrl).procedure(method.name()).noInboundHeaders().build();
38+
return params;
39+
} catch (Exception e) {
40+
}
41+
42+
return null;
43+
}
44+
45+
public static String getSimpliedPath(ServerWebExchange exchange) {
46+
String path = exchange.getRequest().getPath().value();
47+
48+
String simplifiedPath = path;
49+
50+
final String[] splitPath = path.split("/");
51+
52+
if (splitPath.length > 0) {
53+
simplifiedPath = "";
54+
for (String p : splitPath) {
55+
if (versionPattern.matcher(p).matches()) {
56+
simplifiedPath = simplifiedPath.concat("/").concat(p.replaceAll(versionPattern.toString(), "{version}"));
57+
} else if (idPattern.matcher(p).matches()) {
58+
simplifiedPath = simplifiedPath.concat("/").concat(p.replaceAll(idPattern.toString(), "{id}"));
59+
} else if (codPattern.matcher(p).matches()) {
60+
simplifiedPath = simplifiedPath.concat("/").concat(p.replaceAll(codPattern.toString(), "{cod}"));
61+
} else {
62+
simplifiedPath = simplifiedPath.concat("/").concat(p);
63+
}
64+
}
65+
}
66+
67+
return simplifiedPath;
68+
}
69+
70+
public static boolean skip(ServerWebExchange exchange) {
71+
URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
72+
73+
String scheme = requestUrl.getScheme();
74+
if (isAlreadyRouted(exchange) || (!"http".equals(scheme) && !"https".equals(scheme))) {
75+
return true;
76+
}
77+
return false;
78+
}
79+
80+
public static boolean skipWS(ServerWebExchange exchange) {
81+
URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
82+
83+
String scheme = requestUrl.getScheme();
84+
if (isAlreadyRouted(exchange) || (!"ws".equals(scheme) && !"wss".equals(scheme))) {
85+
return true;
86+
}
87+
return false;
88+
}
89+
90+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.newrelic.instrumentation.labs.spring.cloud.gw;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
import org.springframework.http.HttpHeaders;
9+
10+
import com.newrelic.api.agent.HeaderType;
11+
import com.newrelic.api.agent.Headers;
12+
13+
public class SpringGatewayHeaders implements Headers {
14+
15+
private HttpHeaders headers = null;
16+
17+
public SpringGatewayHeaders(HttpHeaders h) {
18+
headers = h;
19+
}
20+
21+
@Override
22+
public void addHeader(String name, String value) {
23+
if(headers != null) {
24+
headers.add(name, value);
25+
}
26+
}
27+
28+
@Override
29+
public boolean containsHeader(String name) {
30+
return getHeaderNames().contains(name);
31+
}
32+
33+
@Override
34+
public String getHeader(String name) {
35+
if(headers != null) {
36+
return headers.getFirst(name);
37+
}
38+
return null;
39+
}
40+
41+
@Override
42+
public Collection<String> getHeaderNames() {
43+
if(headers != null) {
44+
return headers.keySet();
45+
}
46+
return Collections.emptyList();
47+
}
48+
49+
@Override
50+
public HeaderType getHeaderType() {
51+
return HeaderType.HTTP;
52+
}
53+
54+
@Override
55+
public Collection<String> getHeaders(String name) {
56+
if(headers != null) {
57+
return headers.get(name);
58+
}
59+
return null;
60+
}
61+
62+
@Override
63+
public void setHeader(String name, String value) {
64+
if(headers != null) {
65+
headers.set(name, value);
66+
}
67+
}
68+
69+
}

spring-cloud-gateway/src/main/java/com/newrelic/instrumentation/labs/spring/cloud/gw/Util.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)