Retry strict context check failures for library instrumenation tests (#4826)

This commit is contained in:
Lauri Tulmin 2021-12-08 01:35:18 +02:00 committed by GitHub
parent 902748cfd5
commit bba587ea01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 15 deletions

View File

@ -30,7 +30,7 @@ public final class ContextStorageCloser {
} }
private static void cleanup(AutoCloseable storage) throws Exception { private static void cleanup(AutoCloseable storage) throws Exception {
ContextRestorer restorer = ContextRestorer.create(); ContextRestorer restorer = ContextRestorer.create((ContextStorage) storage);
if (restorer == null) { if (restorer == null) {
storage.close(); storage.close();
return; return;
@ -43,6 +43,7 @@ public final class ContextStorageCloser {
.ignoreException(AssertionError.class) .ignoreException(AssertionError.class)
.atMost(Duration.ofSeconds(10)) .atMost(Duration.ofSeconds(10))
.pollInterval(Duration.ofSeconds(1)) .pollInterval(Duration.ofSeconds(1))
.pollDelay(Duration.ZERO)
.until(() -> restorer.runWithRestore(storage)); .until(() -> restorer.runWithRestore(storage));
} }
@ -74,18 +75,15 @@ public final class ContextStorageCloser {
} }
} }
static ContextRestorer create() static ContextRestorer create(ContextStorage storage)
throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { throws NoSuchFieldException, IllegalAccessException {
Object strictContextStorage = getStrictContextStorage(); Object strictContextStorage = getStrictContextStorage(storage);
if (strictContextStorage == null) { if (strictContextStorage == null) {
return null; return null;
} }
Object pendingScopes = getStrictContextStoragePendingScopes(strictContextStorage);
Class<?> pendingScopesClass = Object pendingScopes = getStrictContextStoragePendingScopes(strictContextStorage);
Class.forName( Field mapField = pendingScopes.getClass().getDeclaredField("map");
"io.opentelemetry.javaagent.shaded.io.opentelemetry.context.StrictContextStorage$PendingScopes");
Field mapField = pendingScopesClass.getDeclaredField("map");
mapField.setAccessible(true); mapField.setAccessible(true);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ConcurrentHashMap<Object, Object> map = ConcurrentHashMap<Object, Object> map =
@ -101,17 +99,20 @@ public final class ContextStorageCloser {
} }
private static Object getStrictContextStoragePendingScopes(Object strictContextStorage) private static Object getStrictContextStoragePendingScopes(Object strictContextStorage)
throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException { throws NoSuchFieldException, IllegalAccessException {
Class<?> strictContextStorageClass = Field field = strictContextStorage.getClass().getDeclaredField("pendingScopes");
Class.forName(
"io.opentelemetry.javaagent.shaded.io.opentelemetry.context.StrictContextStorage");
Field field = strictContextStorageClass.getDeclaredField("pendingScopes");
field.setAccessible(true); field.setAccessible(true);
return field.get(strictContextStorage); return field.get(strictContextStorage);
} }
private static Object getStrictContextStorage() private static Object getStrictContextStorage(ContextStorage storage)
throws NoSuchFieldException, IllegalAccessException { throws NoSuchFieldException, IllegalAccessException {
// in library instrumentation tests we already have access to StrictContextStorage
if (storage.getClass().getName().contains("StrictContextStorage")) {
return storage;
}
// in javaagent tests the storage we get is wrapped by opentelemetry api bridge, find the
// actual storage
Object contextStorage = getAgentContextStorage(); Object contextStorage = getAgentContextStorage();
if (contextStorage == null) { if (contextStorage == null) {
return null; return null;