@@ -90,6 +90,60 @@ static <T extends Context> Observation start(String name, Supplier<T> contextSup
90
90
return createNotStarted (name , contextSupplier , registry ).start ();
91
91
}
92
92
93
+ /**
94
+ * Create and start an {@link Observation} with the given name. All Observations of
95
+ * the same type must share the same name.
96
+ * <p>
97
+ * When no registry is passed or the observation is
98
+ * {@link ObservationRegistry.ObservationConfig#observationPredicate(ObservationPredicate)
99
+ * not applicable}, a no-op observation will be returned.
100
+ * @param name name of the observation
101
+ * @param level observation level
102
+ * @param registry observation registry
103
+ * @return a started observation
104
+ * @since 1.13.0
105
+ */
106
+ static Observation start (String name , ObservationLevel level , @ Nullable ObservationRegistry registry ) {
107
+ return start (name , Context ::new , level , registry );
108
+ }
109
+
110
+ /**
111
+ * Creates and starts an {@link Observation}. When the {@link ObservationRegistry} is
112
+ * null or the no-op registry, this fast returns a no-op {@link Observation} and skips
113
+ * the creation of the {@link Observation.Context}. This check avoids unnecessary
114
+ * {@link Observation.Context} creation, which is why it takes a {@link Supplier} for
115
+ * the context rather than the context directly. If the observation is not enabled
116
+ * (see
117
+ * {@link ObservationRegistry.ObservationConfig#observationPredicate(ObservationPredicate)
118
+ * ObservationConfig#observationPredicate}), a no-op observation will also be
119
+ * returned.
120
+ * @param name name of the observation
121
+ * @param contextSupplier mutable context supplier
122
+ * @param level observation level
123
+ * @param registry observation registry
124
+ * @return started observation
125
+ * @since 1.13.0
126
+ */
127
+ static <T extends Context > Observation start (String name , Supplier <T > contextSupplier , ObservationLevel level ,
128
+ @ Nullable ObservationRegistry registry ) {
129
+ return createNotStarted (name , contextSupplier , level , registry ).start ();
130
+ }
131
+
132
+ /**
133
+ * Creates but <b>does not start</b> an {@link Observation}. Remember to call
134
+ * {@link Observation#start()} when you want the measurements to start. When no
135
+ * registry is passed or observation is not applicable will return a no-op
136
+ * observation.
137
+ * @param name name of the observation
138
+ * @param level observation level
139
+ * @param registry observation registry
140
+ * @return created but not started observation
141
+ * @since 1.13.0
142
+ */
143
+ static Observation createNotStarted (String name , ObservationLevel level , @ Nullable ObservationRegistry registry ) {
144
+ return createNotStarted (name , Context ::new , level , registry );
145
+ }
146
+
93
147
/**
94
148
* Creates but <b>does not start</b> an {@link Observation}. Remember to call
95
149
* {@link Observation#start()} when you want the measurements to start. When no
@@ -122,13 +176,38 @@ static Observation createNotStarted(String name, @Nullable ObservationRegistry r
122
176
*/
123
177
static <T extends Context > Observation createNotStarted (String name , Supplier <T > contextSupplier ,
124
178
@ Nullable ObservationRegistry registry ) {
179
+ return createNotStarted (name , contextSupplier , null , registry );
180
+ }
181
+
182
+ /**
183
+ * Creates but <b>does not start</b> an {@link Observation}. Remember to call
184
+ * {@link Observation#start()} when you want the measurements to start. When the
185
+ * {@link ObservationRegistry} is null or the no-op registry, this fast returns a
186
+ * no-op {@link Observation} and skips the creation of the
187
+ * {@link Observation.Context}. This check avoids unnecessary
188
+ * {@link Observation.Context} creation, which is why it takes a {@link Supplier} for
189
+ * the context rather than the context directly. If the observation is not enabled
190
+ * (see
191
+ * {@link ObservationRegistry.ObservationConfig#observationPredicate(ObservationPredicate)
192
+ * ObservationConfig#observationPredicate}), a no-op observation will also be
193
+ * returned.
194
+ * @param name name of the observation
195
+ * @param contextSupplier supplier for mutable context
196
+ * @param level observation level
197
+ * @param registry observation registry
198
+ * @return created but not started observation
199
+ * @since 1.13.0
200
+ */
201
+ static <T extends Context > Observation createNotStarted (String name , Supplier <T > contextSupplier ,
202
+ @ Nullable ObservationLevel level , @ Nullable ObservationRegistry registry ) {
125
203
if (registry == null || registry .isNoop ()) {
126
204
return NOOP ;
127
205
}
128
206
Context context = contextSupplier .get ();
129
207
context .setParentFromCurrentObservation (registry );
208
+ context .setLevel (level != null ? level : null );
130
209
if (!registry .observationConfig ().isObservationEnabled (name , context )) {
131
- return NOOP ;
210
+ return new PassthroughNoopObservation ( context . getParentObservation ()) ;
132
211
}
133
212
return new SimpleObservation (name , registry , context );
134
213
}
@@ -178,7 +257,7 @@ static <T extends Context> Observation createNotStarted(@Nullable ObservationCon
178
257
convention = registry .observationConfig ().getObservationConvention (context , defaultConvention );
179
258
}
180
259
if (!registry .observationConfig ().isObservationEnabled (convention .getName (), context )) {
181
- return NOOP ;
260
+ return new PassthroughNoopObservation ( context . getParentObservation ()) ;
182
261
}
183
262
return new SimpleObservation (convention , registry , context );
184
263
}
@@ -316,7 +395,7 @@ static <T extends Context> Observation createNotStarted(ObservationConvention<T>
316
395
T context = contextSupplier .get ();
317
396
context .setParentFromCurrentObservation (registry );
318
397
if (!registry .observationConfig ().isObservationEnabled (observationConvention .getName (), context )) {
319
- return NOOP ;
398
+ return new PassthroughNoopObservation ( context . getParentObservation ()) ;
320
399
}
321
400
return new SimpleObservation (observationConvention , registry , context );
322
401
}
@@ -417,7 +496,7 @@ default Observation highCardinalityKeyValues(KeyValues keyValues) {
417
496
* @return {@code true} when this is a no-op observation
418
497
*/
419
498
default boolean isNoop () {
420
- return this == NOOP ;
499
+ return this == NOOP || this instanceof NoopObservation ;
421
500
}
422
501
423
502
/**
@@ -923,7 +1002,13 @@ class Context implements ContextView {
923
1002
private Throwable error ;
924
1003
925
1004
@ Nullable
926
- private ObservationView parentObservation ;
1005
+ private ObservationView thisObservation ;
1006
+
1007
+ @ Nullable
1008
+ private ObservationView parentObservationView ;
1009
+
1010
+ @ Nullable
1011
+ private ObservationLevel level ;
927
1012
928
1013
private final Map <String , KeyValue > lowCardinalityKeyValues = new LinkedHashMap <>();
929
1014
@@ -970,15 +1055,15 @@ public void setContextualName(@Nullable String contextualName) {
970
1055
*/
971
1056
@ Nullable
972
1057
public ObservationView getParentObservation () {
973
- return parentObservation ;
1058
+ return parentObservationView ;
974
1059
}
975
1060
976
1061
/**
977
1062
* Sets the parent {@link ObservationView}.
978
1063
* @param parentObservation parent observation to set
979
1064
*/
980
1065
public void setParentObservation (@ Nullable ObservationView parentObservation ) {
981
- this .parentObservation = parentObservation ;
1066
+ this .parentObservationView = parentObservation ;
982
1067
}
983
1068
984
1069
/**
@@ -987,7 +1072,7 @@ public void setParentObservation(@Nullable ObservationView parentObservation) {
987
1072
* @param registry the {@link ObservationRegistry} in using
988
1073
*/
989
1074
void setParentFromCurrentObservation (ObservationRegistry registry ) {
990
- if (this .parentObservation == null ) {
1075
+ if (this .parentObservationView == null ) {
991
1076
Observation currentObservation = registry .getCurrentObservation ();
992
1077
if (currentObservation != null ) {
993
1078
setParentObservation (currentObservation );
@@ -1232,12 +1317,21 @@ public KeyValues getAllKeyValues() {
1232
1317
return getLowCardinalityKeyValues ().and (getHighCardinalityKeyValues ());
1233
1318
}
1234
1319
1320
+ @ Nullable
1321
+ public ObservationLevel getLevel () {
1322
+ return level ;
1323
+ }
1324
+
1325
+ void setLevel (ObservationLevel level ) {
1326
+ this .level = level ;
1327
+ }
1328
+
1235
1329
@ Override
1236
1330
public String toString () {
1237
1331
return "name='" + name + '\'' + ", contextualName='" + contextualName + '\'' + ", error='" + error + '\''
1238
1332
+ ", lowCardinalityKeyValues=" + toString (getLowCardinalityKeyValues ())
1239
1333
+ ", highCardinalityKeyValues=" + toString (getHighCardinalityKeyValues ()) + ", map=" + toString (map )
1240
- + ", parentObservation=" + parentObservation ;
1334
+ + ", parentObservation=" + parentObservationView + ", observationLevel=" + level ;
1241
1335
}
1242
1336
1243
1337
private String toString (KeyValues keyValues ) {
@@ -1452,6 +1546,14 @@ default <T> T getOrDefault(Object key, Supplier<T> defaultObjectSupplier) {
1452
1546
@ NonNull
1453
1547
KeyValues getAllKeyValues ();
1454
1548
1549
+ /**
1550
+ * Returns the observation level.
1551
+ * @return observation level
1552
+ */
1553
+ default Level getObservationLevel () {
1554
+ return Level .ALL ;
1555
+ }
1556
+
1455
1557
}
1456
1558
1457
1559
/**
@@ -1487,4 +1589,103 @@ interface CheckedFunction<T, R, E extends Throwable> {
1487
1589
1488
1590
}
1489
1591
1592
+ /**
1593
+ * Mapping of {@link Level} to {@link Class}.
1594
+ *
1595
+ * @author Marcin Grzejszczak
1596
+ * @since 1.13.0
1597
+ */
1598
+ class ObservationLevel {
1599
+
1600
+ private final Level level ;
1601
+
1602
+ private final Class <?> clazz ;
1603
+
1604
+ public ObservationLevel (Level level , Class <?> clazz ) {
1605
+ this .level = level ;
1606
+ this .clazz = clazz ;
1607
+ }
1608
+
1609
+ public Level getLevel () {
1610
+ return level ;
1611
+ }
1612
+
1613
+ public Class <?> getClazz () {
1614
+ return clazz ;
1615
+ }
1616
+
1617
+ /**
1618
+ * Sets {@link Level#ALL} for observation of the given classs.
1619
+ * @param clazz class to observe
1620
+ * @return observation level
1621
+ */
1622
+ public static ObservationLevel all (Class <?> clazz ) {
1623
+ return new ObservationLevel (Level .ALL , clazz );
1624
+ }
1625
+
1626
+ /**
1627
+ * Sets {@link Level#TRACE} for observation of the given classs.
1628
+ * @param clazz class to observe
1629
+ * @return observation level
1630
+ */
1631
+ public static ObservationLevel trace (Class <?> clazz ) {
1632
+ return new ObservationLevel (Level .TRACE , clazz );
1633
+ }
1634
+
1635
+ /**
1636
+ * Sets {@link Level#DEBUG} for observation of the given classs.
1637
+ * @param clazz class to observe
1638
+ * @return observation level
1639
+ */
1640
+ public static ObservationLevel debug (Class <?> clazz ) {
1641
+ return new ObservationLevel (Level .DEBUG , clazz );
1642
+ }
1643
+
1644
+ /**
1645
+ * Sets {@link Level#INFO} for observation of the given classs.
1646
+ * @param clazz class to observe
1647
+ * @return observation level
1648
+ */
1649
+ public static ObservationLevel info (Class <?> clazz ) {
1650
+ return new ObservationLevel (Level .INFO , clazz );
1651
+ }
1652
+
1653
+ /**
1654
+ * Sets {@link Level#WARN} for observation of the given classs.
1655
+ * @param clazz class to observe
1656
+ * @return observation level
1657
+ */
1658
+ public static ObservationLevel warn (Class <?> clazz ) {
1659
+ return new ObservationLevel (Level .WARN , clazz );
1660
+ }
1661
+
1662
+ /**
1663
+ * Sets {@link Level#ERROR} for observation of the given classs.
1664
+ * @param clazz class to observe
1665
+ * @return observation level
1666
+ */
1667
+ public static ObservationLevel error (Class <?> clazz ) {
1668
+ return new ObservationLevel (Level .ERROR , clazz );
1669
+ }
1670
+
1671
+ /**
1672
+ * Sets {@link Level#FATAL} for observation of the given classs.
1673
+ * @param clazz class to observe
1674
+ * @return observation level
1675
+ */
1676
+ public static ObservationLevel fatal (Class <?> clazz ) {
1677
+ return new ObservationLevel (Level .FATAL , clazz );
1678
+ }
1679
+
1680
+ /**
1681
+ * Sets {@link Level#OFF} for observation of the given classs.
1682
+ * @param clazz class to observe
1683
+ * @return observation level
1684
+ */
1685
+ public static ObservationLevel off (Class <?> clazz ) {
1686
+ return new ObservationLevel (Level .OFF , clazz );
1687
+ }
1688
+
1689
+ }
1690
+
1490
1691
}
0 commit comments