|
28 | 28 | import io.nats.client.Subscription;
|
29 | 29 | import io.nats.client.api.ServerInfo;
|
30 | 30 | import io.nats.client.impl.Headers;
|
31 |
| -import io.nats.client.impl.NatsMessage; |
32 | 31 | import io.opentelemetry.api.trace.Span;
|
33 | 32 | import io.opentelemetry.context.Context;
|
34 | 33 | import io.opentelemetry.context.Scope;
|
35 | 34 | import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
|
| 35 | +import io.opentelemetry.instrumentation.nats.v2_21.internal.NatsRequest; |
36 | 36 | import java.io.IOException;
|
37 | 37 | import java.net.InetAddress;
|
38 | 38 | import java.time.Duration;
|
|
43 | 43 | public class OpenTelemetryConnection implements Connection {
|
44 | 44 |
|
45 | 45 | private final Connection delegate;
|
46 |
| - private final Instrumenter<Message, Void> producerInstrumenter; |
| 46 | + private final Instrumenter<NatsRequest, Void> producerInstrumenter; |
47 | 47 |
|
48 | 48 | public OpenTelemetryConnection(
|
49 |
| - Connection connection, Instrumenter<Message, Void> producerInstrumenter) { |
| 49 | + Connection connection, Instrumenter<NatsRequest, Void> producerInstrumenter) { |
50 | 50 | this.delegate = connection;
|
51 | 51 | this.producerInstrumenter = producerInstrumenter;
|
52 | 52 | }
|
53 | 53 |
|
54 | 54 | @Override
|
55 | 55 | public void publish(String subject, byte[] body) {
|
56 |
| - this.publish(NatsMessage.builder().subject(subject).data(body).build()); |
| 56 | + wrapPublish(NatsRequest.create(this, subject, body), () -> delegate.publish(subject, body)); |
57 | 57 | }
|
58 | 58 |
|
59 | 59 | @Override
|
60 | 60 | public void publish(String subject, Headers headers, byte[] body) {
|
61 |
| - this.publish(NatsMessage.builder().subject(subject).headers(headers).data(body).build()); |
| 61 | + wrapPublish( |
| 62 | + NatsRequest.create(this, subject, headers, body), |
| 63 | + () -> delegate.publish(subject, headers, body)); |
62 | 64 | }
|
63 | 65 |
|
64 | 66 | @Override
|
65 | 67 | public void publish(String subject, String replyTo, byte[] body) {
|
66 |
| - this.publish(NatsMessage.builder().subject(subject).replyTo(replyTo).data(body).build()); |
| 68 | + wrapPublish( |
| 69 | + NatsRequest.create(this, subject, body), () -> delegate.publish(subject, replyTo, body)); |
67 | 70 | }
|
68 | 71 |
|
69 | 72 | @Override
|
70 | 73 | public void publish(String subject, String replyTo, Headers headers, byte[] body) {
|
71 |
| - this.publish( |
72 |
| - NatsMessage.builder() |
73 |
| - .subject(subject) |
74 |
| - .replyTo(replyTo) |
75 |
| - .headers(headers) |
76 |
| - .data(body) |
77 |
| - .build()); |
| 74 | + wrapPublish( |
| 75 | + NatsRequest.create(this, subject, headers, body), |
| 76 | + () -> delegate.publish(subject, replyTo, headers, body)); |
78 | 77 | }
|
79 | 78 |
|
80 | 79 | @Override
|
81 | 80 | public void publish(Message message) {
|
82 |
| - Context parentContext = Context.current(); |
83 |
| - |
84 |
| - if (!Span.fromContext(parentContext).getSpanContext().isValid() |
85 |
| - || !producerInstrumenter.shouldStart(parentContext, message)) { |
86 |
| - delegate.publish(message); |
87 |
| - return; |
88 |
| - } |
89 |
| - |
90 |
| - Message otelMessage = new OpenTelemetryMessage(this, message); |
91 |
| - Context context = producerInstrumenter.start(parentContext, otelMessage); |
92 |
| - |
93 |
| - try (Scope ignored = context.makeCurrent()) { |
94 |
| - delegate.publish(otelMessage); |
95 |
| - } finally { |
96 |
| - producerInstrumenter.end(context, otelMessage, null, null); |
97 |
| - } |
| 81 | + wrapPublish(NatsRequest.create(this, message), () -> delegate.publish(message)); |
98 | 82 | }
|
99 | 83 |
|
100 | 84 | @Override
|
@@ -356,4 +340,21 @@ public ObjectStoreManagement objectStoreManagement(ObjectStoreOptions objectStor
|
356 | 340 | throws IOException {
|
357 | 341 | return delegate.objectStoreManagement(objectStoreOptions);
|
358 | 342 | }
|
| 343 | + |
| 344 | + private void wrapPublish(NatsRequest natsRequest, Runnable publish) { |
| 345 | + Context parentContext = Context.current(); |
| 346 | + |
| 347 | + if (!Span.fromContext(parentContext).getSpanContext().isValid() |
| 348 | + || !producerInstrumenter.shouldStart(parentContext, natsRequest)) { |
| 349 | + publish.run(); |
| 350 | + return; |
| 351 | + } |
| 352 | + |
| 353 | + Context context = producerInstrumenter.start(parentContext, natsRequest); |
| 354 | + try (Scope ignored = context.makeCurrent()) { |
| 355 | + publish.run(); |
| 356 | + } finally { |
| 357 | + producerInstrumenter.end(context, natsRequest, null, null); |
| 358 | + } |
| 359 | + } |
359 | 360 | }
|
0 commit comments