@@ -37,14 +37,14 @@ void setup() {
3737
3838 @ Test
3939 void nestedSamples_parentChildThreadsInstrumented () throws ExecutionException , InterruptedException {
40- ExecutorService taskRunner = Executors .newSingleThreadExecutor ();
40+ ExecutorService executor = Executors .newSingleThreadExecutor ();
4141
4242 Observation observation = Observation .createNotStarted ("test.observation" , registry );
4343 System .out .println ("Outside task: " + observation );
4444 assertThat (registry .getCurrentObservation ()).isNull ();
4545 try (Observation .Scope scope = observation .openScope ()) {
4646 assertThat (registry .getCurrentObservation ()).isSameAs (observation );
47- taskRunner .submit (() -> {
47+ executor .submit (() -> {
4848 System .out .println ("In task: " + registry .getCurrentObservation ());
4949 assertThat (registry .getCurrentObservation ()).isNotEqualTo (observation );
5050 }).get ();
@@ -131,4 +131,54 @@ void nestedScopes_makeCurrent() {
131131 assertThat (registry .getCurrentObservationScope ()).isNull ();
132132 }
133133
134+ @ Test
135+ void currentShouldBePropagatedAcrossThreads () throws Exception {
136+ ExecutorService executor = Executors .newSingleThreadExecutor ();
137+ assertThat (registry .getCurrentObservation ()).isNull ();
138+ Observation .createNotStarted ("a" , registry ).observeChecked (() -> doA (executor , registry ));
139+ assertThat (registry .getCurrentObservation ()).isNull ();
140+ }
141+
142+ @ Test
143+ void currentShouldBePropagatedAcrossThreadsEvenIfObservationIsDisabledByAnObservationPredicate () throws Exception {
144+ ObservationRegistry registry = ObservationRegistry .create ();
145+ registry .observationConfig ()
146+ .observationHandler (context -> true )
147+ // .observationHandler(new ObservationTextPublisher())
148+ .observationPredicate ((name , context ) -> !name .equals ("b" ));
149+ ExecutorService executor = Executors .newSingleThreadExecutor ();
150+ assertThat (registry .getCurrentObservation ()).isNull ();
151+ Observation .createNotStarted ("a" , registry ).observeChecked (() -> doA (executor , registry ));
152+ assertThat (registry .getCurrentObservation ()).isNull ();
153+ }
154+
155+ private void doA (ExecutorService executor , ObservationRegistry registry ) throws Exception {
156+ // System.out.println("A...");
157+ assertThat (registry .getCurrentObservation ()).isNotNull ();
158+ assertThat (registry .getCurrentObservation ().getContextView ().getName ()).isEqualTo ("a" );
159+
160+ Observation b = Observation .createNotStarted ("b" , registry ).start ();
161+ executor .submit (() -> {
162+ try (Observation .Scope ignored = b .openScope ()) {
163+ assertThat (registry .getCurrentObservation ()).isSameAs (b );
164+ doB (registry );
165+ assertThat (registry .getCurrentObservation ()).isSameAs (b );
166+ }
167+ assertThat (registry .getCurrentObservation ()).isNull ();
168+ }).get ();
169+ assertThat (registry .getCurrentObservation ()).isNotNull ();
170+ assertThat (registry .getCurrentObservation ().getContextView ().getName ()).isEqualTo ("a" );
171+ }
172+
173+ private void doB (ObservationRegistry registry ) {
174+ // System.out.println("B...");
175+ Observation .createNotStarted ("c" , registry ).observe (() -> doC (registry ));
176+ }
177+
178+ private void doC (ObservationRegistry registry ) {
179+ // System.out.println("C...");
180+ assertThat (registry .getCurrentObservation ()).isNotNull ();
181+ assertThat (registry .getCurrentObservation ().getContextView ().getName ()).isEqualTo ("c" );
182+ }
183+
134184}
0 commit comments