Address Observability Thread Safety

Closes gh-12829
This commit is contained in:
Josh Cummings 2023-03-06 12:46:17 -07:00
parent acf48721cd
commit c06e604278
No known key found for this signature in database
GPG Key ID: A306A51F43B8E5A5
2 changed files with 107 additions and 58 deletions

View File

@ -18,9 +18,8 @@ package org.springframework.security.web;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import io.micrometer.common.KeyValues; import io.micrometer.common.KeyValues;
@ -227,49 +226,38 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F
class SimpleAroundFilterObservation implements AroundFilterObservation { class SimpleAroundFilterObservation implements AroundFilterObservation {
private final Iterator<Observation> observations; private final ObservationReference before;
private final Observation before; private final ObservationReference after;
private final Observation after; private final AtomicReference<ObservationReference> reference = new AtomicReference<>(
ObservationReference.NOOP);
private final AtomicReference<Observation.Scope> currentScope = new AtomicReference<>(null);
SimpleAroundFilterObservation(Observation before, Observation after) { SimpleAroundFilterObservation(Observation before, Observation after) {
this.before = before; this.before = new ObservationReference(before);
this.after = after; this.after = new ObservationReference(after);
this.observations = Arrays.asList(before, after).iterator();
} }
@Override @Override
public void start() { public void start() {
if (this.observations.hasNext()) { if (this.reference.compareAndSet(ObservationReference.NOOP, this.before)) {
stop(); this.before.start();
Observation observation = this.observations.next(); return;
observation.start(); }
Observation.Scope scope = observation.openScope(); if (this.reference.compareAndSet(this.before, this.after)) {
this.currentScope.set(scope); this.before.stop();
this.after.start();
} }
} }
@Override @Override
public void error(Throwable ex) { public void error(Throwable ex) {
Observation.Scope scope = this.currentScope.get(); this.reference.get().error(ex);
if (scope == null) {
return;
}
scope.close();
scope.getCurrentObservation().error(ex);
} }
@Override @Override
public void stop() { public void stop() {
Observation.Scope scope = this.currentScope.getAndSet(null); this.reference.get().stop();
if (scope == null) {
return;
}
scope.close();
scope.getCurrentObservation().stop();
} }
@Override @Override
@ -304,12 +292,50 @@ public final class ObservationFilterChainDecorator implements FilterChainProxy.F
@Override @Override
public Observation before() { public Observation before() {
return this.before; return this.before.observation;
} }
@Override @Override
public Observation after() { public Observation after() {
return this.after; return this.after.observation;
}
private static final class ObservationReference {
private static final ObservationReference NOOP = new ObservationReference(Observation.NOOP);
private final AtomicInteger state = new AtomicInteger(0);
private final Observation observation;
private volatile Observation.Scope scope;
private ObservationReference(Observation observation) {
this.observation = observation;
this.scope = Observation.Scope.NOOP;
}
private void start() {
if (this.state.compareAndSet(0, 1)) {
this.observation.start();
this.scope = this.observation.openScope();
}
}
private void error(Throwable error) {
if (this.state.get() == 1) {
this.scope.close();
this.scope.getCurrentObservation().error(error);
}
}
private void stop() {
if (this.state.compareAndSet(1, 2)) {
this.scope.close();
this.scope.getCurrentObservation().stop();
}
}
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,10 +17,9 @@
package org.springframework.security.web.server; package org.springframework.security.web.server;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import io.micrometer.common.KeyValues; import io.micrometer.common.KeyValues;
@ -253,46 +252,38 @@ public final class ObservationWebFilterChainDecorator implements WebFilterChainP
class SimpleAroundWebFilterObservation implements AroundWebFilterObservation { class SimpleAroundWebFilterObservation implements AroundWebFilterObservation {
private final Iterator<Observation> observations; private final ObservationReference before;
private final Observation before; private final ObservationReference after;
private final Observation after; private final AtomicReference<ObservationReference> currentObservation = new AtomicReference<>(
ObservationReference.NOOP);
private final AtomicReference<Observation> currentObservation = new AtomicReference<>(null);
SimpleAroundWebFilterObservation(Observation before, Observation after) { SimpleAroundWebFilterObservation(Observation before, Observation after) {
this.before = before; this.before = new ObservationReference(before);
this.after = after; this.after = new ObservationReference(after);
this.observations = Arrays.asList(before, after).iterator();
} }
@Override @Override
public void start() { public void start() {
if (this.observations.hasNext()) { if (this.currentObservation.compareAndSet(ObservationReference.NOOP, this.before)) {
stop(); this.before.start();
Observation observation = this.observations.next(); return;
observation.start(); }
this.currentObservation.set(observation); if (this.currentObservation.compareAndSet(this.before, this.after)) {
this.before.stop();
this.after.start();
} }
} }
@Override @Override
public void error(Throwable ex) { public void error(Throwable ex) {
Observation observation = this.currentObservation.get(); this.currentObservation.get().error(ex);
if (observation == null) {
return;
}
observation.error(ex);
} }
@Override @Override
public void stop() { public void stop() {
Observation observation = this.currentObservation.getAndSet(null); this.currentObservation.get().stop();
if (observation == null) {
return;
}
observation.stop();
} }
@Override @Override
@ -329,12 +320,44 @@ public final class ObservationWebFilterChainDecorator implements WebFilterChainP
@Override @Override
public Observation before() { public Observation before() {
return this.before; return this.before.observation;
} }
@Override @Override
public Observation after() { public Observation after() {
return this.after; return this.after.observation;
}
private static final class ObservationReference {
private static final ObservationReference NOOP = new ObservationReference(Observation.NOOP);
private final AtomicInteger state = new AtomicInteger(0);
private final Observation observation;
private ObservationReference(Observation observation) {
this.observation = observation;
}
private void start() {
if (this.state.compareAndSet(0, 1)) {
this.observation.start();
}
}
private void error(Throwable ex) {
if (this.state.get() == 1) {
this.observation.error(ex);
}
}
private void stop() {
if (this.state.compareAndSet(1, 2)) {
this.observation.stop();
}
}
} }
} }