@@ -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,10 @@ class Context implements ContextView {
923
1002
private Throwable error ;
924
1003
925
1004
@ Nullable
926
- private ObservationView parentObservation ;
1005
+ private ObservationView parentObservationView ;
1006
+
1007
+ @ Nullable
1008
+ private ObservationLevel level ;
927
1009
928
1010
private final Map <String , KeyValue > lowCardinalityKeyValues = new LinkedHashMap <>();
929
1011
@@ -970,15 +1052,15 @@ public void setContextualName(@Nullable String contextualName) {
970
1052
*/
971
1053
@ Nullable
972
1054
public ObservationView getParentObservation () {
973
- return parentObservation ;
1055
+ return parentObservationView ;
974
1056
}
975
1057
976
1058
/**
977
1059
* Sets the parent {@link ObservationView}.
978
1060
* @param parentObservation parent observation to set
979
1061
*/
980
1062
public void setParentObservation (@ Nullable ObservationView parentObservation ) {
981
- this .parentObservation = parentObservation ;
1063
+ this .parentObservationView = parentObservation ;
982
1064
}
983
1065
984
1066
/**
@@ -987,7 +1069,7 @@ public void setParentObservation(@Nullable ObservationView parentObservation) {
987
1069
* @param registry the {@link ObservationRegistry} in using
988
1070
*/
989
1071
void setParentFromCurrentObservation (ObservationRegistry registry ) {
990
- if (this .parentObservation == null ) {
1072
+ if (this .parentObservationView == null ) {
991
1073
Observation currentObservation = registry .getCurrentObservation ();
992
1074
if (currentObservation != null ) {
993
1075
setParentObservation (currentObservation );
@@ -1232,12 +1314,21 @@ public KeyValues getAllKeyValues() {
1232
1314
return getLowCardinalityKeyValues ().and (getHighCardinalityKeyValues ());
1233
1315
}
1234
1316
1317
+ @ Nullable
1318
+ public ObservationLevel getLevel () {
1319
+ return level ;
1320
+ }
1321
+
1322
+ void setLevel (ObservationLevel level ) {
1323
+ this .level = level ;
1324
+ }
1325
+
1235
1326
@ Override
1236
1327
public String toString () {
1237
1328
return "name='" + name + '\'' + ", contextualName='" + contextualName + '\'' + ", error='" + error + '\''
1238
1329
+ ", lowCardinalityKeyValues=" + toString (getLowCardinalityKeyValues ())
1239
1330
+ ", highCardinalityKeyValues=" + toString (getHighCardinalityKeyValues ()) + ", map=" + toString (map )
1240
- + ", parentObservation=" + parentObservation ;
1331
+ + ", parentObservation=" + parentObservationView + ", observationLevel=" + level ;
1241
1332
}
1242
1333
1243
1334
private String toString (KeyValues keyValues ) {
@@ -1452,6 +1543,14 @@ default <T> T getOrDefault(Object key, Supplier<T> defaultObjectSupplier) {
1452
1543
@ NonNull
1453
1544
KeyValues getAllKeyValues ();
1454
1545
1546
+ /**
1547
+ * Returns the observation level.
1548
+ * @return observation level
1549
+ */
1550
+ default Level getObservationLevel () {
1551
+ return Level .ALL ;
1552
+ }
1553
+
1455
1554
}
1456
1555
1457
1556
/**
@@ -1487,4 +1586,103 @@ interface CheckedFunction<T, R, E extends Throwable> {
1487
1586
1488
1587
}
1489
1588
1589
+ /**
1590
+ * Mapping of {@link Level} to {@link Class}.
1591
+ *
1592
+ * @author Marcin Grzejszczak
1593
+ * @since 1.13.0
1594
+ */
1595
+ class ObservationLevel {
1596
+
1597
+ private final Level level ;
1598
+
1599
+ private final Class <?> clazz ;
1600
+
1601
+ public ObservationLevel (Level level , Class <?> clazz ) {
1602
+ this .level = level ;
1603
+ this .clazz = clazz ;
1604
+ }
1605
+
1606
+ public Level getLevel () {
1607
+ return level ;
1608
+ }
1609
+
1610
+ public Class <?> getClazz () {
1611
+ return clazz ;
1612
+ }
1613
+
1614
+ /**
1615
+ * Sets {@link Level#ALL} for observation of the given classs.
1616
+ * @param clazz class to observe
1617
+ * @return observation level
1618
+ */
1619
+ public static ObservationLevel all (Class <?> clazz ) {
1620
+ return new ObservationLevel (Level .ALL , clazz );
1621
+ }
1622
+
1623
+ /**
1624
+ * Sets {@link Level#TRACE} for observation of the given classs.
1625
+ * @param clazz class to observe
1626
+ * @return observation level
1627
+ */
1628
+ public static ObservationLevel trace (Class <?> clazz ) {
1629
+ return new ObservationLevel (Level .TRACE , clazz );
1630
+ }
1631
+
1632
+ /**
1633
+ * Sets {@link Level#DEBUG} for observation of the given classs.
1634
+ * @param clazz class to observe
1635
+ * @return observation level
1636
+ */
1637
+ public static ObservationLevel debug (Class <?> clazz ) {
1638
+ return new ObservationLevel (Level .DEBUG , clazz );
1639
+ }
1640
+
1641
+ /**
1642
+ * Sets {@link Level#INFO} for observation of the given classs.
1643
+ * @param clazz class to observe
1644
+ * @return observation level
1645
+ */
1646
+ public static ObservationLevel info (Class <?> clazz ) {
1647
+ return new ObservationLevel (Level .INFO , clazz );
1648
+ }
1649
+
1650
+ /**
1651
+ * Sets {@link Level#WARN} for observation of the given classs.
1652
+ * @param clazz class to observe
1653
+ * @return observation level
1654
+ */
1655
+ public static ObservationLevel warn (Class <?> clazz ) {
1656
+ return new ObservationLevel (Level .WARN , clazz );
1657
+ }
1658
+
1659
+ /**
1660
+ * Sets {@link Level#ERROR} for observation of the given classs.
1661
+ * @param clazz class to observe
1662
+ * @return observation level
1663
+ */
1664
+ public static ObservationLevel error (Class <?> clazz ) {
1665
+ return new ObservationLevel (Level .ERROR , clazz );
1666
+ }
1667
+
1668
+ /**
1669
+ * Sets {@link Level#FATAL} for observation of the given classs.
1670
+ * @param clazz class to observe
1671
+ * @return observation level
1672
+ */
1673
+ public static ObservationLevel fatal (Class <?> clazz ) {
1674
+ return new ObservationLevel (Level .FATAL , clazz );
1675
+ }
1676
+
1677
+ /**
1678
+ * Sets {@link Level#OFF} for observation of the given classs.
1679
+ * @param clazz class to observe
1680
+ * @return observation level
1681
+ */
1682
+ public static ObservationLevel off (Class <?> clazz ) {
1683
+ return new ObservationLevel (Level .OFF , clazz );
1684
+ }
1685
+
1686
+ }
1687
+
1490
1688
}
0 commit comments