Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ static ObservationRegistry create() {
*/
void setCurrentObservationScope(Observation.@Nullable Scope current);

/**
* Sets the observation scope as current only if the candidate is present in the
* registry.
* @param candidate observation scope
*/
default void setCurrentObservationScopeIfExists(Observation.@Nullable Scope candidate) {
setCurrentObservationScope(candidate);
}

/**
* Configuration options for this registry.
* @return observation configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,10 @@ else if (currentObservation != null && !currentObservation.isNoop()) {
else {
log.trace("NoOp observation used with SimpleScope");
}
this.registry.setCurrentObservationScope(previousObservationScope);
// DB connections might be closed out of order, so only set the previous scope
// as current if it has not been already closed (by checking if it still
// exists in the registry).
this.registry.setCurrentObservationScopeIfExists(previousObservationScope);
}

private @Nullable SimpleScope getLastScope(SimpleScope simpleScope) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ public void setCurrentObservationScope(Observation.@Nullable Scope current) {
localObservationScope.set(current);
}

@Override
public void setCurrentObservationScopeIfExists(Observation.@Nullable Scope candidate) {
if (candidate == null) {
setCurrentObservationScope(null);
}
else {
Observation.Scope scope = localObservationScope.get();
while (scope != null) {
if (scope.equals(candidate)) {
setCurrentObservationScope(candidate);
break;
}
scope = scope.getPreviousObservationScope();
}
}
}

@Override
public ObservationConfig observationConfig() {
return this.observationConfig;
Expand Down