-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathOpenTelemetryPubsubTracer.java
More file actions
460 lines (413 loc) · 15.6 KB
/
OpenTelemetryPubsubTracer.java
File metadata and controls
460 lines (413 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.pubsub.v1;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.SubscriptionName;
import com.google.pubsub.v1.TopicName;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.List;
public class OpenTelemetryPubsubTracer {
private final Tracer tracer;
private boolean enabled = false;
private static final String PUBLISH_FLOW_CONTROL_SPAN_NAME = "publisher flow control";
private static final String PUBLISH_BATCHING_SPAN_NAME = "publisher batching";
private static final String SUBSCRIBE_CONCURRENCY_CONTROL_SPAN_NAME =
"subscriber concurrency control";
private static final String SUBSCRIBE_SCHEDULER_SPAN_NAME = "subscriber scheduler";
private static final String MESSAGE_SIZE_ATTR_KEY = "messaging.message.body.size";
private static final String ORDERING_KEY_ATTR_KEY = "messaging.gcp_pubsub.message.ordering_key";
private static final String MESSAGE_ACK_ID_ATTR_KEY = "messaging.gcp_pubsub.message.ack_id";
private static final String MESSAGE_EXACTLY_ONCE_ATTR_KEY =
"messaging.gcp_pubsub.message.exactly_once_delivery";
private static final String MESSAGE_DELIVERY_ATTEMPT_ATTR_KEY =
"messaging.gcp_pubsub.message.delivery_attempt";
private static final String PROJECT_ATTR_KEY = "gcp.project_id";
private static final String PUBLISH_RPC_SPAN_SUFFIX = " publish";
private static final String MESSAGING_SYSTEM_VALUE = "gcp_pubsub";
OpenTelemetryPubsubTracer(Tracer tracer, boolean enableOpenTelemetry) {
this.tracer = tracer;
if (this.tracer != null && enableOpenTelemetry) {
this.enabled = true;
}
}
/** Populates attributes that are common the publisher parent span and publish RPC span. */
private static final AttributesBuilder createCommonSpanAttributesBuilder(
String destinationName, String projectName, String codeFunction, String operation) {
AttributesBuilder attributesBuilder =
Attributes.builder()
.put(SemanticAttributes.MESSAGING_SYSTEM, MESSAGING_SYSTEM_VALUE)
.put(SemanticAttributes.MESSAGING_DESTINATION_NAME, destinationName)
.put(PROJECT_ATTR_KEY, projectName)
.put(SemanticAttributes.CODE_FUNCTION, codeFunction);
if (operation != null) {
attributesBuilder.put(SemanticAttributes.MESSAGING_OPERATION, operation);
}
return attributesBuilder;
}
private Span startChildSpan(String name, Span parent) {
return tracer.spanBuilder(name).setParent(Context.current().with(parent)).startSpan();
}
/**
* Creates and starts the parent span with the appropriate span attributes and injects the span
* context into the {@link PubsubMessage} attributes.
*/
void startPublisherSpan(PubsubMessageWrapper message) {
if (!enabled) {
return;
}
AttributesBuilder attributesBuilder =
createCommonSpanAttributesBuilder(
message.getTopicName(), message.getTopicProject(), "publish", "create");
attributesBuilder.put(MESSAGE_SIZE_ATTR_KEY, message.getDataSize());
if (!message.getOrderingKey().isEmpty()) {
attributesBuilder.put(ORDERING_KEY_ATTR_KEY, message.getOrderingKey());
}
Span publisherSpan =
tracer
.spanBuilder(message.getTopicName() + " create")
.setSpanKind(SpanKind.PRODUCER)
.setAllAttributes(attributesBuilder.build())
.startSpan();
message.setPublisherSpan(publisherSpan);
if (publisherSpan.getSpanContext().isValid()) {
message.injectSpanContext();
}
}
void endPublisherSpan(PubsubMessageWrapper message) {
if (!enabled) {
return;
}
message.endPublisherSpan();
}
void setPublisherMessageIdSpanAttribute(PubsubMessageWrapper message, String messageId) {
if (!enabled) {
return;
}
message.setPublisherMessageIdSpanAttribute(messageId);
}
/** Creates a span for publish-side flow control as a child of the parent publisher span. */
void startPublishFlowControlSpan(PubsubMessageWrapper message) {
if (!enabled) {
return;
}
Span publisherSpan = message.getPublisherSpan();
if (publisherSpan != null)
message.setPublishFlowControlSpan(
startChildSpan(PUBLISH_FLOW_CONTROL_SPAN_NAME, publisherSpan));
}
void endPublishFlowControlSpan(PubsubMessageWrapper message) {
if (!enabled) {
return;
}
message.endPublishFlowControlSpan();
}
void setPublishFlowControlSpanException(PubsubMessageWrapper message, Throwable t) {
if (!enabled) {
return;
}
message.setPublishFlowControlSpanException(t);
}
/** Creates a span for publish message batching as a child of the parent publisher span. */
void startPublishBatchingSpan(PubsubMessageWrapper message) {
if (!enabled) {
return;
}
Span publisherSpan = message.getPublisherSpan();
if (publisherSpan != null) {
message.setPublishBatchingSpan(startChildSpan(PUBLISH_BATCHING_SPAN_NAME, publisherSpan));
}
}
void endPublishBatchingSpan(PubsubMessageWrapper message) {
if (!enabled) {
return;
}
message.endPublishBatchingSpan();
}
/**
* Creates, starts, and returns a publish RPC span for the given message batch. Bi-directional
* links with the publisher parent span are created for sampled messages in the batch.
*/
Span startPublishRpcSpan(TopicName topicName, List<PubsubMessageWrapper> messages) {
if (!enabled) {
return null;
}
Attributes attributes =
createCommonSpanAttributesBuilder(
topicName.getTopic(), topicName.getProject(), "publishCall", "publish")
.put(SemanticAttributes.MESSAGING_BATCH_MESSAGE_COUNT, messages.size())
.build();
SpanBuilder publishRpcSpanBuilder =
tracer
.spanBuilder(topicName.getTopic() + PUBLISH_RPC_SPAN_SUFFIX)
.setSpanKind(SpanKind.CLIENT)
.setAllAttributes(attributes);
Attributes linkAttributes =
Attributes.builder().put(SemanticAttributes.MESSAGING_OPERATION, "publish").build();
for (PubsubMessageWrapper message : messages) {
if (message.getPublisherSpan().getSpanContext().isSampled())
publishRpcSpanBuilder.addLink(message.getPublisherSpan().getSpanContext(), linkAttributes);
}
Span publishRpcSpan = publishRpcSpanBuilder.startSpan();
for (PubsubMessageWrapper message : messages) {
if (publishRpcSpan.getSpanContext().isSampled()) {
message.getPublisherSpan().addLink(publishRpcSpan.getSpanContext(), linkAttributes);
message.addPublishStartEvent();
}
}
return publishRpcSpan;
}
/** Ends the given publish RPC span if it exists. */
void endPublishRpcSpan(Span publishRpcSpan) {
if (!enabled) {
return;
}
if (publishRpcSpan != null) {
publishRpcSpan.end();
}
}
/**
* Sets an error status and records an exception when an exception is thrown when publishing the
* message batch.
*/
void setPublishRpcSpanException(Span publishRpcSpan, Throwable t) {
if (!enabled) {
return;
}
if (publishRpcSpan != null) {
publishRpcSpan.setStatus(StatusCode.ERROR, "Exception thrown on publish RPC.");
publishRpcSpan.recordException(t);
publishRpcSpan.end();
}
}
void startSubscriberSpan(PubsubMessageWrapper message, boolean exactlyOnceDeliveryEnabled) {
if (!enabled) {
return;
}
AttributesBuilder attributesBuilder =
createCommonSpanAttributesBuilder(
message.getSubscriptionName(), message.getSubscriptionProject(), "onResponse", null);
attributesBuilder
.put(SemanticAttributes.MESSAGING_MESSAGE_ID, message.getMessageId())
.put(MESSAGE_SIZE_ATTR_KEY, message.getDataSize())
.put(MESSAGE_ACK_ID_ATTR_KEY, message.getAckId())
.put(MESSAGE_EXACTLY_ONCE_ATTR_KEY, exactlyOnceDeliveryEnabled);
if (!message.getOrderingKey().isEmpty()) {
attributesBuilder.put(ORDERING_KEY_ATTR_KEY, message.getOrderingKey());
}
if (message.getDeliveryAttempt() > 0) {
attributesBuilder.put(MESSAGE_DELIVERY_ATTEMPT_ATTR_KEY, message.getDeliveryAttempt());
}
Attributes attributes = attributesBuilder.build();
Context publisherSpanContext = message.extractSpanContext(attributes);
message.setPublisherSpan(Span.fromContextOrNull(publisherSpanContext));
message.setSubscriberSpan(
tracer
.spanBuilder(message.getSubscriptionName() + " subscribe")
.setSpanKind(SpanKind.CONSUMER)
.setParent(publisherSpanContext)
.setAllAttributes(attributes)
.startSpan());
}
void endSubscriberSpan(PubsubMessageWrapper message) {
if (!enabled) {
return;
}
message.endSubscriberSpan();
}
void setSubscriberSpanExpirationResult(PubsubMessageWrapper message) {
if (!enabled) {
return;
}
message.setSubscriberSpanExpirationResult();
}
void setSubscriberSpanException(PubsubMessageWrapper message, Throwable t, String exception) {
if (!enabled) {
return;
}
message.setSubscriberSpanException(t, exception);
}
/** Creates a span for subscribe concurrency control as a child of the parent subscriber span. */
void startSubscribeConcurrencyControlSpan(PubsubMessageWrapper message) {
if (!enabled) {
return;
}
Span subscriberSpan = message.getSubscriberSpan();
if (subscriberSpan != null) {
message.setSubscribeConcurrencyControlSpan(
startChildSpan(SUBSCRIBE_CONCURRENCY_CONTROL_SPAN_NAME, subscriberSpan));
}
}
void endSubscribeConcurrencyControlSpan(PubsubMessageWrapper message) {
if (!enabled) {
return;
}
message.endSubscribeConcurrencyControlSpan();
}
void setSubscribeConcurrencyControlSpanException(PubsubMessageWrapper message, Throwable t) {
if (!enabled) {
return;
}
message.setSubscribeConcurrencyControlSpanException(t);
}
/**
* Creates a span for subscribe ordering key scheduling as a child of the parent subscriber span.
*/
void startSubscribeSchedulerSpan(PubsubMessageWrapper message) {
if (!enabled) {
return;
}
Span subscriberSpan = message.getSubscriberSpan();
if (subscriberSpan != null) {
message.setSubscribeSchedulerSpan(
startChildSpan(SUBSCRIBE_SCHEDULER_SPAN_NAME, subscriberSpan));
}
}
void endSubscribeSchedulerSpan(PubsubMessageWrapper message) {
if (!enabled) {
return;
}
message.endSubscribeSchedulerSpan();
}
/** Creates a span for subscribe message processing as a child of the parent subscriber span. */
void startSubscribeProcessSpan(PubsubMessageWrapper message) {
if (!enabled) {
return;
}
Span subscriberSpan = message.getSubscriberSpan();
if (subscriberSpan != null) {
Span subscribeProcessSpan =
startChildSpan(message.getSubscriptionName() + " process", subscriberSpan);
subscribeProcessSpan.setAttribute(
SemanticAttributes.MESSAGING_SYSTEM, MESSAGING_SYSTEM_VALUE);
Span publisherSpan = message.getPublisherSpan();
if (publisherSpan != null) {
subscribeProcessSpan.addLink(publisherSpan.getSpanContext());
}
message.setSubscribeProcessSpan(subscribeProcessSpan);
}
}
void endSubscribeProcessSpan(PubsubMessageWrapper message, String action) {
if (!enabled) {
return;
}
message.endSubscribeProcessSpan(action);
}
/**
* Creates, starts, and returns spans for ModAck, Nack, and Ack RPC requests. Bi-directional links
* to parent subscribe span for sampled messages are added.
*/
Span startSubscribeRpcSpan(
SubscriptionName subscriptionName,
String rpcOperation,
List<PubsubMessageWrapper> messages,
int ackDeadline,
boolean isReceiptModack) {
if (!enabled) {
return null;
}
// Get the appropriate handler for the operation
RpcOperationHandler handler = getHandler(rpcOperation);
AttributesBuilder attributesBuilder =
createCommonSpanAttributesBuilder(
subscriptionName.getSubscription(),
subscriptionName.getProject(),
handler.getCodeFunction(),
rpcOperation)
.put(SemanticAttributes.MESSAGING_BATCH_MESSAGE_COUNT, messages.size());
// Ack deadline and receipt modack are specific to the modack operation
if (rpcOperation == "modack") {
handler.addAttributes(attributesBuilder, ackDeadline, isReceiptModack);
}
SpanBuilder rpcSpanBuilder =
tracer
.spanBuilder(subscriptionName.getSubscription() + " " + rpcOperation)
.setSpanKind(SpanKind.CLIENT)
.setAllAttributes(attributesBuilder.build());
Attributes linkAttributes =
Attributes.builder().put(SemanticAttributes.MESSAGING_OPERATION, rpcOperation).build();
for (PubsubMessageWrapper message : messages) {
if (message.getSubscriberSpan().getSpanContext().isSampled()) {
rpcSpanBuilder.addLink(message.getSubscriberSpan().getSpanContext(), linkAttributes);
}
}
Span rpcSpan = rpcSpanBuilder.startSpan();
for (PubsubMessageWrapper message : messages) {
if (rpcSpan.getSpanContext().isSampled()) {
message.getSubscriberSpan().addLink(rpcSpan.getSpanContext(), linkAttributes);
handler.addEvent(message);
}
}
return rpcSpan;
}
private RpcOperationHandler getHandler(String rpcOperation) {
switch (rpcOperation) {
case "ack":
return new AckHandler();
case "modack":
return new ModAckHandler();
case "nack":
return new NackHandler();
default:
throw new IllegalArgumentException("Unknown RPC operation: " + rpcOperation);
}
}
/** Ends the given subscribe RPC span if it exists. */
void endSubscribeRpcSpan(Span rpcSpan) {
if (!enabled) {
return;
}
if (rpcSpan != null) {
rpcSpan.end();
}
}
/**
* Sets an error status and records an exception when an exception is thrown when handling a
* subscribe-side RPC.
*/
void setSubscribeRpcSpanException(Span rpcSpan, boolean isModack, int ackDeadline, Throwable t) {
if (!enabled) {
return;
}
if (rpcSpan != null) {
String operation = !isModack ? "ack" : (ackDeadline == 0 ? "nack" : "modack");
rpcSpan.setStatus(StatusCode.ERROR, "Exception thrown on " + operation + " RPC.");
rpcSpan.recordException(t);
rpcSpan.end();
}
}
/** Adds the appropriate subscribe-side RPC end event. */
void addEndRpcEvent(
PubsubMessageWrapper message, boolean rpcSampled, boolean isModack, int ackDeadline) {
if (!enabled || !rpcSampled) {
return;
}
if (!isModack) {
message.addAckEndEvent();
} else if (ackDeadline == 0) {
message.addNackEndEvent();
} else {
message.addModAckEndEvent();
}
}
}