Skip to content

Commit 6db2b0d

Browse files
committed
Align Filter Chain Observability Lineage
Closes gh-12849
1 parent 834e361 commit 6db2b0d

File tree

2 files changed

+299
-12
lines changed

2 files changed

+299
-12
lines changed

web/src/main/java/org/springframework/security/web/server/ObservationWebFilterChainDecorator.java

Lines changed: 149 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.concurrent.atomic.AtomicInteger;
2323
import java.util.concurrent.atomic.AtomicReference;
2424

25+
import io.micrometer.common.KeyValue;
2526
import io.micrometer.common.KeyValues;
2627
import io.micrometer.observation.Observation;
2728
import io.micrometer.observation.ObservationConvention;
@@ -265,27 +266,70 @@ class SimpleAroundWebFilterObservation implements AroundWebFilterObservation {
265266
}
266267

267268
@Override
268-
public void start() {
269+
public Observation start() {
269270
if (this.currentObservation.compareAndSet(ObservationReference.NOOP, this.before)) {
270271
this.before.start();
271-
return;
272+
return this.before.observation;
272273
}
273274
if (this.currentObservation.compareAndSet(this.before, this.after)) {
274275
this.before.stop();
275276
this.after.start();
277+
return this.after.observation;
276278
}
279+
return Observation.NOOP;
277280
}
278281

279282
@Override
280-
public void error(Throwable ex) {
283+
public Observation error(Throwable ex) {
281284
this.currentObservation.get().error(ex);
285+
return this.currentObservation.get().observation;
282286
}
283287

284288
@Override
285289
public void stop() {
286290
this.currentObservation.get().stop();
287291
}
288292

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+
289333
@Override
290334
public WebFilterChain wrap(WebFilterChain chain) {
291335
return (exchange) -> {
@@ -313,7 +357,8 @@ public WebFilter wrap(WebFilter filter) {
313357
.doOnError((t) -> {
314358
error(t);
315359
stop();
316-
});
360+
})
361+
.contextWrite((context) -> context.put(ObservationThreadLocalAccessor.KEY, this));
317362
// @formatter:on
318363
};
319364
}
@@ -328,6 +373,11 @@ public Observation after() {
328373
return this.after.observation;
329374
}
330375

376+
@Override
377+
public String toString() {
378+
return this.currentObservation.get().observation.toString();
379+
}
380+
331381
private static final class ObservationReference {
332382

333383
private static final ObservationReference NOOP = new ObservationReference(Observation.NOOP);
@@ -364,7 +414,7 @@ private void stop() {
364414

365415
}
366416

367-
interface WebFilterObservation {
417+
interface WebFilterObservation extends Observation {
368418

369419
WebFilterObservation NOOP = new WebFilterObservation() {
370420
};
@@ -376,13 +426,59 @@ static WebFilterObservation create(Observation observation) {
376426
return new SimpleWebFilterObservation(observation);
377427
}
378428

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;
380442
}
381443

382-
default void error(Throwable ex) {
444+
@Override
445+
default Observation highCardinalityKeyValue(KeyValue keyValue) {
446+
return Observation.NOOP;
383447
}
384448

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
385475
default void stop() {
476+
477+
}
478+
479+
@Override
480+
default Scope openScope() {
481+
return Scope.NOOP;
386482
}
387483

388484
default WebFilter wrap(WebFilter filter) {
@@ -402,20 +498,60 @@ class SimpleWebFilterObservation implements WebFilterObservation {
402498
}
403499

404500
@Override
405-
public void start() {
406-
this.observation.start();
501+
public Observation start() {
502+
return this.observation.start();
407503
}
408504

409505
@Override
410-
public void error(Throwable ex) {
411-
this.observation.error(ex);
506+
public Observation error(Throwable ex) {
507+
return this.observation.error(ex);
412508
}
413509

414510
@Override
415511
public void stop() {
416512
this.observation.stop();
417513
}
418514

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+
419555
@Override
420556
public WebFilter wrap(WebFilter filter) {
421557
if (this.observation.isNoop()) {
@@ -442,7 +578,8 @@ public WebFilterChain wrap(WebFilterChain chain) {
442578
.doOnCancel(this.observation::stop).doOnError((t) -> {
443579
this.observation.error(t);
444580
this.observation.stop();
445-
});
581+
}).contextWrite(
582+
(context) -> context.put(ObservationThreadLocalAccessor.KEY, this.observation));
446583
};
447584
}
448585

0 commit comments

Comments
 (0)