Skip to content

Commit

Permalink
Added the getCurrentObservationScope method
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed May 10, 2022
1 parent 304bc11 commit 4640297
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ public Observation getCurrentObservation() {
}

@Override
public void setCurrentObservation(Observation current) {
this.delegate.setCurrentObservation(current);
public Observation.Scope getCurrentObservationScope() {
return this.delegate.getCurrentObservationScope();
}

@Override
public void setCurrentObservationScope(Observation.Scope current) {
this.delegate.setCurrentObservationScope(current);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ public Observation getCurrentObservation() {
}

@Override
public void setCurrentObservation(Observation current) {
public Observation.Scope getCurrentObservationScope() {
return NoopObservation.NoOpScope.INSTANCE;
}

@Override
public void setCurrentObservationScope(Observation.Scope current) {

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,21 @@ static ObservationRegistry create() {
Observation getCurrentObservation();

/**
* Sets the observation as current.
* @param current observation
* When previously set will allow to retrieve the {@link Observation.Scope} at any
* point in time.
*
* Example: if an {@link Observation} was put in {@link Observation.Scope} then this
* method will return the current present {@link Observation.Scope}.
* @return current observation scope or {@code null} if it's not present
*/
@Nullable
Observation.Scope getCurrentObservationScope();

/**
* Sets the observation scope as current.
* @param current observation scope
*/
void setCurrentObservation(@Nullable Observation current);
void setCurrentObservationScope(@Nullable Observation.Scope current);

/**
* Configuration options for this registry.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ static class SimpleScope implements Scope {
private final SimpleObservation currentObservation;

@Nullable
private final Observation previousObservation;
private final Observation.Scope previousObservation;

SimpleScope(ObservationRegistry registry, SimpleObservation current) {
this.registry = registry;
this.currentObservation = current;
this.previousObservation = registry.getCurrentObservation();
this.registry.setCurrentObservation(current);
this.previousObservation = registry.getCurrentObservationScope();
this.registry.setCurrentObservationScope(this);
}

@Override
Expand All @@ -168,7 +168,7 @@ public Observation getCurrentObservation() {

@Override
public void close() {
this.registry.setCurrentObservation(previousObservation);
this.registry.setCurrentObservationScope(previousObservation);
this.currentObservation.notifyOnScopeClosed();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,27 @@
*/
class SimpleObservationRegistry implements ObservationRegistry {

private static final ThreadLocal<Observation> localObservation = new ThreadLocal<>();
private static final ThreadLocal<Observation.Scope> localObservation = new ThreadLocal<>();

private final ObservationConfig observationConfig = new ObservationConfig();

@Nullable
@Override
public Observation getCurrentObservation() {
Observation.Scope scope = localObservation.get();
if (scope != null) {
return scope.getCurrentObservation();
}
return null;
}

@Override
public Observation.Scope getCurrentObservationScope() {
return localObservation.get();
}

@Override
public void setCurrentObservation(@Nullable Observation current) {
public void setCurrentObservationScope(Observation.Scope current) {
localObservation.set(current);
}

Expand Down

0 comments on commit 4640297

Please sign in to comment.