Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract tracing metadata from Event not OriginalEvent #284

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void onNext(@NotNull StreamsOuterClass.ReadResp readResp) {
channel,
client.getSettings(),
options.getCredentials(),
resolvedEvent.getOriginalEvent());
resolvedEvent.getEvent());
} catch (Exception e) {
onError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void onNext(Persistent.ReadResp readResp) {
args.getChannel(),
client.getSettings(),
options.getCredentials(),
resolvedEvent.getOriginalEvent());
resolvedEvent.getEvent());
} catch (Exception e) {
onError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,18 @@ private static SpanContext tryExtractTracingContext(byte[] userMetadataBytes) {
if (!TraceId.isValid(traceId) || !SpanId.isValid(spanId))
return null;

return SpanContext.createFromRemoteParent(traceId, spanId, TraceFlags.getSampled(), TraceState.getDefault());
return SpanContext.createFromRemoteParent(traceId, spanId, TraceFlags.getSampled(),
TraceState.getDefault());
} catch (Throwable t) {
return null;
}
}

static CompletableFuture<WriteResult> traceAppend(BiFunction<ManagedChannel, List<EventData>, CompletableFuture<WriteResult>> appendOperation, ManagedChannel channel,
List<EventData> events, String streamId, EventStoreDBClientSettings settings, UserCredentials optionalCallCredentials) {
static CompletableFuture<WriteResult> traceAppend(
BiFunction<ManagedChannel, List<EventData>, CompletableFuture<WriteResult>> appendOperation,
ManagedChannel channel,
List<EventData> events, String streamId, EventStoreDBClientSettings settings,
UserCredentials optionalCallCredentials) {
Span span = createSpan(
ClientTelemetryConstants.Operations.APPEND,
SpanKind.CLIENT,
Expand Down Expand Up @@ -115,9 +119,16 @@ static CompletableFuture<WriteResult> traceAppend(BiFunction<ManagedChannel, Lis
}
}

static void traceSubscribe(Runnable tracedOperation, String subscriptionId, ManagedChannel channel, EventStoreDBClientSettings settings,
static void traceSubscribe(Runnable tracedOperation, String subscriptionId, ManagedChannel channel,
EventStoreDBClientSettings settings,
UserCredentials optionalCallCredentials, RecordedEvent event) {
SpanContext remoteParentContext = tryExtractTracingContext(event.getUserMetadata());

if (remoteParentContext == null) {
tracedOperation.run();
return;
}

Span span = createSpan(
ClientTelemetryConstants.Operations.SUBSCRIBE,
SpanKind.CONSUMER,
Expand Down Expand Up @@ -145,7 +156,8 @@ static void traceSubscribe(Runnable tracedOperation, String subscriptionId, Mana
}
}

static Span createSpan(String operationName, SpanKind spanKind, SpanContext parentContext, ClientTelemetryTags customAttributes) {
static Span createSpan(String operationName, SpanKind spanKind, SpanContext parentContext,
ClientTelemetryTags customAttributes) {
SpanBuilder spanBuilder = getTracer().spanBuilder(operationName).setSpanKind(spanKind);

if (parentContext != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,62 @@ default void testTracingContextIsInjectedAsExpectedWhenUserMetadataIsJsonObject(
}

@Test
default void testTracingContextInjectionIsIgnoredAsExpectedWhenUserMetadataIsNonNullAndNotAJsonObject() throws Throwable {
@Timeout(value = 2, unit = TimeUnit.MINUTES)
default void testTracingContextInjectionIsIgnoredAsExpectedWhenUserMetadataIsNonNullAndNotAJsonObject()
throws Throwable {
EventStoreDBClient client = getDefaultClient();
String streamName = generateName();
byte[] userMetadata = mapper.writeValueAsBytes("clearlynotvalidjson");

EventData eventWithValidMetadata = EventData.builderAsJson("TestEvent", mapper.writeValueAsBytes(new Foo()))
.eventId(UUID.randomUUID())
.build();

EventData eventWithInvalidMetadata = EventData.builderAsJson("TestEvent", mapper.writeValueAsBytes(new Foo()))
.metadataAsBytes(userMetadata)
.eventId(UUID.randomUUID())
.build();

client.appendToStream(
streamName,
AppendToStreamOptions.get().expectedRevision(ExpectedRevision.noStream()),
EventData.builderAsJson("TestEvent", mapper.writeValueAsBytes(new Foo()))
.metadataAsBytes(userMetadata)
.eventId(UUID.randomUUID())
.build())
eventWithValidMetadata,
eventWithInvalidMetadata)
.get();

ReadResult readResult = client.readStream(streamName, ReadStreamOptions.get()).get();

ResolvedEvent resolvedEvent = readResult.getEvents().get(0);
Assertions.assertNotNull(resolvedEvent);
List<ResolvedEvent> resolvedEvent = readResult.getEvents();
Assertions.assertEquals(2, resolvedEvent.size());

// Assert unchanged
Assertions.assertArrayEquals(userMetadata, resolvedEvent.getEvent().getUserMetadata());
Assertions.assertArrayEquals(userMetadata, resolvedEvent.get(1).getEvent().getUserMetadata());

CountDownLatch subscribeSpansLatch = new CountDownLatch(1);
onOperationSpanEnded(ClientTelemetryConstants.Operations.SUBSCRIBE, span -> subscribeSpansLatch.countDown());

Subscription subscription = client.subscribeToStream(
streamName,
new SubscriptionListener() {
}
).get();

List<ReadableSpan> appendSpans = this.getSpansForOperation(ClientTelemetryConstants.Operations.APPEND);
Assertions.assertEquals(1, appendSpans.size());

subscribeSpansLatch.await();
subscription.stop();

List<ReadableSpan> subscribeSpans = this.getSpansForOperation(ClientTelemetryConstants.Operations.SUBSCRIBE);

Assertions.assertEquals(1, subscribeSpans.size());

assertSubscriptionActivityHasExpectedAttributes(
subscribeSpans.get(0),
streamName,
subscription.getSubscriptionId(),
eventWithValidMetadata.getEventId().toString(),
eventWithValidMetadata.getEventType());
}

@Test
Expand Down
Loading