22
22
import java .util .concurrent .atomic .AtomicInteger ;
23
23
import java .util .concurrent .atomic .AtomicReference ;
24
24
25
+ import io .micrometer .common .KeyValue ;
25
26
import io .micrometer .common .KeyValues ;
26
27
import io .micrometer .observation .Observation ;
27
28
import io .micrometer .observation .ObservationConvention ;
@@ -265,27 +266,70 @@ class SimpleAroundWebFilterObservation implements AroundWebFilterObservation {
265
266
}
266
267
267
268
@ Override
268
- public void start () {
269
+ public Observation start () {
269
270
if (this .currentObservation .compareAndSet (ObservationReference .NOOP , this .before )) {
270
271
this .before .start ();
271
- return ;
272
+ return this . before . observation ;
272
273
}
273
274
if (this .currentObservation .compareAndSet (this .before , this .after )) {
274
275
this .before .stop ();
275
276
this .after .start ();
277
+ return this .after .observation ;
276
278
}
279
+ return Observation .NOOP ;
277
280
}
278
281
279
282
@ Override
280
- public void error (Throwable ex ) {
283
+ public Observation error (Throwable ex ) {
281
284
this .currentObservation .get ().error (ex );
285
+ return this .currentObservation .get ().observation ;
282
286
}
283
287
284
288
@ Override
285
289
public void stop () {
286
290
this .currentObservation .get ().stop ();
287
291
}
288
292
293
+ @ Override
294
+ public Observation contextualName (String contextualName ) {
295
+ return this .currentObservation .get ().observation .contextualName (contextualName );
296
+ }
297
+
298
+ @ Override
299
+ public Observation parentObservation (Observation parentObservation ) {
300
+ return this .currentObservation .get ().observation .parentObservation (parentObservation );
301
+ }
302
+
303
+ @ Override
304
+ public Observation lowCardinalityKeyValue (KeyValue keyValue ) {
305
+ return this .currentObservation .get ().observation .lowCardinalityKeyValue (keyValue );
306
+ }
307
+
308
+ @ Override
309
+ public Observation highCardinalityKeyValue (KeyValue keyValue ) {
310
+ return this .currentObservation .get ().observation .highCardinalityKeyValue (keyValue );
311
+ }
312
+
313
+ @ Override
314
+ public Observation observationConvention (ObservationConvention <?> observationConvention ) {
315
+ return this .currentObservation .get ().observation .observationConvention (observationConvention );
316
+ }
317
+
318
+ @ Override
319
+ public Observation event (Event event ) {
320
+ return this .currentObservation .get ().observation .event (event );
321
+ }
322
+
323
+ @ Override
324
+ public Context getContext () {
325
+ return this .currentObservation .get ().observation .getContext ();
326
+ }
327
+
328
+ @ Override
329
+ public Scope openScope () {
330
+ return this .currentObservation .get ().observation .openScope ();
331
+ }
332
+
289
333
@ Override
290
334
public WebFilterChain wrap (WebFilterChain chain ) {
291
335
return (exchange ) -> {
@@ -313,7 +357,8 @@ public WebFilter wrap(WebFilter filter) {
313
357
.doOnError ((t ) -> {
314
358
error (t );
315
359
stop ();
316
- });
360
+ })
361
+ .contextWrite ((context ) -> context .put (ObservationThreadLocalAccessor .KEY , this ));
317
362
// @formatter:on
318
363
};
319
364
}
@@ -328,6 +373,11 @@ public Observation after() {
328
373
return this .after .observation ;
329
374
}
330
375
376
+ @ Override
377
+ public String toString () {
378
+ return this .currentObservation .get ().observation .toString ();
379
+ }
380
+
331
381
private static final class ObservationReference {
332
382
333
383
private static final ObservationReference NOOP = new ObservationReference (Observation .NOOP );
@@ -364,7 +414,7 @@ private void stop() {
364
414
365
415
}
366
416
367
- interface WebFilterObservation {
417
+ interface WebFilterObservation extends Observation {
368
418
369
419
WebFilterObservation NOOP = new WebFilterObservation () {
370
420
};
@@ -376,13 +426,59 @@ static WebFilterObservation create(Observation observation) {
376
426
return new SimpleWebFilterObservation (observation );
377
427
}
378
428
379
- default void start () {
429
+ @ Override
430
+ default Observation contextualName (String contextualName ) {
431
+ return Observation .NOOP ;
432
+ }
433
+
434
+ @ Override
435
+ default Observation parentObservation (Observation parentObservation ) {
436
+ return Observation .NOOP ;
437
+ }
438
+
439
+ @ Override
440
+ default Observation lowCardinalityKeyValue (KeyValue keyValue ) {
441
+ return Observation .NOOP ;
380
442
}
381
443
382
- default void error (Throwable ex ) {
444
+ @ Override
445
+ default Observation highCardinalityKeyValue (KeyValue keyValue ) {
446
+ return Observation .NOOP ;
383
447
}
384
448
449
+ @ Override
450
+ default Observation observationConvention (ObservationConvention <?> observationConvention ) {
451
+ return Observation .NOOP ;
452
+ }
453
+
454
+ @ Override
455
+ default Observation error (Throwable error ) {
456
+ return Observation .NOOP ;
457
+ }
458
+
459
+ @ Override
460
+ default Observation event (Event event ) {
461
+ return Observation .NOOP ;
462
+ }
463
+
464
+ @ Override
465
+ default Observation start () {
466
+ return Observation .NOOP ;
467
+ }
468
+
469
+ @ Override
470
+ default Context getContext () {
471
+ return new Observation .Context ();
472
+ }
473
+
474
+ @ Override
385
475
default void stop () {
476
+
477
+ }
478
+
479
+ @ Override
480
+ default Scope openScope () {
481
+ return Scope .NOOP ;
386
482
}
387
483
388
484
default WebFilter wrap (WebFilter filter ) {
@@ -402,20 +498,60 @@ class SimpleWebFilterObservation implements WebFilterObservation {
402
498
}
403
499
404
500
@ Override
405
- public void start () {
406
- this .observation .start ();
501
+ public Observation start () {
502
+ return this .observation .start ();
407
503
}
408
504
409
505
@ Override
410
- public void error (Throwable ex ) {
411
- this .observation .error (ex );
506
+ public Observation error (Throwable ex ) {
507
+ return this .observation .error (ex );
412
508
}
413
509
414
510
@ Override
415
511
public void stop () {
416
512
this .observation .stop ();
417
513
}
418
514
515
+ @ Override
516
+ public Observation contextualName (String contextualName ) {
517
+ return this .observation .contextualName (contextualName );
518
+ }
519
+
520
+ @ Override
521
+ public Observation parentObservation (Observation parentObservation ) {
522
+ return this .observation .parentObservation (parentObservation );
523
+ }
524
+
525
+ @ Override
526
+ public Observation lowCardinalityKeyValue (KeyValue keyValue ) {
527
+ return this .observation .lowCardinalityKeyValue (keyValue );
528
+ }
529
+
530
+ @ Override
531
+ public Observation highCardinalityKeyValue (KeyValue keyValue ) {
532
+ return this .observation .highCardinalityKeyValue (keyValue );
533
+ }
534
+
535
+ @ Override
536
+ public Observation observationConvention (ObservationConvention <?> observationConvention ) {
537
+ return this .observation .observationConvention (observationConvention );
538
+ }
539
+
540
+ @ Override
541
+ public Observation event (Event event ) {
542
+ return this .observation .event (event );
543
+ }
544
+
545
+ @ Override
546
+ public Context getContext () {
547
+ return this .observation .getContext ();
548
+ }
549
+
550
+ @ Override
551
+ public Scope openScope () {
552
+ return this .observation .openScope ();
553
+ }
554
+
419
555
@ Override
420
556
public WebFilter wrap (WebFilter filter ) {
421
557
if (this .observation .isNoop ()) {
@@ -442,7 +578,8 @@ public WebFilterChain wrap(WebFilterChain chain) {
442
578
.doOnCancel (this .observation ::stop ).doOnError ((t ) -> {
443
579
this .observation .error (t );
444
580
this .observation .stop ();
445
- });
581
+ }).contextWrite (
582
+ (context ) -> context .put (ObservationThreadLocalAccessor .KEY , this .observation ));
446
583
};
447
584
}
448
585
0 commit comments