-
Notifications
You must be signed in to change notification settings - Fork 713
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for org.mongodb:mongodb-driver-core v5 (#1431)
Signed-off-by: Adrian Cole <[email protected]>
- Loading branch information
1 parent
fe3c8b4
commit ef6fbbd
Showing
12 changed files
with
393 additions
and
59 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
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
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,2 @@ | ||
# mongodb_floor | ||
This tests that MongoDBTracing can be used with mongodb <5 |
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,120 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2013-2024 The OpenZipkin Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
in compliance with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed under the License | ||
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
or implied. See the License for the specific language governing permissions and limitations under | ||
the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>@project.groupId@</groupId> | ||
<artifactId>mongodb_floor</artifactId> | ||
<version>@project.version@</version> | ||
<name>mongodb_floor</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.build.resourceEncoding>UTF-8</project.build.resourceEncoding> | ||
|
||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.mongodb</groupId> | ||
<artifactId>mongodb-driver</artifactId> | ||
<version>@floor-mongodb-driver.version@</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>@project.groupId@</groupId> | ||
<artifactId>brave-instrumentation-mongodb</artifactId> | ||
<version>@project.version@</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>@project.groupId@</groupId> | ||
<artifactId>brave-tests</artifactId> | ||
<version>@project.version@</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>@junit-jupiter.version@</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-junit-jupiter</artifactId> | ||
<version>@mockito.version@</version> | ||
</dependency> | ||
<!-- Route jul over log4j2 during integration tests --> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-slf4j-impl</artifactId> | ||
<version>@log4j.version@</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
<version>@log4j.version@</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>@testcontainers.version@</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<sourceDirectory>@project.build.testSourceDirectory@</sourceDirectory> | ||
<testResources> | ||
<testResource> | ||
<directory>@project.basedir@/src/test/resources</directory> | ||
</testResource> | ||
</testResources> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>@maven-compiler-plugin.version@</version> | ||
<configuration> | ||
<includes> | ||
<include>**/IT*.java</include> | ||
</includes> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<!-- Use surefire to run the ITs until someone figures out how to get invoker to run | ||
failsafe --> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>@maven-surefire-plugin.version@</version> | ||
<configuration> | ||
<failIfNoTests>true</failIfNoTests> | ||
<includes> | ||
<include>**/IT*.java</include> | ||
</includes> | ||
<!-- Try to prevent flakes in CI --> | ||
<reuseForks>false</reuseForks> | ||
<!-- workaround to SUREFIRE-1831 --> | ||
<useModulePath>false</useModulePath> | ||
<!-- Ensure scope leak cause ends up in the console --> | ||
<trimStackTrace>false</trimStackTrace> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
17 changes: 17 additions & 0 deletions
17
...ntation/mongodb/src/it/mongodb_floor/src/test/java/brave/mongodb_v3/ITMongoDBTracing.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,17 @@ | ||
/* | ||
* Copyright 2013-2024 The OpenZipkin Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package brave.mongodb_floor; | ||
|
||
class ITMongoDBTracing extends brave.mongodb.ITMongoDBTracing { | ||
} |
112 changes: 112 additions & 0 deletions
112
instrumentation/mongodb/src/main/java/brave/mongodb/MongoDBDriver.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,112 @@ | ||
/* | ||
* Copyright 2013-2024 The OpenZipkin Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package brave.mongodb; | ||
|
||
import brave.Span; | ||
import com.mongodb.ServerAddress; | ||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
import java.net.InetSocketAddress; | ||
|
||
import static brave.internal.Throwables.propagateIfFatal; | ||
|
||
/** | ||
* Access to MongoDB version-specific features | ||
* | ||
* <p>Originally designed by OkHttp team, derived from {@code okhttp3.internal.platform.Platform} | ||
*/ | ||
class MongoDBDriver { | ||
private static final MongoDBDriver MONGO_DB_DRIVER = findMongoDBDriver(); | ||
|
||
/** adds the remote IP and port to the span */ | ||
void setRemoteIpAndPort(Span span, ServerAddress address) { | ||
// default to no-op instead of crash on future drift | ||
} | ||
|
||
MongoDBDriver() { | ||
} | ||
|
||
public static MongoDBDriver get() { | ||
return MONGO_DB_DRIVER; | ||
} | ||
|
||
/** | ||
* Attempt to match the driver version from two known patterns: | ||
* | ||
* <ol> | ||
* <li>org.mongodb:mongodb-driver - 3.x</li> | ||
* <li>org.mongodb:mongodb-driver-core - 5.x</li> | ||
* </ol> | ||
*/ | ||
private static MongoDBDriver findMongoDBDriver() { | ||
MethodHandles.Lookup lookup = MethodHandles.lookup(); | ||
try { | ||
MethodHandle getHost = | ||
lookup.findVirtual(ServerAddress.class, "getHost", MethodType.methodType(String.class)); | ||
MethodHandle getPort = | ||
lookup.findVirtual(ServerAddress.class, "getPort", MethodType.methodType(int.class)); | ||
return new Driver5x(getHost, getPort); | ||
} catch (NoSuchMethodException | IllegalAccessException e) { | ||
// not 5.x | ||
} | ||
try { | ||
MethodHandle getSocketAddress = lookup.findVirtual(ServerAddress.class, "getSocketAddress", | ||
MethodType.methodType(InetSocketAddress.class)); | ||
return new Driver3x(getSocketAddress); | ||
} catch (NoSuchMethodException | IllegalAccessException e) { | ||
// unknown version | ||
} | ||
|
||
// Unknown | ||
return new MongoDBDriver(); | ||
} | ||
|
||
static final class Driver3x extends MongoDBDriver { | ||
final MethodHandle getSocketAddress; | ||
|
||
Driver3x(MethodHandle getSocketAddress) { | ||
this.getSocketAddress = getSocketAddress; | ||
} | ||
|
||
@Override void setRemoteIpAndPort(Span span, ServerAddress serverAddress) { | ||
try { | ||
InetSocketAddress socketAddress = | ||
(InetSocketAddress) getSocketAddress.invokeExact(serverAddress); | ||
span.remoteIpAndPort(socketAddress.getAddress().getHostAddress(), socketAddress.getPort()); | ||
} catch (Throwable t) { | ||
propagateIfFatal(t); | ||
} | ||
} | ||
} | ||
|
||
static final class Driver5x extends MongoDBDriver { | ||
final MethodHandle getHost, getPort; | ||
|
||
Driver5x(MethodHandle getHost, MethodHandle getPort) { | ||
this.getHost = getHost; | ||
this.getPort = getPort; | ||
} | ||
|
||
@Override void setRemoteIpAndPort(Span span, ServerAddress serverAddress) { | ||
try { | ||
String host = (String) getHost.invokeExact(serverAddress); | ||
int port = (int) getPort.invokeExact(serverAddress); | ||
span.remoteIpAndPort(host, port); | ||
} catch (Throwable t) { | ||
propagateIfFatal(t); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.