forked from boskworks/bosk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POC OTel context propagation working
- Loading branch information
Showing
4 changed files
with
149 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
bosk-core/src/main/java/io/vena/bosk/drivers/OtelSpanContextDriver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.vena.bosk.drivers; | ||
|
||
import io.opentelemetry.api.trace.SpanContext; | ||
import io.vena.bosk.BoskDiagnosticContext; | ||
import io.vena.bosk.BoskDriver; | ||
import io.vena.bosk.DriverFactory; | ||
import io.vena.bosk.Identifier; | ||
import io.vena.bosk.Reference; | ||
import io.vena.bosk.StateTreeNode; | ||
import io.vena.bosk.exceptions.InvalidTypeException; | ||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Adds OpenTelemetry {@link SpanContext} info to the {@link BoskDiagnosticContext} | ||
* automatically on each method call. | ||
*/ | ||
public class OtelSpanContextDriver<R extends StateTreeNode> implements BoskDriver<R> { | ||
private final BoskDiagnosticContext context; | ||
private final BoskDriver<R> downstream; | ||
|
||
OtelSpanContextDriver(BoskDiagnosticContext context, BoskDriver<R> downstream) { | ||
this.context = context; | ||
this.downstream = downstream; | ||
} | ||
|
||
public static <RR extends StateTreeNode> DriverFactory<RR> factory() { | ||
return (b,d) -> new OtelSpanContextDriver<>(b.rootReference().diagnosticContext(), d); | ||
} | ||
|
||
@Override | ||
public R initialRoot(Type rootType) throws InvalidTypeException, IOException, InterruptedException { | ||
try (var __ = context.withCurrentOtelContext()) { | ||
return downstream.initialRoot(rootType); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> void submitReplacement(Reference<T> target, T newValue) { | ||
try (var __ = context.withCurrentOtelContext()) { | ||
downstream.submitReplacement(target, newValue); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> void submitConditionalReplacement(Reference<T> target, T newValue, Reference<Identifier> precondition, Identifier requiredValue) { | ||
try (var __ = context.withCurrentOtelContext()) { | ||
downstream.submitConditionalReplacement(target, newValue, precondition, requiredValue); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> void submitInitialization(Reference<T> target, T newValue) { | ||
try (var __ = context.withCurrentOtelContext()) { | ||
downstream.submitInitialization(target, newValue); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> void submitDeletion(Reference<T> target) { | ||
try (var __ = context.withCurrentOtelContext()) { | ||
downstream.submitDeletion(target); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> void submitConditionalDeletion(Reference<T> target, Reference<Identifier> precondition, Identifier requiredValue) { | ||
try (var __ = context.withCurrentOtelContext()) { | ||
downstream.submitConditionalDeletion(target, precondition, requiredValue); | ||
} | ||
} | ||
|
||
@Override | ||
public void flush() throws IOException, InterruptedException { | ||
try (var __ = context.withCurrentOtelContext()) { | ||
downstream.flush(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters