1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import java .util .ArrayList ;
20
20
import java .util .List ;
21
21
import java .util .ListIterator ;
22
- import java .util .concurrent .atomic .AtomicInteger ;
23
- import java .util .concurrent .atomic .AtomicReference ;
24
- import java .util .concurrent .locks .Lock ;
25
- import java .util .concurrent .locks .ReentrantLock ;
26
22
27
23
import io .micrometer .common .KeyValue ;
28
24
import io .micrometer .common .KeyValues ;
@@ -258,12 +254,13 @@ default Observation after() {
258
254
259
255
class SimpleAroundWebFilterObservation implements AroundWebFilterObservation {
260
256
257
+ private final Object lock = new Object ();
258
+
261
259
private final PhasedObservation before ;
262
260
263
261
private final PhasedObservation after ;
264
262
265
- private final AtomicReference <PhasedObservation > currentObservation = new AtomicReference <>(
266
- PhasedObservation .NOOP );
263
+ private volatile PhasedObservation currentObservation = PhasedObservation .NOOP ;
267
264
268
265
SimpleAroundWebFilterObservation (Observation before , Observation after ) {
269
266
this .before = new PhasedObservation (before );
@@ -272,22 +269,26 @@ class SimpleAroundWebFilterObservation implements AroundWebFilterObservation {
272
269
273
270
@ Override
274
271
public Observation start () {
275
- if (this .currentObservation .compareAndSet (PhasedObservation .NOOP , this .before )) {
276
- this .before .start ();
277
- return this .before .observation ;
278
- }
279
- if (this .currentObservation .compareAndSet (this .before , this .after )) {
280
- this .before .stop ();
281
- this .after .start ();
282
- return this .after .observation ;
272
+ synchronized (this .lock ) {
273
+ if (this .currentObservation == PhasedObservation .NOOP ) {
274
+ this .before .start ();
275
+ this .currentObservation = this .before ;
276
+ return this .currentObservation ;
277
+ }
278
+ if (this .currentObservation == this .before ) {
279
+ this .before .stop ();
280
+ this .after .start ();
281
+ this .currentObservation = this .after ;
282
+ return this .currentObservation ;
283
+ }
283
284
}
284
285
return Observation .NOOP ;
285
286
}
286
287
287
288
@ Override
288
289
public Observation error (Throwable ex ) {
289
- this .currentObservation .get (). error (ex );
290
- return this .currentObservation .get (). observation ;
290
+ this .currentObservation .error (ex );
291
+ return this .currentObservation .observation ;
291
292
}
292
293
293
294
@ Override
@@ -303,42 +304,42 @@ private void close() {
303
304
304
305
@ Override
305
306
public Observation contextualName (String contextualName ) {
306
- return this .currentObservation .get (). observation .contextualName (contextualName );
307
+ return this .currentObservation .observation .contextualName (contextualName );
307
308
}
308
309
309
310
@ Override
310
311
public Observation parentObservation (Observation parentObservation ) {
311
- return this .currentObservation .get (). observation .parentObservation (parentObservation );
312
+ return this .currentObservation .observation .parentObservation (parentObservation );
312
313
}
313
314
314
315
@ Override
315
316
public Observation lowCardinalityKeyValue (KeyValue keyValue ) {
316
- return this .currentObservation .get (). observation .lowCardinalityKeyValue (keyValue );
317
+ return this .currentObservation .observation .lowCardinalityKeyValue (keyValue );
317
318
}
318
319
319
320
@ Override
320
321
public Observation highCardinalityKeyValue (KeyValue keyValue ) {
321
- return this .currentObservation .get (). observation .highCardinalityKeyValue (keyValue );
322
+ return this .currentObservation .observation .highCardinalityKeyValue (keyValue );
322
323
}
323
324
324
325
@ Override
325
326
public Observation observationConvention (ObservationConvention <?> observationConvention ) {
326
- return this .currentObservation .get (). observation .observationConvention (observationConvention );
327
+ return this .currentObservation .observation .observationConvention (observationConvention );
327
328
}
328
329
329
330
@ Override
330
331
public Observation event (Event event ) {
331
- return this .currentObservation .get (). observation .event (event );
332
+ return this .currentObservation .observation .event (event );
332
333
}
333
334
334
335
@ Override
335
336
public Context getContext () {
336
- return this .currentObservation .get (). observation .getContext ();
337
+ return this .currentObservation .observation .getContext ();
337
338
}
338
339
339
340
@ Override
340
341
public Scope openScope () {
341
- return this .currentObservation .get (). observation .openScope ();
342
+ return this .currentObservation .observation .openScope ();
342
343
}
343
344
344
345
@ Override
@@ -386,7 +387,7 @@ public Observation after() {
386
387
387
388
@ Override
388
389
public String toString () {
389
- return this .currentObservation .get (). observation .toString ();
390
+ return this .currentObservation .observation .toString ();
390
391
}
391
392
392
393
}
@@ -665,9 +666,9 @@ private static final class PhasedObservation implements Observation {
665
666
666
667
private static final PhasedObservation NOOP = new PhasedObservation (Observation .NOOP );
667
668
668
- private final Lock lock = new ReentrantLock ();
669
+ private final Object lock = new Object ();
669
670
670
- private final AtomicInteger phase = new AtomicInteger ( 0 ) ;
671
+ private volatile int phase = 0 ;
671
672
672
673
private final Observation observation ;
673
674
@@ -717,57 +718,41 @@ public Scope openScope() {
717
718
718
719
@ Override
719
720
public PhasedObservation start () {
720
- try {
721
- this .lock .lock ();
722
- if (this .phase .compareAndSet (0 , 1 )) {
721
+ synchronized (this .lock ) {
722
+ if (this .phase == 0 ) {
723
723
this .observation .start ();
724
+ this .phase = 1 ;
724
725
}
725
726
}
726
- finally {
727
- this .lock .unlock ();
728
- }
729
727
return this ;
730
728
}
731
729
732
730
@ Override
733
731
public PhasedObservation error (Throwable ex ) {
734
- try {
735
- this .lock .lock ();
736
- if (this .phase .get () == 1 ) {
732
+ synchronized (this .lock ) {
733
+ if (this .phase == 1 ) {
737
734
this .observation .error (ex );
738
735
}
739
736
}
740
- finally {
741
- this .lock .unlock ();
742
- }
743
737
return this ;
744
738
}
745
739
746
740
@ Override
747
741
public void stop () {
748
- try {
749
- this .lock .lock ();
750
- if (this .phase .compareAndSet (1 , 2 )) {
742
+ synchronized (this .lock ) {
743
+ if (this .phase == 1 ) {
751
744
this .observation .stop ();
745
+ this .phase = 2 ;
752
746
}
753
747
}
754
- finally {
755
- this .lock .unlock ();
756
- }
757
748
}
758
749
759
750
void close () {
760
- try {
761
- this .lock .lock ();
762
- if (this .phase .compareAndSet (1 , 3 )) {
751
+ synchronized (this .lock ) {
752
+ if (this .phase == 1 ) {
763
753
this .observation .stop ();
764
754
}
765
- else {
766
- this .phase .set (3 );
767
- }
768
- }
769
- finally {
770
- this .lock .unlock ();
755
+ this .phase = 3 ;
771
756
}
772
757
}
773
758
0 commit comments