Skip to content

Commit

Permalink
Avoid BlockHound NoSuchTypeException when context propagation depende…
Browse files Browse the repository at this point in the history
…ncy is unavailable (#3337)

When the context-propagation jar is unavailable (which can happen since the dependency is optional), then BlockHound reports the following ERROR log:

reactor.blockhound.shaded.net.bytebuddy.pool.TypePool$Resolution$NoSuchTypeException: Cannot resolve type description for io.micrometer.context.ContextRegistry

This log does not break anything, it is only about logging, but it can be avoided using this patch.
In a nutshell, Byte Buddy requires all type information for a transformed type to be available, else the above error log may be displayed. The log may happen occasionally with very rare byte code combinations
when the class is being defined (initial classloading).

To avoid this problem, the proposed patch is refactoring the ContextPropagation static initializer in order to not use a lambda when capturing the context snapshot.
  • Loading branch information
pderop committed Feb 3, 2023
1 parent 89202c4 commit ecb6cd2
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 VMware Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2022-2023 VMware Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,9 +53,9 @@ final class ContextPropagation {
Function<Context, Context> contextCaptureFunction;
boolean contextPropagation;
try {
// The following line will throw a LinkageError (NoClassDefFoundError) in case context propagation is not available
ContextRegistry globalRegistry = ContextRegistry.getInstance();
contextCaptureFunction = target -> ContextSnapshot.captureAllUsing(PREDICATE_TRUE, globalRegistry)
.updateContext(target);
contextCaptureFunction = new ContextCaptureNoPredicate(globalRegistry);
contextPropagation = true;
}
catch (LinkageError t) {
Expand Down Expand Up @@ -280,4 +280,17 @@ public Context addToContext(Context originalContext) {
}
}
}

static final class ContextCaptureNoPredicate implements Function<Context, Context> {
final ContextRegistry globalRegistry;

ContextCaptureNoPredicate(ContextRegistry globalRegistry) {
this.globalRegistry = globalRegistry;
}
@Override
public Context apply(Context context) {
return ContextSnapshot.captureAllUsing(PREDICATE_TRUE, globalRegistry)
.updateContext(context);
}
}
}

0 comments on commit ecb6cd2

Please sign in to comment.