diff --git a/db-client-java/src/main/java/com/eventstore/dbclient/AbstractRegularSubscription.java b/db-client-java/src/main/java/com/eventstore/dbclient/AbstractRegularSubscription.java index 922d17cb..e7678bfa 100644 --- a/db-client-java/src/main/java/com/eventstore/dbclient/AbstractRegularSubscription.java +++ b/db-client-java/src/main/java/com/eventstore/dbclient/AbstractRegularSubscription.java @@ -107,13 +107,17 @@ public void onNext(@NotNull StreamsOuterClass.ReadResp readResp) { try { ResolvedEvent resolvedEvent = ResolvedEvent.fromWire(readResp.getEvent()); - ClientTelemetry.traceSubscribe( - () -> listener.onEvent(this._subscription, resolvedEvent), - _subscription.getSubscriptionId(), - channel, - client.getSettings(), - options.getCredentials(), - resolvedEvent.getEvent()); + if (ClientTelemetry.isEnabled()) { + listener.onEvent(this._subscription, resolvedEvent); + } else { + ClientTelemetry.traceSubscribe( + () -> listener.onEvent(this._subscription, resolvedEvent), + _subscription.getSubscriptionId(), + channel, + client.getSettings(), + options.getCredentials(), + resolvedEvent.getEvent()); + } } catch (Exception e) { onError(e); } diff --git a/db-client-java/src/main/java/com/eventstore/dbclient/AbstractSubscribePersistentSubscription.java b/db-client-java/src/main/java/com/eventstore/dbclient/AbstractSubscribePersistentSubscription.java index 9419bf88..4c7815b6 100644 --- a/db-client-java/src/main/java/com/eventstore/dbclient/AbstractSubscribePersistentSubscription.java +++ b/db-client-java/src/main/java/com/eventstore/dbclient/AbstractSubscribePersistentSubscription.java @@ -92,13 +92,17 @@ public void onNext(Persistent.ReadResp readResp) { try { ResolvedEvent resolvedEvent = ResolvedEvent.fromWire(readResp.getEvent()); - ClientTelemetry.traceSubscribe( - () -> listener.onEvent(this._subscription, retryCount, resolvedEvent), - _subscription.getSubscriptionId(), - args.getChannel(), - client.getSettings(), - options.getCredentials(), - resolvedEvent.getEvent()); + if (ClientTelemetry.isEnabled()) { + listener.onEvent(this._subscription, retryCount, resolvedEvent); + } else { + ClientTelemetry.traceSubscribe( + () -> listener.onEvent(this._subscription, retryCount, resolvedEvent), + _subscription.getSubscriptionId(), + args.getChannel(), + client.getSettings(), + options.getCredentials(), + resolvedEvent.getEvent()); + } } catch (Exception e) { onError(e); } diff --git a/db-client-java/src/main/java/com/eventstore/dbclient/AppendToStream.java b/db-client-java/src/main/java/com/eventstore/dbclient/AppendToStream.java index 2f3a8140..885afc39 100644 --- a/db-client-java/src/main/java/com/eventstore/dbclient/AppendToStream.java +++ b/db-client-java/src/main/java/com/eventstore/dbclient/AppendToStream.java @@ -31,13 +31,18 @@ public AppendToStream(GrpcClient client, String streamName, Iterator } public CompletableFuture execute() { - return this.client.run(channel -> ClientTelemetry.traceAppend( - this::append, - channel, - events, - this.streamName, - this.client.getSettings(), - this.options.getCredentials())); + return this.client.run(channel -> { + if (ClientTelemetry.isEnabled()) + return append(channel, events); + + return ClientTelemetry.traceAppend( + this::append, + channel, + events, + this.streamName, + this.client.getSettings(), + this.options.getCredentials()); + }); } private CompletableFuture append(ManagedChannel channel, List events) { diff --git a/db-client-java/src/main/java/com/eventstore/dbclient/ClientTelemetry.java b/db-client-java/src/main/java/com/eventstore/dbclient/ClientTelemetry.java index 4d05c8c6..f9dc59cd 100644 --- a/db-client-java/src/main/java/com/eventstore/dbclient/ClientTelemetry.java +++ b/db-client-java/src/main/java/com/eventstore/dbclient/ClientTelemetry.java @@ -5,9 +5,11 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.grpc.ManagedChannel; import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.*; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; +import io.opentelemetry.context.propagation.TextMapPropagator; import java.util.ArrayList; import java.util.List; @@ -18,6 +20,10 @@ import java.util.function.BiFunction; class ClientTelemetry { + protected static boolean isEnabled() { + return OpenTelemetry.noop().getPropagators().getTextMapPropagator() == TextMapPropagator.noop(); + } + private static final ClientTelemetryTags DEFAULT_ATTRIBUTES = new ClientTelemetryTags() {{ put(ClientTelemetryAttributes.Database.SYSTEM, ClientTelemetryConstants.INSTRUMENTATION_NAME); }}; diff --git a/db-client-java/src/test/java/com/eventstore/dbclient/streams/AppendTests.java b/db-client-java/src/test/java/com/eventstore/dbclient/streams/AppendTests.java index b2b5ff24..48f58ba4 100644 --- a/db-client-java/src/test/java/com/eventstore/dbclient/streams/AppendTests.java +++ b/db-client-java/src/test/java/com/eventstore/dbclient/streams/AppendTests.java @@ -9,45 +9,64 @@ public interface AppendTests extends ConnectionAware { @Test - default void testAppendSingleEventNoStream() throws Throwable { + default void testAppendMultipleEvents() throws Throwable { EventStoreDBClient client = getDatabase().defaultClient(); final String streamName = generateName(); final String eventType = "TestEvent"; - final String eventId = "38fffbc2-339e-11ea-8c7b-784f43837872"; + final String eventId1 = UUID.randomUUID().toString(); + final String eventId2 = UUID.randomUUID().toString(); final byte[] eventMetaData = new byte[]{0xd, 0xe, 0xa, 0xd}; final JsonMapper jsonMapper = new JsonMapper(); - EventData event = EventData.builderAsJson(eventType, jsonMapper.writeValueAsBytes(new Foo())) + // Create first event data with metadata bytes + EventData event1 = EventData.builderAsJson(eventType, jsonMapper.writeValueAsBytes(new Foo())) .metadataAsBytes(eventMetaData) - .eventId(UUID.fromString(eventId)) + .eventId(UUID.fromString(eventId1)) + .build(); + + // Create second event data with JSON metadata + EventData event2 = EventData.builderAsJson(eventType, jsonMapper.writeValueAsBytes(new Foo())) + .metadataAsBytes(jsonMapper.writeValueAsBytes(new Foo())) + .eventId(UUID.fromString(eventId2)) .build(); AppendToStreamOptions appendOptions = AppendToStreamOptions.get() .expectedRevision(ExpectedRevision.noStream()); - WriteResult appendResult = client.appendToStream(streamName, appendOptions, event) + // Append both events to stream + WriteResult appendResult = client.appendToStream(streamName, appendOptions, event1, event2) .get(); - Assertions.assertEquals(ExpectedRevision.expectedRevision(0), appendResult.getNextExpectedRevision()); + // Validate the append operation + Assertions.assertEquals(ExpectedRevision.expectedRevision(1), appendResult.getNextExpectedRevision()); ReadStreamOptions readStreamOptions = ReadStreamOptions.get() .fromEnd() .backwards() - .maxCount(1); + .maxCount(2); - // Ensure appended event is readable + // Ensure both appended events are readable ReadResult result = client.readStream(streamName, readStreamOptions) .get(); - Assertions.assertEquals(1, result.getEvents().size()); - RecordedEvent first = result.getEvents().get(0).getEvent(); + Assertions.assertEquals(2, result.getEvents().size()); + RecordedEvent first = result.getEvents().get(1).getEvent(); + RecordedEvent second = result.getEvents().get(0).getEvent(); JsonMapper mapper = new JsonMapper(); + // Verify first event details Assertions.assertEquals(streamName, first.getStreamId()); Assertions.assertEquals(eventType, first.getEventType()); - Assertions.assertEquals(eventId, first.getEventId().toString()); + Assertions.assertEquals(eventId1, first.getEventId().toString()); Assertions.assertArrayEquals(eventMetaData, first.getUserMetadata()); Assertions.assertEquals(new Foo(), mapper.readValue(first.getEventData(), Foo.class)); + + // Verify second event details + Assertions.assertEquals(streamName, second.getStreamId()); + Assertions.assertEquals(eventType, second.getEventType()); + Assertions.assertEquals(eventId2, second.getEventId().toString()); + Assertions.assertEquals(new Foo(), mapper.readValue(second.getEventData(), Foo.class)); } } +