@@ -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 ();
@@ -130,4 +130,54 @@ void nestedScopes_makeCurrent() {
130130 assertThat (registry .getCurrentObservationScope ()).isNull ();
131131 }
132132
133+ @ Test
134+ void currentShouldBePropagatedAcrossThreads () throws Exception {
135+ ExecutorService executor = Executors .newSingleThreadExecutor ();
136+ assertThat (registry .getCurrentObservation ()).isNull ();
137+ Observation .createNotStarted ("a" , registry ).observeChecked (() -> doA (executor , registry ));
138+ assertThat (registry .getCurrentObservation ()).isNull ();
139+ }
140+
141+ @ Test
142+ void currentShouldBePropagatedAcrossThreadsEvenIfObservationIsDisabledByAnObservationPredicate () throws Exception {
143+ ObservationRegistry registry = ObservationRegistry .create ();
144+ registry .observationConfig ()
145+ .observationHandler (context -> true )
146+ // .observationHandler(new ObservationTextPublisher())
147+ .observationPredicate ((name , context ) -> !name .equals ("b" ));
148+ ExecutorService executor = Executors .newSingleThreadExecutor ();
149+ assertThat (registry .getCurrentObservation ()).isNull ();
150+ Observation .createNotStarted ("a" , registry ).observeChecked (() -> doA (executor , registry ));
151+ assertThat (registry .getCurrentObservation ()).isNull ();
152+ }
153+
154+ private void doA (ExecutorService executor , ObservationRegistry registry ) throws Exception {
155+ // System.out.println("A...");
156+ assertThat (registry .getCurrentObservation ()).isNotNull ();
157+ assertThat (registry .getCurrentObservation ().getContextView ().getName ()).isEqualTo ("a" );
158+
159+ Observation b = Observation .createNotStarted ("b" , registry ).start ();
160+ executor .submit (() -> {
161+ try (Observation .Scope ignored = b .openScope ()) {
162+ assertThat (registry .getCurrentObservation ()).isSameAs (b );
163+ doB (registry );
164+ assertThat (registry .getCurrentObservation ()).isSameAs (b );
165+ }
166+ assertThat (registry .getCurrentObservation ()).isNull ();
167+ }).get ();
168+ assertThat (registry .getCurrentObservation ()).isNotNull ();
169+ assertThat (registry .getCurrentObservation ().getContextView ().getName ()).isEqualTo ("a" );
170+ }
171+
172+ private void doB (ObservationRegistry registry ) {
173+ // System.out.println("B...");
174+ Observation .createNotStarted ("c" , registry ).observe (() -> doC (registry ));
175+ }
176+
177+ private void doC (ObservationRegistry registry ) {
178+ // System.out.println("C...");
179+ assertThat (registry .getCurrentObservation ()).isNotNull ();
180+ assertThat (registry .getCurrentObservation ().getContextView ().getName ()).isEqualTo ("c" );
181+ }
182+
133183}
0 commit comments