Skip to content

Commit 7c6a575

Browse files
xiepuhuanxiepuhuan
authored and
xiepuhuan
committed
Add isGlobPattern for GlobUtil.
Add comments for copied methods.
1 parent 8e7dcf6 commit 7c6a575

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/net/PeerServiceResolverImpl.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class PeerServiceResolverImpl implements PeerServiceResolver {
4949
return;
5050
}
5151

52-
if (host.contains("*") || host.contains("?")) {
52+
if (GlobUtil.isGlobPattern(host)) {
5353
ServiceMatcher serviceMatcher =
5454
ServiceMatcher.create(GlobUrlParser.getPort(url), GlobUrlParser.getPath(url));
5555
globMapping
@@ -72,22 +72,6 @@ public boolean isEmpty() {
7272
return mapping.isEmpty() && globMapping.isEmpty();
7373
}
7474

75-
@Nullable
76-
static String matchService(
77-
@Nullable Map<ServiceMatcher, String> matchers,
78-
@Nullable Integer port,
79-
@Nullable Supplier<String> pathSupplier) {
80-
if (matchers == null) {
81-
return null;
82-
}
83-
84-
return matchers.entrySet().stream()
85-
.filter(entry -> entry.getKey().matches(port, pathSupplier))
86-
.findFirst()
87-
.map(Map.Entry::getValue)
88-
.orElse(null);
89-
}
90-
9175
@Override
9276
@Nullable
9377
public String resolveService(
@@ -112,6 +96,22 @@ public String resolveService(
11296
return null;
11397
}
11498

99+
@Nullable
100+
static String matchService(
101+
@Nullable Map<ServiceMatcher, String> matchers,
102+
@Nullable Integer port,
103+
@Nullable Supplier<String> pathSupplier) {
104+
if (matchers == null) {
105+
return null;
106+
}
107+
108+
return matchers.entrySet().stream()
109+
.filter(entry -> entry.getKey().matches(port, pathSupplier))
110+
.findFirst()
111+
.map(Map.Entry::getValue)
112+
.orElse(null);
113+
}
114+
115115
@AutoValue
116116
abstract static class ServiceMatcher {
117117

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/net/internal/GlobUrlParser.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
1313
* any time.
1414
*/
15+
// Copied from UrlParser except for getHostEndIndexExclusive.
1516
public final class GlobUrlParser {
1617

1718
@Nullable
@@ -105,7 +106,7 @@ private static int getHostStartIndex(String url) {
105106
private static int getHostEndIndexExclusive(String url, int startIndex) {
106107
// look for the end of the host:
107108
// ':' ==> start of port, or
108-
// '/', '?', '#' ==> start of path
109+
// '/', '#' ==> start of path
109110
return getEndIndexExclusive(url, startIndex, c -> (c == ':' || c == '/' || c == '#'));
110111
}
111112

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/util/internal/GlobUtil.java

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
1515
* at any time.
1616
*/
17+
// copied from io.opentelemetry.sdk.internal.GlobUtil except for isGlobString.
1718
public final class GlobUtil {
1819

1920
private GlobUtil() {}
@@ -79,4 +80,9 @@ private static Pattern toRegexPattern(String globPattern) {
7980
}
8081
return Pattern.compile(patternBuilder.toString());
8182
}
83+
84+
/** Returns true if the {@code string} contains {@code *} or {@code .*}. */
85+
public static boolean isGlobPattern(String string) {
86+
return string.contains("*") || string.contains("?");
87+
}
8288
}

instrumentation-api-incubator/src/test/java/io/opentelemetry/instrumentation/api/incubator/semconv/net/internal/GlobUrlParserTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ void testGetHostWithPort() {
5252
.isEqualTo("*.example.???");
5353
}
5454

55+
// Copied from UrlParserTest
5556
@Test
5657
void testGetHostWithNoAuthority() {
5758
assertThat(GlobUrlParser.getHost("https:")).isNull();
@@ -70,6 +71,7 @@ void testGetHostWithNoAuthority() {
7071
assertThat(GlobUrlParser.getHost("https:/#fragment")).isNull();
7172
}
7273

74+
// Copied from UrlParserTest
7375
@Test
7476
void testGetHostWithNoScheme() {
7577
assertThat(GlobUrlParser.getHost("")).isNull();
@@ -124,6 +126,7 @@ void testGetPortWithPort() {
124126
assertThat(GlobUrlParser.getPort("https://*.example.???:8080/#fragment")).isEqualTo(8080);
125127
}
126128

129+
// Copied from UrlParserTest
127130
@Test
128131
void testGetPortWithNoAuthority() {
129132
assertThat(GlobUrlParser.getPort("https:")).isNull();
@@ -142,6 +145,7 @@ void testGetPortWithNoAuthority() {
142145
assertThat(GlobUrlParser.getPort("https:/#fragment")).isNull();
143146
}
144147

148+
// Copied from UrlParserTest
145149
@Test
146150
void testGetPortWithNoScheme() {
147151
assertThat(GlobUrlParser.getPort("")).isNull();
@@ -208,6 +212,7 @@ void testGetPathWithPort() {
208212
.isEqualTo("/api/v1");
209213
}
210214

215+
// Copied from UrlParserTest
211216
@Test
212217
void testGetPathWithNoAuthority() {
213218
assertThat(GlobUrlParser.getPath("https:")).isNull();
@@ -231,6 +236,7 @@ void testGetPathWithNoAuthority() {
231236
assertThat(GlobUrlParser.getPath("https:/api/v1#fragment")).isNull();
232237
}
233238

239+
// Copied from UrlParserTest
234240
@Test
235241
void testGetPathWithNoScheme() {
236242
assertThat(GlobUrlParser.getPath("")).isNull();

instrumentation-api-incubator/src/test/java/io/opentelemetry/instrumentation/api/incubator/semconv/util/internal/GlobUtilTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
package io.opentelemetry.instrumentation.api.incubator.semconv.util.internal;
77

8+
import static io.opentelemetry.instrumentation.api.incubator.semconv.util.internal.GlobUtil.isGlobPattern;
89
import static io.opentelemetry.instrumentation.api.incubator.semconv.util.internal.GlobUtil.toGlobPatternPredicate;
910
import static org.assertj.core.api.Assertions.assertThat;
1011

1112
import org.junit.jupiter.api.Test;
1213

14+
// copied from io.opentelemetry.sdk.internal.GlobUtilTest
1315
class GlobUtilTest {
1416

1517
@Test
@@ -43,4 +45,20 @@ void matchesName() {
4345
assertThat(toGlobPatternPredicate("f()[]$^.{}|?").test("f()[]$^.{}|o")).isTrue();
4446
assertThat(toGlobPatternPredicate("f()[]$^.{}|*").test("f()[]$^.{}|ooo")).isTrue();
4547
}
48+
49+
@Test
50+
void testIsGlobPattern() {
51+
assertThat(isGlobPattern("foo")).isFalse();
52+
assertThat(isGlobPattern("fo?")).isTrue();
53+
assertThat(isGlobPattern("fo??")).isTrue();
54+
assertThat(isGlobPattern("*")).isTrue();
55+
assertThat(isGlobPattern("fo*")).isTrue();
56+
assertThat(isGlobPattern("*bar")).isTrue();
57+
assertThat(isGlobPattern("fo*b*")).isTrue();
58+
assertThat(isGlobPattern("fo? b??")).isTrue();
59+
assertThat(isGlobPattern("fo* ba?")).isTrue();
60+
assertThat(isGlobPattern("f()[]$^.{}|")).isFalse();
61+
assertThat(isGlobPattern("f()[]$^.{}|?")).isTrue();
62+
assertThat(isGlobPattern("f()[]$^.{}|*")).isTrue();
63+
}
4664
}

0 commit comments

Comments
 (0)