Skip to content

Commit

Permalink
More fiddling with OTel
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Jun 23, 2024
1 parent bbdff21 commit cee3caf
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 13 deletions.
9 changes: 8 additions & 1 deletion bosk-core/src/main/java/io/vena/bosk/Bosk.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.vena.bosk;

import io.opentelemetry.instrumentation.annotations.SpanAttribute;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.vena.bosk.BoskDiagnosticContext.DiagnosticScope;
import io.vena.bosk.ReferenceUtils.CatalogRef;
import io.vena.bosk.ReferenceUtils.ListingRef;
Expand Down Expand Up @@ -399,7 +401,7 @@ private <T,S> void triggerQueueingOfHooks(Reference<T> target, @Nullable R prior
) {
try (@SuppressWarnings("unused") ReadContext executionContext = new ReadContext(rootForHook)) {
LOGGER.debug("Hook: RUN {}({})", reg.name, changedRef);
reg.hook.onChanged(changedRef);
callHook(reg, changedRef, reg.name());
} catch (Exception e) {
LOGGER.error("Bosk hook \"" + reg.name() + "\" terminated with an exception, which usually indicates a bug. State updates may have been lost", e);

Expand All @@ -415,6 +417,11 @@ private <T,S> void triggerQueueingOfHooks(Reference<T> target, @Nullable R prior
});
}

@WithSpan
private static <R extends StateTreeNode, S> void callHook(Bosk<R>.HookRegistration<S> reg, @SpanAttribute("hook.reference") Reference<S> changedRef, @SpanAttribute("hook.name") String hookName) {
reg.hook.onChanged(changedRef);
}

/**
* Runs queued hooks in a "breadth-first" fashion: all hooks "H" triggered by
* any single hook "G" will run before any consequent hooks triggered by "H".
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.vena.bosk.drivers;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.instrumentation.annotations.SpanAttribute;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.vena.bosk.BoskDiagnosticContext;
import io.vena.bosk.BoskDriver;
import io.vena.bosk.DriverFactory;
Expand All @@ -18,60 +21,100 @@
public class OtelSpanContextDriver<R extends StateTreeNode> implements BoskDriver<R> {
private final BoskDiagnosticContext context;
private final BoskDriver<R> downstream;
private final String caller;

OtelSpanContextDriver(BoskDiagnosticContext context, BoskDriver<R> downstream) {
OtelSpanContextDriver(BoskDiagnosticContext context, BoskDriver<R> downstream, String caller) {
this.context = context;
this.downstream = downstream;
this.caller = caller;
}

public static <RR extends StateTreeNode> DriverFactory<RR> factory() {
return (b,d) -> new OtelSpanContextDriver<>(b.rootReference().diagnosticContext(), d);
StackTraceElement caller = new RuntimeException().getStackTrace()[1];
return (b,d) -> new OtelSpanContextDriver<>(
b.rootReference().diagnosticContext(),
d,
caller.toString()
);
}

@Override
public R initialRoot(Type rootType) throws InvalidTypeException, IOException, InterruptedException {
@WithSpan("initialRoot")
public R initialRoot(
@SpanAttribute("bosk.rootType") Type rootType
) throws InvalidTypeException, IOException, InterruptedException {
Span.current().setAttribute("bosk.createdAt", caller);
try (var __ = context.withCurrentOtelContext()) {
return downstream.initialRoot(rootType);
}
}

@Override
public <T> void submitReplacement(Reference<T> target, T newValue) {
@WithSpan("submitReplacement")
public <T> void submitReplacement(
@SpanAttribute("bosk.target") Reference<T> target,
T newValue
) {
Span.current().setAttribute("bosk.createdAt", caller);
try (var __ = context.withCurrentOtelContext()) {
downstream.submitReplacement(target, newValue);
}
}

@Override
public <T> void submitConditionalReplacement(Reference<T> target, T newValue, Reference<Identifier> precondition, Identifier requiredValue) {
@WithSpan("submitConditionalReplacement")
public <T> void submitConditionalReplacement(
@SpanAttribute("bosk.target") Reference<T> target,
T newValue,
@SpanAttribute("bosk.precondition") Reference<Identifier> precondition,
@SpanAttribute("bosk.requiredValue") Identifier requiredValue
) {
Span.current().setAttribute("bosk.createdAt", caller);
try (var __ = context.withCurrentOtelContext()) {
downstream.submitConditionalReplacement(target, newValue, precondition, requiredValue);
}
}

@Override
public <T> void submitInitialization(Reference<T> target, T newValue) {
@WithSpan("submitInitialization")
public <T> void submitInitialization(
@SpanAttribute("bosk.target") Reference<T> target,
T newValue
) {
Span.current().setAttribute("bosk.createdAt", caller);
try (var __ = context.withCurrentOtelContext()) {
downstream.submitInitialization(target, newValue);
}
}

@Override
public <T> void submitDeletion(Reference<T> target) {
@WithSpan("submitDeletion")
public <T> void submitDeletion(
@SpanAttribute("bosk.target") Reference<T> target
) {
Span.current().setAttribute("bosk.createdAt", caller);
try (var __ = context.withCurrentOtelContext()) {
downstream.submitDeletion(target);
}
}

@Override
public <T> void submitConditionalDeletion(Reference<T> target, Reference<Identifier> precondition, Identifier requiredValue) {
@WithSpan("submitConditionalDeletion")
public <T> void submitConditionalDeletion(
@SpanAttribute("bosk.target") Reference<T> target,
@SpanAttribute("bosk.precondition") Reference<Identifier> precondition,
@SpanAttribute("bosk.requiredValue") Identifier requiredValue
) {
Span.current().setAttribute("bosk.createdAt", caller);
try (var __ = context.withCurrentOtelContext()) {
downstream.submitConditionalDeletion(target, precondition, requiredValue);
}
}

@Override
@WithSpan("InterruptedException")
public void flush() throws IOException, InterruptedException {
Span.current().setAttribute("bosk.createdAt", caller);
try (var __ = context.withCurrentOtelContext()) {
downstream.flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ private Listener(FutureTask<R> initialRootAction) {
}

@Override
@WithSpan("onConnectionSucceeded")
public void onConnectionSucceeded() throws
UnrecognizedFormatException,
UninitializedCollectionException,
Expand Down Expand Up @@ -582,7 +583,6 @@ private MDCScope beginDriverOperation(String description, Object... args) {
return ex;
}

@WithSpan
private <X extends Exception, Y extends Exception> void doRetryableDriverOperation(RetryableOperation<X,Y> operation, String description, Object... args) throws X,Y {
RetryableOperation<X,Y> operationInSession = () -> {
int immediateRetriesLeft = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public MongoDriverHanoiTest(ParameterSet parameters) {
mongoService.clientSettings(),
settings,
new BsonPlugin()
)
),
OtelSpanContextDriver.factory()
);
mongoService.client()
.getDatabase(settings.database())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected void setupBosksAndReferences(DriverFactory<TestEntity> driverFactory)
// This is the bosk we're testing
bosk = new Bosk<TestEntity>("Test bosk", TestEntity.class, AbstractDriverTest::initialRoot, DriverStack.of(
MirroringDriver.targeting(canonicalBosk),
OtelSpanContextDriver.factory(),
DriverStateVerifier.wrap(driverFactory, TestEntity.class, AbstractDriverTest::initialRoot)
));
driver = bosk.driver();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.vena.bosk.drivers;

import io.opentelemetry.instrumentation.annotations.SpanAttribute;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.vena.bosk.Bosk;
import io.vena.bosk.Catalog;
import io.vena.bosk.CatalogReference;
Expand Down Expand Up @@ -62,7 +64,8 @@ void initialState(Path enclosingCatalogPath) {
}

@ParametersByName
void replaceIdentical(Path enclosingCatalogPath, Identifier childID) throws InvalidTypeException {
@WithSpan("replaceIdentical")
void replaceIdentical(@SpanAttribute("enclosingPath") Path enclosingCatalogPath, @SpanAttribute("childID") Identifier childID) throws InvalidTypeException {
CatalogReference<TestEntity> ref = initializeBoskWithCatalog(enclosingCatalogPath);
driver.submitReplacement(ref.then(childID), newEntity(childID, ref));
assertCorrectBoskContents();
Expand Down Expand Up @@ -455,7 +458,8 @@ private Reference<TestValues> initializeBoskWithBlankValues(Path enclosingCatalo
return ref;
}

private CatalogReference<TestEntity> initializeBoskWithCatalog(Path enclosingCatalogPath) {
@WithSpan
private CatalogReference<TestEntity> initializeBoskWithCatalog(@SpanAttribute("enclosingCatalogPath") Path enclosingCatalogPath) {
LOGGER.debug("initializeBoskWithCatalog({})", enclosingCatalogPath);
setupBosksAndReferences(driverFactory);
try {
Expand Down

0 comments on commit cee3caf

Please sign in to comment.