21
21
import java .util .ListIterator ;
22
22
import java .util .concurrent .atomic .AtomicInteger ;
23
23
import java .util .concurrent .atomic .AtomicReference ;
24
+ import java .util .concurrent .locks .Lock ;
25
+ import java .util .concurrent .locks .ReentrantLock ;
24
26
25
27
import io .micrometer .common .KeyValue ;
26
28
import io .micrometer .common .KeyValues ;
@@ -382,6 +384,8 @@ private static final class ObservationReference {
382
384
383
385
private static final ObservationReference NOOP = new ObservationReference (Observation .NOOP );
384
386
387
+ private final Lock lock = new ReentrantLock ();
388
+
385
389
private final AtomicInteger state = new AtomicInteger (0 );
386
390
387
391
private final Observation observation ;
@@ -391,20 +395,38 @@ private ObservationReference(Observation observation) {
391
395
}
392
396
393
397
private void start () {
394
- if (this .state .compareAndSet (0 , 1 )) {
395
- this .observation .start ();
398
+ try {
399
+ this .lock .lock ();
400
+ if (this .state .compareAndSet (0 , 1 )) {
401
+ this .observation .start ();
402
+ }
403
+ }
404
+ finally {
405
+ this .lock .unlock ();
396
406
}
397
407
}
398
408
399
409
private void error (Throwable ex ) {
400
- if (this .state .get () == 1 ) {
401
- this .observation .error (ex );
410
+ try {
411
+ this .lock .lock ();
412
+ if (this .state .get () == 1 ) {
413
+ this .observation .error (ex );
414
+ }
415
+ }
416
+ finally {
417
+ this .lock .unlock ();
402
418
}
403
419
}
404
420
405
421
private void stop () {
406
- if (this .state .compareAndSet (1 , 2 )) {
407
- this .observation .stop ();
422
+ try {
423
+ this .lock .lock ();
424
+ if (this .state .compareAndSet (1 , 2 )) {
425
+ this .observation .stop ();
426
+ }
427
+ }
428
+ finally {
429
+ this .lock .unlock ();
408
430
}
409
431
}
410
432
0 commit comments