Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed Jan 30, 2024
1 parent d144742 commit 3396c1a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 51 deletions.
Expand Up @@ -204,6 +204,9 @@ static <T extends Context> Observation createNotStarted(String name, Supplier<T>
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)) {
Expand Down Expand Up @@ -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);
}

}
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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<Entry<String, Level>> sortedLevels = this.observationLevels.entrySet()
.stream()
.sorted(Collections.reverseOrder(Comparator.comparingInt(value -> value.getKey().length())))
.collect(Collectors.toList());
for (Entry<String, Level> levelEntry : sortedLevels) {
if (classToObserveFqn.equals(levelEntry.getKey())
|| classToObservePackage.contains(levelEntry.getKey())) {
String observationName = context.getName();
for (Entry<String, Level> 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();
Expand Down
Expand Up @@ -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);
}
Expand Down

0 comments on commit 3396c1a

Please sign in to comment.