-
Notifications
You must be signed in to change notification settings - Fork 867
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matthieu MOREL <[email protected]> Co-Authored-By: jason plumb <[email protected]>
- Loading branch information
1 parent
dc5d76a
commit d9faa5e
Showing
4 changed files
with
183 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
.../main/java/io/opentelemetry/instrumentation/api/instrumenter/net/PeerServiceResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.instrumenter.net; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.Comparator; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
class PeerServiceResolver { | ||
|
||
private static final Map<String, Map<ServiceMatcher, String>> MAPPING = new ConcurrentHashMap<>(); | ||
|
||
PeerServiceResolver(Map<String, String> peerServiceMapping) { | ||
peerServiceMapping.forEach( | ||
(key, serviceName) -> { | ||
try { | ||
String protocol = null; | ||
URI uri; | ||
if (key.contains("://")) { | ||
uri = new URI(key); | ||
protocol = uri.getScheme(); | ||
} else { | ||
uri = new URI("http://" + key); | ||
} | ||
String host = uri.getHost(); | ||
Integer port = uri.getPort() == -1 ? null : uri.getPort(); | ||
Map<ServiceMatcher, String> matchers = | ||
MAPPING.computeIfAbsent(host, x -> new ConcurrentHashMap<>()); | ||
matchers.putIfAbsent(new ServiceMatcher(protocol, port, uri.getPath()), serviceName); | ||
} catch (URISyntaxException use) { | ||
// TODO wire logging | ||
} | ||
}); | ||
} | ||
|
||
boolean isEmpty() { | ||
return MAPPING.isEmpty(); | ||
} | ||
|
||
String resolveService(String protocol, String host, Integer port, String path) { | ||
Map<ServiceMatcher, String> matchers = MAPPING.get(host); | ||
if (matchers == null) { | ||
return null; | ||
} | ||
return matchers.entrySet().stream() | ||
.filter(entry -> entry.getKey().matches(protocol, port, path)) | ||
.max(Comparator.comparingInt(entry -> entry.getKey().getPathLength())) | ||
.map(Map.Entry::getValue) | ||
.orElse(null); | ||
} | ||
|
||
private static class ServiceMatcher { | ||
|
||
private final String protocol; | ||
private final Integer port; | ||
private final String path; | ||
|
||
public ServiceMatcher(String protocol, Integer port, String path) { | ||
this.protocol = protocol; | ||
this.port = port; | ||
this.path = path; | ||
} | ||
|
||
public int getPathLength() { | ||
if (this.path == null) { | ||
return 0; | ||
} | ||
return this.path.length(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof ServiceMatcher)) { | ||
return false; | ||
} | ||
ServiceMatcher that = (ServiceMatcher) o; | ||
return Objects.equals(protocol, that.protocol) | ||
&& Objects.equals(port, that.port) | ||
&& Objects.equals(path, that.path); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(protocol, port, path); | ||
} | ||
|
||
public boolean matches(String protocol, Integer port, String path) { | ||
if (this.protocol != null) { | ||
if (!this.protocol.equals(protocol)) { | ||
return false; | ||
} | ||
} | ||
if (this.port != null) { | ||
if (!this.port.equals(port)) { | ||
return false; | ||
} | ||
} | ||
if (this.path != null) { | ||
if (path == null) { | ||
return false; | ||
} | ||
if (!path.startsWith(this.path)) { | ||
return false; | ||
} | ||
if (port != null) { | ||
return port.equals(this.port); | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...t/java/io/opentelemetry/instrumentation/api/instrumenter/net/PeerServiceResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.instrumenter.net; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class PeerServiceResolverTest { | ||
|
||
@Test | ||
public void test() { | ||
Map<String, String> peerServiceMapping = new HashMap<>(); | ||
peerServiceMapping.put("tcp://example.com:8080", "myTcpService"); | ||
peerServiceMapping.put("example.com:8080", "myService"); | ||
peerServiceMapping.put("example.com", "myServiceBase"); | ||
peerServiceMapping.put("1.2.3.4", "someOtherService"); | ||
peerServiceMapping.put("1.2.3.4:8080/api", "someOtherService8080"); | ||
peerServiceMapping.put("1.2.3.4/api", "someOtherServiceAPI"); | ||
|
||
PeerServiceResolver peerServiceResolver = new PeerServiceResolver(peerServiceMapping); | ||
|
||
assertEquals( | ||
"myServiceBase", peerServiceResolver.resolveService(null, "example.com", null, null)); | ||
assertEquals("myService", peerServiceResolver.resolveService("http", "example.com", 8080, "/")); | ||
assertEquals( | ||
"myTcpService", peerServiceResolver.resolveService("tcp", "example.com", 8080, "/api")); | ||
assertEquals( | ||
"someOtherService8080", peerServiceResolver.resolveService(null, "1.2.3.4", 8080, "/api")); | ||
assertEquals( | ||
"someOtherService", peerServiceResolver.resolveService(null, "1.2.3.4", 9000, "/api")); | ||
assertEquals( | ||
"someOtherService", peerServiceResolver.resolveService(null, "1.2.3.4", 8080, null)); | ||
assertEquals( | ||
"someOtherServiceAPI", peerServiceResolver.resolveService(null, "1.2.3.4", null, "/api")); | ||
} | ||
} |