Skip to content

Commit de730d7

Browse files
authored
Fix possible IllegalStateException when using Micrometer (#531)
1 parent 079ab17 commit de730d7

5 files changed

Lines changed: 79 additions & 8 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Release Notes.
1616
* Fix Jedis-2.x plugin can not get host info in jedis 3.3.x+
1717
* Change the classloader to locate the agent path in AgentPackagePath, from `SystemClassLoader` to AgentPackagePath's loader.
1818
* Support Grizzly Trace
19+
* Fix possible IllegalStateException when using Micrometer.
1920

2021
#### Documentation
2122

apm-sniffer/apm-toolkit-activation/apm-toolkit-micrometer-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/micrometer/MicrometerReceiverTracingHandlerInterceptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
6060
SpanLayer spanLayer = TaggingHelper.toLayer(context.getAllKeyValues());
6161
AbstractSpan abstractSpan = ContextManager.activeSpan();
6262
abstractSpan
63-
.setPeer(SpanHelper.tryToGetPeer(context.getRemoteServiceAddress(), context.getAllKeyValues()))
63+
.setPeer(SpanHelper.tryToGetPeer(context.getRemoteServiceAddress(), context.getRemoteServiceName(),
64+
context.getAllKeyValues()))
6465
.setOperationName(getOperationName(context));
6566
context.getAllKeyValues()
6667
.forEach(keyValue -> abstractSpan.tag(Tags.ofKey(keyValue.getKey()), keyValue.getValue()));

apm-sniffer/apm-toolkit-activation/apm-toolkit-micrometer-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/micrometer/MicrometerSenderTracingHandlerInterceptor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
4848
SenderContext<Object> context = (SenderContext<Object>) allArguments[0];
4949
final ContextCarrier contextCarrier = new ContextCarrier();
5050
AbstractSpan span = ContextManager.createExitSpan(
51-
getOperationName(context), contextCarrier, SpanHelper.tryToGetPeer(context.getRemoteServiceAddress(), context.getAllKeyValues()));
51+
getOperationName(context), contextCarrier, SpanHelper.tryToGetPeer(context.getRemoteServiceAddress(),
52+
context.getRemoteServiceName(), context.getAllKeyValues()));
5253
CarrierItem next = contextCarrier.items();
5354
while (next.hasNext()) {
5455
next = next.next();
@@ -60,7 +61,8 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr
6061
SpanLayer spanLayer = TaggingHelper.toLayer(context.getAllKeyValues());
6162
AbstractSpan abstractSpan = ContextManager.activeSpan();
6263
abstractSpan
63-
.setPeer(SpanHelper.tryToGetPeer(context.getRemoteServiceAddress(), context.getAllKeyValues()))
64+
.setPeer(SpanHelper.tryToGetPeer(context.getRemoteServiceAddress(), context.getRemoteServiceName(),
65+
context.getAllKeyValues()))
6466
.setOperationName(getOperationName(context));
6567
context.getAllKeyValues()
6668
.forEach(keyValue -> abstractSpan.tag(Tags.ofKey(keyValue.getKey()), keyValue.getValue()));

apm-sniffer/apm-toolkit-activation/apm-toolkit-micrometer-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/micrometer/SpanHelper.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020

2121
import io.micrometer.common.KeyValue;
2222
import io.micrometer.common.KeyValues;
23+
2324
import java.net.URI;
2425

2526
class SpanHelper {
2627

27-
static String tryToGetPeer(String remoteAddress, KeyValues allKeyValues) {
28+
static String tryToGetPeer(String remoteAddress, String remoteName, KeyValues allKeyValues) {
2829
if (remoteAddress != null) {
2930
return remoteAddress;
3031
}
@@ -39,12 +40,12 @@ static String tryToGetPeer(String remoteAddress, KeyValues allKeyValues) {
3940
.orElse("unknown");
4041
try {
4142
URI uri = URI.create(result);
42-
if (uri.getHost() == null) {
43-
return null;
43+
if (uri.getHost() != null) {
44+
return uri.getHost() + ":" + uri.getPort();
4445
}
45-
return uri.getHost() + ":" + uri.getPort();
46+
return remoteName;
4647
} catch (Exception ex) {
47-
return null;
48+
return remoteName;
4849
}
4950
}
5051
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.toolkit.activation.micrometer;
20+
21+
import io.micrometer.common.KeyValue;
22+
import io.micrometer.common.KeyValues;
23+
24+
import org.junit.Test;
25+
26+
import static org.hamcrest.CoreMatchers.is;
27+
import static org.junit.Assert.assertThat;
28+
29+
public class SpanHelperTest {
30+
31+
@Test
32+
public void testTryToGetPeer() {
33+
String remoteName = SpanHelper.tryToGetPeer("http://localhost:8080", "remoteName",
34+
KeyValues.of(KeyValue.of("http.url", "http://localhost:8081")));
35+
assertThat(remoteName, is("http://localhost:8080"));
36+
}
37+
38+
@Test
39+
public void testTryToGetPeerWhenRemoteAddressIsNull() {
40+
String remoteName = SpanHelper.tryToGetPeer(null, "remoteName",
41+
KeyValues.of(KeyValue.of("http.url", "http://localhost:8080")));
42+
assertThat(remoteName, is("localhost:8080"));
43+
}
44+
45+
@Test
46+
public void testTryToGetPeerWhenURIWithQueryString() {
47+
String remoteName = SpanHelper.tryToGetPeer(null, "remoteName",
48+
KeyValues.of(KeyValue.of("http.url", "http://localhost:8080?a=b")));
49+
assertThat(remoteName, is("localhost:8080"));
50+
}
51+
52+
@Test
53+
public void testTryToGetPeerWhenLackOfURIComponents() {
54+
String remoteName = SpanHelper.tryToGetPeer(null, "remoteName",
55+
KeyValues.empty());
56+
assertThat(remoteName, is("remoteName"));
57+
}
58+
59+
@Test
60+
public void testTryToGetPeerWhenURIViolatesRFC2396() {
61+
String remoteName = SpanHelper.tryToGetPeer(null, "remoteName",
62+
KeyValues.of(KeyValue.of("http.url", "https://www.example.com/path/with spaces")));
63+
assertThat(remoteName, is("remoteName"));
64+
}
65+
66+
}

0 commit comments

Comments
 (0)