Skip to content

Commit

Permalink
Added trace logging for OTLA
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed Sep 26, 2023
1 parent e50fe21 commit e96dd42
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.micrometer.context.ThreadLocalAccessor;
import io.micrometer.observation.NullObservation;
import io.micrometer.observation.Observation;
import io.micrometer.observation.Observation.Scope;
import io.micrometer.observation.ObservationRegistry;

/**
Expand Down Expand Up @@ -113,7 +114,10 @@ public Observation getValue() {
public void setValue(Observation value) {
// Iterate over all handlers and open a new scope. The created scope will put
// itself to TL.
value.openScope();
Scope scope = value.openScope();
if (log.isTraceEnabled()) {
log.trace("Called setValue(...) for Observation <{}> and opened scope <{}>", value, scope);
}
}

@Override
Expand All @@ -122,6 +126,10 @@ public void setValue() {
if (currentObservation == null) {
return;
}
if (log.isTraceEnabled()) {
log.trace("Calling setValue(), currentObservation <{}> but we will open a NullObservation",
currentObservation);
}
// Since we can't fully rely on using the observation registry static instance
// because you can have multiple ones being created in your code (e.g. tests,
// production code, multiple contexts etc.) we need a way to use an existing,
Expand All @@ -133,24 +141,39 @@ public void setValue() {
// Creating a new one with empty context and opens a new scope
// This scope will remember the previously created one to
// which we will revert once "null scope" is closed
new NullObservation(registryAttachedToCurrentObservation).start().openScope();
Scope scope = new NullObservation(registryAttachedToCurrentObservation).start().openScope();
if (log.isTraceEnabled()) {
log.trace("Created the NullObservation scope <{}>", scope);
}
}

private void closeCurrentScope() {
Observation.Scope scope = observationRegistry.getCurrentObservationScope();
if (log.isTraceEnabled()) {
log.trace("Closing current scope <{}>", scope);
}
if (scope != null) {
scope.close();
}
if (log.isTraceEnabled()) {
log.trace("After closing scope, current one is <{}>", observationRegistry.getCurrentObservationScope());
}
}

@Override
public void restore() {
if (log.isTraceEnabled()) {
log.trace("Calling restore()");
}
closeCurrentScope();
}

@Override
public void restore(Observation value) {
Observation.Scope scope = observationRegistry.getCurrentObservationScope();
if (log.isTraceEnabled()) {
log.trace("Calling restore(...) with Observation <{}> and scope <{}>", value, scope);
}
if (scope == null) {
String msg = "There is no current scope in thread local. This situation should not happen";
log.warn(msg);
Expand All @@ -177,6 +200,9 @@ void assertFalse(String msg) {
@Override
@Deprecated
public void reset() {
if (log.isTraceEnabled()) {
log.trace("Calling reset()");
}
ThreadLocalAccessor.super.reset();
}

Expand Down

0 comments on commit e96dd42

Please sign in to comment.