Skip to content

Commit 64a409d

Browse files
Polish
1 parent d223632 commit 64a409d

File tree

3 files changed

+25
-51
lines changed

3 files changed

+25
-51
lines changed

micrometer-observation/src/main/java/io/micrometer/observation/Observation.java

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ static <T extends Context> Observation createNotStarted(String name, Supplier<T>
204204
return NOOP;
205205
}
206206
Context context = contextSupplier.get();
207+
if (context.getName() == null) {
208+
context.setName(name);
209+
}
207210
context.setParentFromCurrentObservation(registry);
208211
context.setLevel(level != null ? level : null);
209212
if (!registry.observationConfig().isObservationEnabled(name, context)) {
@@ -1596,91 +1599,76 @@ class ObservationLevel {
15961599

15971600
private final Level level;
15981601

1599-
private final Class<?> clazz;
1600-
1601-
public ObservationLevel(Level level, Class<?> clazz) {
1602+
public ObservationLevel(Level level) {
16021603
this.level = level;
1603-
this.clazz = clazz;
16041604
}
16051605

16061606
public Level getLevel() {
16071607
return level;
16081608
}
16091609

1610-
public Class<?> getClazz() {
1611-
return clazz;
1612-
}
1613-
16141610
/**
16151611
* Sets {@link Level#ALL} for observation of the given classs.
1616-
* @param clazz class to observe
16171612
* @return observation level
16181613
*/
1619-
public static ObservationLevel all(Class<?> clazz) {
1620-
return new ObservationLevel(Level.ALL, clazz);
1614+
public static ObservationLevel all() {
1615+
return new ObservationLevel(Level.ALL);
16211616
}
16221617

16231618
/**
16241619
* Sets {@link Level#TRACE} for observation of the given classs.
1625-
* @param clazz class to observe
16261620
* @return observation level
16271621
*/
1628-
public static ObservationLevel trace(Class<?> clazz) {
1629-
return new ObservationLevel(Level.TRACE, clazz);
1622+
public static ObservationLevel trace() {
1623+
return new ObservationLevel(Level.TRACE);
16301624
}
16311625

16321626
/**
16331627
* Sets {@link Level#DEBUG} for observation of the given classs.
1634-
* @param clazz class to observe
16351628
* @return observation level
16361629
*/
1637-
public static ObservationLevel debug(Class<?> clazz) {
1638-
return new ObservationLevel(Level.DEBUG, clazz);
1630+
public static ObservationLevel debug() {
1631+
return new ObservationLevel(Level.DEBUG);
16391632
}
16401633

16411634
/**
16421635
* Sets {@link Level#INFO} for observation of the given classs.
1643-
* @param clazz class to observe
16441636
* @return observation level
16451637
*/
1646-
public static ObservationLevel info(Class<?> clazz) {
1647-
return new ObservationLevel(Level.INFO, clazz);
1638+
public static ObservationLevel info() {
1639+
return new ObservationLevel(Level.INFO);
16481640
}
16491641

16501642
/**
16511643
* Sets {@link Level#WARN} for observation of the given classs.
1652-
* @param clazz class to observe
16531644
* @return observation level
16541645
*/
1655-
public static ObservationLevel warn(Class<?> clazz) {
1656-
return new ObservationLevel(Level.WARN, clazz);
1646+
public static ObservationLevel warn() {
1647+
return new ObservationLevel(Level.WARN);
16571648
}
16581649

16591650
/**
16601651
* Sets {@link Level#ERROR} for observation of the given classs.
1661-
* @param clazz class to observe
16621652
* @return observation level
16631653
*/
1664-
public static ObservationLevel error(Class<?> clazz) {
1665-
return new ObservationLevel(Level.ERROR, clazz);
1654+
public static ObservationLevel error() {
1655+
return new ObservationLevel(Level.ERROR);
16661656
}
16671657

16681658
/**
16691659
* Sets {@link Level#FATAL} for observation of the given classs.
1670-
* @param clazz class to observe
16711660
* @return observation level
16721661
*/
1673-
public static ObservationLevel fatal(Class<?> clazz) {
1674-
return new ObservationLevel(Level.FATAL, clazz);
1662+
public static ObservationLevel fatal() {
1663+
return new ObservationLevel(Level.FATAL);
16751664
}
16761665

16771666
/**
16781667
* Sets {@link Level#OFF} for observation of the given classs.
1679-
* @param clazz class to observe
16801668
* @return observation level
16811669
*/
1682-
public static ObservationLevel off(Class<?> clazz) {
1683-
return new ObservationLevel(Level.OFF, clazz);
1670+
public static ObservationLevel off() {
1671+
return new ObservationLevel(Level.OFF);
16841672
}
16851673

16861674
}

micrometer-observation/src/main/java/io/micrometer/observation/ObservationRegistry.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.concurrent.ConcurrentHashMap;
2525
import java.util.concurrent.CopyOnWriteArrayList;
2626
import java.util.function.Supplier;
27-
import java.util.stream.Collectors;
2827

2928
/**
3029
* Implementations of this interface are responsible for managing state of an
@@ -212,21 +211,9 @@ boolean isObservationEnabled(String name, @Nullable Observation.Context context)
212211
if (level == null) {
213212
return true;
214213
}
215-
Class<?> clazz = level.getClazz();
216-
String classToObserveFqn = clazz.getCanonicalName();
217-
String classToObservePackage = clazz.getPackage().getName();
218-
// a.b.c - 2
219-
// a.b.c.D - 3
220-
// a.b - 1
221-
// we sort by string length, that means that we will find the closest
222-
// matching first
223-
List<Entry<String, Level>> sortedLevels = this.observationLevels.entrySet()
224-
.stream()
225-
.sorted(Collections.reverseOrder(Comparator.comparingInt(value -> value.getKey().length())))
226-
.collect(Collectors.toList());
227-
for (Entry<String, Level> levelEntry : sortedLevels) {
228-
if (classToObserveFqn.equals(levelEntry.getKey())
229-
|| classToObservePackage.contains(levelEntry.getKey())) {
214+
String observationName = context.getName();
215+
for (Entry<String, Level> levelEntry : this.observationLevels.entrySet()) {
216+
if (levelEntry.getKey().equalsIgnoreCase(observationName)) {
230217
// exact or partial match
231218
// e.g. ctx has INFO (3), configured is DEBUG (2)
232219
return level.getLevel().ordinal() >= levelEntry.getValue().ordinal();

micrometer-observation/src/test/java/io/micrometer/observation/ObservationTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ void notMatchingObservationPredicateShouldResultInNoopObservation() {
7979
void notMatchingObservationLevelShouldResultInPassthroughObservation() {
8080
registry.observationConfig().observationHandler(context -> true);
8181
registry.observationConfig().observationPredicate((s, context) -> true);
82-
registry.observationConfig().observationLevel("io.micrometer", Level.TRACE);
83-
registry.observationConfig().observationLevel("io.micrometer.observation", Level.ERROR);
82+
registry.observationConfig().observationLevel("foo", Level.ERROR);
8483

85-
Observation observation = Observation.createNotStarted("foo", ObservationLevel.debug(getClass()), registry);
84+
Observation observation = Observation.createNotStarted("foo", ObservationLevel.debug(), registry);
8685

8786
assertThat(observation).isInstanceOf(PassthroughNoopObservation.class);
8887
}

0 commit comments

Comments
 (0)