-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delay loading
InetAddressResolverProvider
until after the agent has…
… started (#11987) Co-authored-by: Lauri Tulmin <[email protected]>
- Loading branch information
1 parent
7d004b5
commit 7b49d12
Showing
9 changed files
with
216 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
plugins { | ||
id("otel.javaagent-testing") | ||
} | ||
|
||
dependencies { | ||
compileOnly("io.opentelemetry:opentelemetry-sdk-common") | ||
} | ||
|
||
otelJava { | ||
minJavaVersionSupported.set(JavaVersion.VERSION_18) | ||
} |
30 changes: 30 additions & 0 deletions
30
javaagent-tooling/jdk18-testing/src/main/java/testing/TestResourceProvider.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,30 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package testing; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
|
||
@AutoService(ResourceProvider.class) | ||
public class TestResourceProvider implements ResourceProvider { | ||
|
||
@Override | ||
public Resource createResource(ConfigProperties config) { | ||
// used in test to determine whether this method was called | ||
System.setProperty("test.resource.provider.called", "true"); | ||
// this call trigger loading InetAddressResolverProvider SPI on jdk 18 | ||
try { | ||
InetAddress.getLocalHost(); | ||
} catch (UnknownHostException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
return Resource.empty(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...src/test/java/io/opentelemetry/javaagent/tooling/inetaddress/InetAddressResolverTest.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,28 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.tooling.inetaddress; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.net.InetAddress; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class InetAddressResolverTest { | ||
|
||
@Test | ||
void agentStartShouldNotTriggerLoadingCustomInetAddressResolvers() throws Exception { | ||
// This system property is set in TestResourceProvider | ||
assertThat(System.getProperty("test.resource.provider.called")).isEqualTo("true"); | ||
// Agent start should not trigger loading (and instantiating) custom InetAddress resolvers | ||
assertThat(TestAddressResolver.isInstantiated()).isFalse(); | ||
|
||
// Trigger loading (and instantiating) custom InetAddress resolvers manually | ||
InetAddress.getAllByName("test"); | ||
|
||
// Verify that custom InetAddress resolver loaded and instantiated | ||
assertThat(TestAddressResolver.isInstantiated()).isTrue(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...ing/src/test/java/io/opentelemetry/javaagent/tooling/inetaddress/TestAddressResolver.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,39 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.tooling.inetaddress; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.net.spi.InetAddressResolver; | ||
import java.util.stream.Stream; | ||
|
||
public class TestAddressResolver implements InetAddressResolver { | ||
|
||
private static volatile boolean instantiated = false; | ||
|
||
@SuppressWarnings("StaticAssignmentInConstructor") | ||
public TestAddressResolver() { | ||
TestAddressResolver.instantiated = true; | ||
} | ||
|
||
public static boolean isInstantiated() { | ||
return instantiated; | ||
} | ||
|
||
@Override | ||
public Stream<InetAddress> lookupByName(String host, LookupPolicy lookupPolicy) | ||
throws UnknownHostException { | ||
if (host.equals("test")) { | ||
return Stream.of(InetAddress.getByAddress(new byte[] {127, 0, 0, 1})); | ||
} | ||
throw new UnknownHostException(); | ||
} | ||
|
||
@Override | ||
public String lookupByAddress(byte[] addr) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...test/java/io/opentelemetry/javaagent/tooling/inetaddress/TestAddressResolverProvider.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,22 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.tooling.inetaddress; | ||
|
||
import java.net.spi.InetAddressResolver; | ||
import java.net.spi.InetAddressResolverProvider; | ||
|
||
public class TestAddressResolverProvider extends InetAddressResolverProvider { | ||
|
||
@Override | ||
public InetAddressResolver get(Configuration configuration) { | ||
return new TestAddressResolver(); | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return "Test Internet Address Resolver Provider"; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...k18-testing/src/test/resources/META-INF/services/java.net.spi.InetAddressResolverProvider
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 @@ | ||
io.opentelemetry.javaagent.tooling.inetaddress.TestAddressResolverProvider |
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