Skip to content

Commit

Permalink
Use OpenTelemetry and Jaeger for troubleshooting
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed May 26, 2023
1 parent df91094 commit 9cc496a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions bosk-mongo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ dependencies {
api 'org.mongodb:mongodb-driver-sync:4.1.2'
testImplementation project(":bosk-testing")
testImplementation project(":lib-testing")
implementation 'io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:1.26.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mongodb.client.MongoChangeStreamCursor;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.changestream.ChangeStreamDocument;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.vena.bosk.exceptions.NotYetImplementedException;
import java.io.Closeable;
import java.util.concurrent.CancellationException;
Expand All @@ -20,6 +21,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
Expand Down Expand Up @@ -136,8 +138,8 @@ private void setupNewState(ChangeEventListener newListener) {
MongoChangeStreamCursor<ChangeStreamDocument<Document>> cursor;
ChangeStreamDocument<Document> initialEvent;
if (lastProcessedResumeToken == null) {
cursor = collection.watch().cursor();
initialEvent = cursor.tryNext();
cursor = collection.watch().maxAwaitTime(0, MILLISECONDS).cursor();
initialEvent = doTryNext(cursor);
if (initialEvent == null) {
// In this case, tryNext() has caused the cursor to point to
// a token in the past, so we can reliably use that.
Expand All @@ -150,6 +152,11 @@ private void setupNewState(ChangeEventListener newListener) {
current = new State(cursor, initialEvent, newListener);
}

@WithSpan
private static ChangeStreamDocument<Document> doTryNext(MongoChangeStreamCursor<ChangeStreamDocument<Document>> cursor) {
return cursor.tryNext();
}

/**
* This method has no uncaught exceptions. They're all reported to {@link ChangeEventListener#onException}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.changestream.ChangeStreamDocument;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.vena.bosk.Bosk;
import io.vena.bosk.BoskDriver;
import io.vena.bosk.Entity;
Expand Down Expand Up @@ -86,6 +87,7 @@ private void validateMongoClientSettings(MongoClientSettings clientSettings) {
}

@Override
@WithSpan
public R initialRoot(Type rootType) throws InvalidTypeException, IOException, InterruptedException {
// TODO: How to initialize the database and collection if they don't exist?
R result;
Expand Down
2 changes: 2 additions & 0 deletions bosk-testing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ dependencies {
implementation 'org.junit.jupiter:junit-jupiter-api:5.9.2'
implementation 'org.junit.jupiter:junit-jupiter-params:5.9.2'
runtimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.2'
api 'io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:1.26.0'

}

// SpotBugs warnings on test code are not useful and often counterproductive
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.vena.bosk.drivers;

import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.vena.bosk.Catalog;
import io.vena.bosk.CatalogReference;
import io.vena.bosk.DriverFactory;
Expand Down Expand Up @@ -32,19 +33,22 @@ public abstract class DriverConformanceTest extends AbstractDriverTest {
protected DriverFactory<TestEntity> driverFactory;

@ParametersByName
@WithSpan
void testInitialState(Path enclosingCatalogPath) {
initializeBoskWithCatalog(enclosingCatalogPath);
assertCorrectBoskContents();
}

@ParametersByName
@WithSpan
void testReplaceIdentical(Path enclosingCatalogPath, Identifier childID) throws InvalidTypeException {
CatalogReference<TestEntity> ref = initializeBoskWithCatalog(enclosingCatalogPath);
driver.submitReplacement(ref.then(childID), newEntity(childID, ref));
assertCorrectBoskContents();
}

@ParametersByName
@WithSpan
void testReplaceDifferent(Path enclosingCatalogPath, Identifier childID) throws InvalidTypeException {
CatalogReference<TestEntity> ref = initializeBoskWithCatalog(enclosingCatalogPath);
driver.submitReplacement(ref.then(childID), newEntity(childID, ref)
Expand All @@ -53,6 +57,7 @@ void testReplaceDifferent(Path enclosingCatalogPath, Identifier childID) throws
}

@ParametersByName
@WithSpan
void testReplaceWholeThenParts(Path enclosingCatalogPath, Identifier childID) throws InvalidTypeException {
CatalogReference<TestEntity> catalogRef = initializeBoskWithCatalog(enclosingCatalogPath);
Identifier awkwardID = Identifier.from(AWKWARD_ID);
Expand Down Expand Up @@ -88,13 +93,15 @@ void testReplaceWholeThenParts(Path enclosingCatalogPath, Identifier childID) th
}

@ParametersByName
@WithSpan
void testDelete(Path enclosingCatalogPath, Identifier childID) {
CatalogReference<TestEntity> ref = initializeBoskWithCatalog(enclosingCatalogPath);
driver.submitDeletion(ref.then(childID));
assertCorrectBoskContents();
}

@ParametersByName
@WithSpan
void testReplaceCatalog(Path enclosingCatalogPath) throws InvalidTypeException {
CatalogReference<TestEntity> ref = initializeBoskWithCatalog(enclosingCatalogPath);
Identifier unique = Identifier.unique("child");
Expand All @@ -107,13 +114,15 @@ void testReplaceCatalog(Path enclosingCatalogPath) throws InvalidTypeException {
}

@ParametersByName
@WithSpan
void testReplaceCatalogEmpty(Path enclosingCatalogPath) {
CatalogReference<TestEntity> ref = initializeBoskWithCatalog(enclosingCatalogPath);
driver.submitReplacement(ref, Catalog.empty());
assertCorrectBoskContents();
}

@ParametersByName
@WithSpan
void testConditionalReplaceFirst(Path enclosingCatalogPath) throws InvalidTypeException {
CatalogReference<TestEntity> ref = initializeBoskWithCatalog(enclosingCatalogPath);
Reference<Identifier> child1IDRef = ref.then(child1ID).then(Identifier.class, TestEntity.Fields.id);
Expand Down Expand Up @@ -150,6 +159,7 @@ void testConditionalReplaceFirst(Path enclosingCatalogPath) throws InvalidTypeEx
}

@ParametersByName
@WithSpan
void testDeleteForward(Path enclosingCatalogPath) {
CatalogReference<TestEntity> ref = initializeBoskWithCatalog(enclosingCatalogPath);
driver.submitDeletion(ref.then(child1ID));
Expand All @@ -159,6 +169,7 @@ void testDeleteForward(Path enclosingCatalogPath) {
}

@ParametersByName
@WithSpan
void testDeleteBackward(Path enclosingCatalogPath) {
CatalogReference<TestEntity> ref = initializeBoskWithCatalog(enclosingCatalogPath);
driver.submitDeletion(ref.then(child2ID));
Expand All @@ -168,6 +179,7 @@ void testDeleteBackward(Path enclosingCatalogPath) {
}

@ParametersByName
@WithSpan
void testConditionalDelete(Path enclosingCatalogPath) throws InvalidTypeException {
CatalogReference<TestEntity> ref = initializeBoskWithCatalog(enclosingCatalogPath);
Reference<Identifier> child1IDRef = ref.then(child1ID).then(Identifier.class, TestEntity.Fields.id);
Expand Down Expand Up @@ -204,6 +216,7 @@ void testConditionalDelete(Path enclosingCatalogPath) throws InvalidTypeExceptio
}

@ParametersByName
@WithSpan
void testDeleteNonexistent(Path enclosingCatalogPath) throws InvalidTypeException {
CatalogReference<TestEntity> ref = initializeBoskWithCatalog(enclosingCatalogPath);
driver.submitDeletion(ref.then(Identifier.from("nonexistent")));
Expand All @@ -213,6 +226,7 @@ void testDeleteNonexistent(Path enclosingCatalogPath) throws InvalidTypeExceptio
}

@ParametersByName
@WithSpan
void testDeleteCatalog_fails(Path enclosingCatalogPath) {
CatalogReference<TestEntity> ref = initializeBoskWithCatalog(enclosingCatalogPath);
assertThrows(IllegalArgumentException.class, ()->
Expand All @@ -221,6 +235,7 @@ void testDeleteCatalog_fails(Path enclosingCatalogPath) {
}

@ParametersByName
@WithSpan
void testDeleteFields_fails(Path enclosingCatalogPath) throws InvalidTypeException {
CatalogReference<TestEntity> ref = initializeBoskWithCatalog(enclosingCatalogPath);
// Use loops instead of parameters to avoid unnecessarily creating and initializing
Expand All @@ -236,6 +251,7 @@ void testDeleteFields_fails(Path enclosingCatalogPath) throws InvalidTypeExcepti
}

@ParametersByName
@WithSpan
void testOptional() throws InvalidTypeException {
Reference<TestValues> ref = initializeBoskWithBlankValues(Path.just(TestEntity.Fields.catalog));
assertCorrectBoskContents();
Expand All @@ -252,6 +268,7 @@ void testOptional() throws InvalidTypeException {
}

@ParametersByName
@WithSpan
void testString() throws InvalidTypeException {
Reference<TestValues> ref = initializeBoskWithBlankValues(Path.just(TestEntity.Fields.catalog));
Reference<String> stringRef = ref.then(String.class, TestValues.Fields.string);
Expand All @@ -265,6 +282,7 @@ void testString() throws InvalidTypeException {
}

@ParametersByName
@WithSpan
void testEnum() throws InvalidTypeException {
Reference<TestValues> ref = initializeBoskWithBlankValues(Path.just(TestEntity.Fields.catalog));
Reference<ChronoUnit> enumRef = ref.then(ChronoUnit.class, TestValues.Fields.chronoUnit);
Expand All @@ -278,6 +296,7 @@ void testEnum() throws InvalidTypeException {
}

@ParametersByName
@WithSpan
void testListValue() throws InvalidTypeException {
Reference<TestValues> ref = initializeBoskWithBlankValues(Path.just(TestEntity.Fields.catalog));
Reference<ListValue<String>> listRef = ref.then(listValue(String.class), TestValues.Fields.list);
Expand All @@ -293,6 +312,7 @@ void testListValue() throws InvalidTypeException {
}

@ParametersByName
@WithSpan
void testMapValue() throws InvalidTypeException {
Reference<TestValues> ref = initializeBoskWithBlankValues(Path.just(TestEntity.Fields.catalog));
Reference<MapValue<String>> mapRef = ref.then(mapValue(String.class), TestValues.Fields.map);
Expand Down Expand Up @@ -326,6 +346,7 @@ void testMapValue() throws InvalidTypeException {
}

@ParametersByName
@WithSpan
void testFlushNothing() throws IOException, InterruptedException {
setupBosksAndReferences(driverFactory);
// Flush before any writes should work
Expand Down

0 comments on commit 9cc496a

Please sign in to comment.