Skip to content

Commit e812b03

Browse files
committed
Do inject trace context when opentelemetry is disabled
1 parent 94ee76a commit e812b03

File tree

5 files changed

+69
-32
lines changed

5 files changed

+69
-32
lines changed

db-client-java/src/main/java/com/eventstore/dbclient/AbstractRegularSubscription.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,17 @@ public void onNext(@NotNull StreamsOuterClass.ReadResp readResp) {
107107

108108
try {
109109
ResolvedEvent resolvedEvent = ResolvedEvent.fromWire(readResp.getEvent());
110-
ClientTelemetry.traceSubscribe(
111-
() -> listener.onEvent(this._subscription, resolvedEvent),
112-
_subscription.getSubscriptionId(),
113-
channel,
114-
client.getSettings(),
115-
options.getCredentials(),
116-
resolvedEvent.getEvent());
110+
if (ClientTelemetry.isEnabled()) {
111+
listener.onEvent(this._subscription, resolvedEvent);
112+
} else {
113+
ClientTelemetry.traceSubscribe(
114+
() -> listener.onEvent(this._subscription, resolvedEvent),
115+
_subscription.getSubscriptionId(),
116+
channel,
117+
client.getSettings(),
118+
options.getCredentials(),
119+
resolvedEvent.getEvent());
120+
}
117121
} catch (Exception e) {
118122
onError(e);
119123
}

db-client-java/src/main/java/com/eventstore/dbclient/AbstractSubscribePersistentSubscription.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,17 @@ public void onNext(Persistent.ReadResp readResp) {
9292

9393
try {
9494
ResolvedEvent resolvedEvent = ResolvedEvent.fromWire(readResp.getEvent());
95-
ClientTelemetry.traceSubscribe(
96-
() -> listener.onEvent(this._subscription, retryCount, resolvedEvent),
97-
_subscription.getSubscriptionId(),
98-
args.getChannel(),
99-
client.getSettings(),
100-
options.getCredentials(),
101-
resolvedEvent.getEvent());
95+
if (ClientTelemetry.isEnabled()) {
96+
listener.onEvent(this._subscription, retryCount, resolvedEvent);
97+
} else {
98+
ClientTelemetry.traceSubscribe(
99+
() -> listener.onEvent(this._subscription, retryCount, resolvedEvent),
100+
_subscription.getSubscriptionId(),
101+
args.getChannel(),
102+
client.getSettings(),
103+
options.getCredentials(),
104+
resolvedEvent.getEvent());
105+
}
102106
} catch (Exception e) {
103107
onError(e);
104108
}

db-client-java/src/main/java/com/eventstore/dbclient/AppendToStream.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ public AppendToStream(GrpcClient client, String streamName, Iterator<EventData>
3131
}
3232

3333
public CompletableFuture<WriteResult> execute() {
34-
return this.client.run(channel -> ClientTelemetry.traceAppend(
35-
this::append,
36-
channel,
37-
events,
38-
this.streamName,
39-
this.client.getSettings(),
40-
this.options.getCredentials()));
34+
return this.client.run(channel -> {
35+
if (ClientTelemetry.isEnabled())
36+
return append(channel, events);
37+
38+
return ClientTelemetry.traceAppend(
39+
this::append,
40+
channel,
41+
events,
42+
this.streamName,
43+
this.client.getSettings(),
44+
this.options.getCredentials());
45+
});
4146
}
4247

4348
private CompletableFuture<WriteResult> append(ManagedChannel channel, List<EventData> events) {

db-client-java/src/main/java/com/eventstore/dbclient/ClientTelemetry.java

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.opentelemetry.api.trace.*;
99
import io.opentelemetry.context.Context;
1010
import io.opentelemetry.context.Scope;
11+
import io.opentelemetry.context.propagation.TextMapPropagator;
1112

1213
import java.util.ArrayList;
1314
import java.util.List;
@@ -18,6 +19,10 @@
1819
import java.util.function.BiFunction;
1920

2021
class ClientTelemetry {
22+
protected static boolean isEnabled() {
23+
return GlobalOpenTelemetry.get().getPropagators().getTextMapPropagator() == TextMapPropagator.noop();
24+
}
25+
2126
private static final ClientTelemetryTags DEFAULT_ATTRIBUTES = new ClientTelemetryTags() {{
2227
put(ClientTelemetryAttributes.Database.SYSTEM, ClientTelemetryConstants.INSTRUMENTATION_NAME);
2328
}};

db-client-java/src/test/java/com/eventstore/dbclient/streams/AppendTests.java

+30-11
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,64 @@
99

1010
public interface AppendTests extends ConnectionAware {
1111
@Test
12-
default void testAppendSingleEventNoStream() throws Throwable {
12+
default void testAppendAndTraceMultipleEvents() throws Throwable {
1313
EventStoreDBClient client = getDatabase().defaultClient();
1414

1515
final String streamName = generateName();
1616
final String eventType = "TestEvent";
17-
final String eventId = "38fffbc2-339e-11ea-8c7b-784f43837872";
17+
final String eventId1 = UUID.randomUUID().toString();
18+
final String eventId2 = UUID.randomUUID().toString();
1819
final byte[] eventMetaData = new byte[]{0xd, 0xe, 0xa, 0xd};
1920
final JsonMapper jsonMapper = new JsonMapper();
2021

21-
EventData event = EventData.builderAsJson(eventType, jsonMapper.writeValueAsBytes(new Foo()))
22+
// Create first event data with metadata bytes
23+
EventData event1 = EventData.builderAsJson(eventType, jsonMapper.writeValueAsBytes(new Foo()))
2224
.metadataAsBytes(eventMetaData)
23-
.eventId(UUID.fromString(eventId))
25+
.eventId(UUID.fromString(eventId1))
26+
.build();
27+
28+
// Create second event data with JSON metadata
29+
EventData event2 = EventData.builderAsJson(eventType, jsonMapper.writeValueAsBytes(new Foo()))
30+
.metadataAsBytes(jsonMapper.writeValueAsBytes(new Foo()))
31+
.eventId(UUID.fromString(eventId2))
2432
.build();
2533

2634
AppendToStreamOptions appendOptions = AppendToStreamOptions.get()
2735
.expectedRevision(ExpectedRevision.noStream());
2836

29-
WriteResult appendResult = client.appendToStream(streamName, appendOptions, event)
37+
// Append both events to stream
38+
WriteResult appendResult = client.appendToStream(streamName, appendOptions, event1, event2)
3039
.get();
3140

32-
Assertions.assertEquals(ExpectedRevision.expectedRevision(0), appendResult.getNextExpectedRevision());
41+
// Validate the append operation
42+
Assertions.assertEquals(ExpectedRevision.expectedRevision(1), appendResult.getNextExpectedRevision());
3343

3444
ReadStreamOptions readStreamOptions = ReadStreamOptions.get()
3545
.fromEnd()
3646
.backwards()
37-
.maxCount(1);
47+
.maxCount(2);
3848

39-
// Ensure appended event is readable
49+
// Ensure both appended events are readable
4050
ReadResult result = client.readStream(streamName, readStreamOptions)
4151
.get();
4252

43-
Assertions.assertEquals(1, result.getEvents().size());
44-
RecordedEvent first = result.getEvents().get(0).getEvent();
53+
Assertions.assertEquals(2, result.getEvents().size());
54+
RecordedEvent first = result.getEvents().get(1).getEvent();
55+
RecordedEvent second = result.getEvents().get(0).getEvent();
4556
JsonMapper mapper = new JsonMapper();
4657

58+
// Verify first event details
4759
Assertions.assertEquals(streamName, first.getStreamId());
4860
Assertions.assertEquals(eventType, first.getEventType());
49-
Assertions.assertEquals(eventId, first.getEventId().toString());
61+
Assertions.assertEquals(eventId1, first.getEventId().toString());
5062
Assertions.assertArrayEquals(eventMetaData, first.getUserMetadata());
5163
Assertions.assertEquals(new Foo(), mapper.readValue(first.getEventData(), Foo.class));
64+
65+
// Verify second event details
66+
Assertions.assertEquals(streamName, second.getStreamId());
67+
Assertions.assertEquals(eventType, second.getEventType());
68+
Assertions.assertEquals(eventId2, second.getEventId().toString());
69+
Assertions.assertEquals(new Foo(), mapper.readValue(second.getEventData(), Foo.class));
5270
}
5371
}
72+

0 commit comments

Comments
 (0)