diff --git a/micrometer-observation/src/main/java/io/micrometer/observation/Observation.java b/micrometer-observation/src/main/java/io/micrometer/observation/Observation.java index 11fb802a21..a7586fd29f 100644 --- a/micrometer-observation/src/main/java/io/micrometer/observation/Observation.java +++ b/micrometer-observation/src/main/java/io/micrometer/observation/Observation.java @@ -204,6 +204,9 @@ static Observation createNotStarted(String name, Supplier return NOOP; } Context context = contextSupplier.get(); + if (context.getName() == null) { + context.setName(name); + } context.setParentFromCurrentObservation(registry); context.setLevel(level != null ? level : null); if (!registry.observationConfig().isObservationEnabled(name, context)) { @@ -1596,91 +1599,76 @@ class ObservationLevel { private final Level level; - private final Class clazz; - - public ObservationLevel(Level level, Class clazz) { + public ObservationLevel(Level level) { this.level = level; - this.clazz = clazz; } public Level getLevel() { return level; } - public Class getClazz() { - return clazz; - } - /** * Sets {@link Level#ALL} for observation of the given classs. - * @param clazz class to observe * @return observation level */ - public static ObservationLevel all(Class clazz) { - return new ObservationLevel(Level.ALL, clazz); + public static ObservationLevel all() { + return new ObservationLevel(Level.ALL); } /** * Sets {@link Level#TRACE} for observation of the given classs. - * @param clazz class to observe * @return observation level */ - public static ObservationLevel trace(Class clazz) { - return new ObservationLevel(Level.TRACE, clazz); + public static ObservationLevel trace() { + return new ObservationLevel(Level.TRACE); } /** * Sets {@link Level#DEBUG} for observation of the given classs. - * @param clazz class to observe * @return observation level */ - public static ObservationLevel debug(Class clazz) { - return new ObservationLevel(Level.DEBUG, clazz); + public static ObservationLevel debug() { + return new ObservationLevel(Level.DEBUG); } /** * Sets {@link Level#INFO} for observation of the given classs. - * @param clazz class to observe * @return observation level */ - public static ObservationLevel info(Class clazz) { - return new ObservationLevel(Level.INFO, clazz); + public static ObservationLevel info() { + return new ObservationLevel(Level.INFO); } /** * Sets {@link Level#WARN} for observation of the given classs. - * @param clazz class to observe * @return observation level */ - public static ObservationLevel warn(Class clazz) { - return new ObservationLevel(Level.WARN, clazz); + public static ObservationLevel warn() { + return new ObservationLevel(Level.WARN); } /** * Sets {@link Level#ERROR} for observation of the given classs. - * @param clazz class to observe * @return observation level */ - public static ObservationLevel error(Class clazz) { - return new ObservationLevel(Level.ERROR, clazz); + public static ObservationLevel error() { + return new ObservationLevel(Level.ERROR); } /** * Sets {@link Level#FATAL} for observation of the given classs. - * @param clazz class to observe * @return observation level */ - public static ObservationLevel fatal(Class clazz) { - return new ObservationLevel(Level.FATAL, clazz); + public static ObservationLevel fatal() { + return new ObservationLevel(Level.FATAL); } /** * Sets {@link Level#OFF} for observation of the given classs. - * @param clazz class to observe * @return observation level */ - public static ObservationLevel off(Class clazz) { - return new ObservationLevel(Level.OFF, clazz); + public static ObservationLevel off() { + return new ObservationLevel(Level.OFF); } } diff --git a/micrometer-observation/src/main/java/io/micrometer/observation/ObservationRegistry.java b/micrometer-observation/src/main/java/io/micrometer/observation/ObservationRegistry.java index 82aa698694..dd129b9a2e 100644 --- a/micrometer-observation/src/main/java/io/micrometer/observation/ObservationRegistry.java +++ b/micrometer-observation/src/main/java/io/micrometer/observation/ObservationRegistry.java @@ -24,7 +24,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Supplier; -import java.util.stream.Collectors; /** * Implementations of this interface are responsible for managing state of an @@ -212,21 +211,9 @@ boolean isObservationEnabled(String name, @Nullable Observation.Context context) if (level == null) { return true; } - Class clazz = level.getClazz(); - String classToObserveFqn = clazz.getCanonicalName(); - String classToObservePackage = clazz.getPackage().getName(); - // a.b.c - 2 - // a.b.c.D - 3 - // a.b - 1 - // we sort by string length, that means that we will find the closest - // matching first - List> sortedLevels = this.observationLevels.entrySet() - .stream() - .sorted(Collections.reverseOrder(Comparator.comparingInt(value -> value.getKey().length()))) - .collect(Collectors.toList()); - for (Entry levelEntry : sortedLevels) { - if (classToObserveFqn.equals(levelEntry.getKey()) - || classToObservePackage.contains(levelEntry.getKey())) { + String observationName = context.getName(); + for (Entry levelEntry : this.observationLevels.entrySet()) { + if (levelEntry.getKey().equalsIgnoreCase(observationName)) { // exact or partial match // e.g. ctx has INFO (3), configured is DEBUG (2) return level.getLevel().ordinal() >= levelEntry.getValue().ordinal(); diff --git a/micrometer-observation/src/test/java/io/micrometer/observation/ObservationTests.java b/micrometer-observation/src/test/java/io/micrometer/observation/ObservationTests.java index d491fb9113..301aaab24b 100644 --- a/micrometer-observation/src/test/java/io/micrometer/observation/ObservationTests.java +++ b/micrometer-observation/src/test/java/io/micrometer/observation/ObservationTests.java @@ -79,10 +79,9 @@ void notMatchingObservationPredicateShouldResultInNoopObservation() { void notMatchingObservationLevelShouldResultInPassthroughObservation() { registry.observationConfig().observationHandler(context -> true); registry.observationConfig().observationPredicate((s, context) -> true); - registry.observationConfig().observationLevel("io.micrometer", Level.TRACE); - registry.observationConfig().observationLevel("io.micrometer.observation", Level.ERROR); + registry.observationConfig().observationLevel("foo", Level.ERROR); - Observation observation = Observation.createNotStarted("foo", ObservationLevel.debug(getClass()), registry); + Observation observation = Observation.createNotStarted("foo", ObservationLevel.debug(), registry); assertThat(observation).isInstanceOf(PassthroughNoopObservation.class); }