-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathKafkaHelper.cs
More file actions
535 lines (463 loc) · 23.3 KB
/
KafkaHelper.cs
File metadata and controls
535 lines (463 loc) · 23.3 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// <copyright file="KafkaHelper.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
#nullable enable
using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Text;
using System.Threading;
using Datadog.Trace.Configuration.Schema;
using Datadog.Trace.DataStreamsMonitoring;
using Datadog.Trace.DataStreamsMonitoring.TransactionTracking;
using Datadog.Trace.DataStreamsMonitoring.Utils;
using Datadog.Trace.DuckTyping;
using Datadog.Trace.Logging;
using Datadog.Trace.Propagators;
using Datadog.Trace.Tagging;
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Kafka
{
internal static class KafkaHelper
{
internal const string GroupIdKey = "group.id";
internal const string BootstrapServersKey = "bootstrap.servers";
internal const string EnableDeliveryReportsField = "dotnet.producer.enable.delivery.reports";
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(KafkaHelper));
private static readonly string[] DefaultProduceEdgeTags = ["direction:out", "type:kafka"];
private static readonly ConcurrentDictionary<string, string?> ClusterIdCache = new();
private static bool _headersInjectionEnabled = true;
internal static Scope? CreateProducerScope(
Tracer tracer,
object producer,
ITopicPartition topicPartition,
bool isTombstone,
bool finishOnClose)
{
Scope? scope = null;
try
{
var settings = tracer.CurrentTraceSettings;
if (!settings.Settings.IsIntegrationEnabled(KafkaConstants.IntegrationId))
{
// integration disabled, don't create a scope/span, skip this trace
return null;
}
var parent = tracer.ActiveScope?.Span;
string operationName = settings.Schema.Messaging.GetOutboundOperationName(MessagingSchema.OperationType.Kafka);
if (parent is not null &&
parent.OperationName == operationName &&
parent.GetTag(Tags.InstrumentationName) != null)
{
return null;
}
var (serviceName, serviceNameSource) = settings.Schema.Messaging.GetServiceNameMetadata(MessagingSchema.ServiceType.Kafka);
KafkaTags tags = settings.Schema.Messaging.CreateKafkaTags(SpanKinds.Producer);
scope = tracer.StartActiveInternal(
operationName,
tags: tags,
serviceName: serviceName,
serviceNameSource: serviceNameSource,
finishOnClose: finishOnClose);
string resourceName = $"Produce Topic {(string.IsNullOrEmpty(topicPartition?.Topic) ? "kafka" : topicPartition?.Topic)}";
var span = scope.Span;
span.Type = SpanTypes.Queue;
span.ResourceName = resourceName;
if (topicPartition?.Partition is not null && !topicPartition.Partition.IsSpecial)
{
tags.Partition = (topicPartition?.Partition).ToString();
}
if (ProducerCache.TryGetProducer(producer, out var bootstrapServers, out var clusterId))
{
tags.BootstrapServers = bootstrapServers;
if (!string.IsNullOrEmpty(clusterId))
{
tags.ClusterId = clusterId;
}
}
if (isTombstone)
{
tags.Tombstone = "true";
}
if (topicPartition is not null && !string.IsNullOrEmpty(topicPartition.Topic))
{
tags.Topic = topicPartition.Topic;
}
// Producer spans should always be measured
span.SetMetric(Trace.Tags.Measured, 1.0);
tracer.CurrentTraceSettings.Schema.RemapPeerService(tags);
tags.SetAnalyticsSampleRate(KafkaConstants.IntegrationId, settings.Settings, enabledWithGlobalSetting: false);
tracer.TracerManager.Telemetry.IntegrationGeneratedSpan(KafkaConstants.IntegrationId);
}
catch (Exception ex)
{
Log.Error(ex, "Error creating or populating scope.");
}
return scope;
}
private static long GetMessageSize<T>(T message)
where T : IMessage
{
if (((IDuckType)message).Instance is null)
{
return 0;
}
var size = MessageSizeHelper.TryGetSize(message.Key);
size += MessageSizeHelper.TryGetSize(message.Value);
if (message.Headers == null)
{
return size;
}
for (var i = 0; i < message.Headers.Count; i++)
{
var header = message.Headers[i];
size += Encoding.UTF8.GetByteCount(header.Key);
var value = header.GetValueBytes();
if (value != null)
{
size += value.Length;
}
}
return size;
}
internal static Scope? CreateConsumerScope(
Tracer tracer,
DataStreamsManager dataStreamsManager,
object consumer,
string topic,
Partition? partition,
Offset? offset,
IMessage message)
{
Scope? scope = null;
try
{
if (!tracer.CurrentTraceSettings.Settings.IsIntegrationEnabled(KafkaConstants.IntegrationId))
{
// integration disabled, don't create a scope/span, skip this trace
return null;
}
var parent = tracer.ActiveScope?.Span;
string operationName = tracer.CurrentTraceSettings.Schema.Messaging.GetInboundOperationName(MessagingSchema.OperationType.Kafka);
if (parent is not null &&
parent.OperationName == operationName &&
parent.GetTag(Tags.InstrumentationName) != null)
{
return null;
}
PropagationContext extractedContext = default;
PathwayContext? pathwayContext = null;
// Try to extract propagated context from headers
if (message?.Headers is not null)
{
var headers = new KafkaHeadersCollectionAdapter(message.Headers);
try
{
extractedContext = tracer.TracerManager.SpanContextPropagator.Extract(headers).MergeBaggageInto(Baggage.Current);
}
catch (Exception ex)
{
Log.Error(ex, "Error extracting propagated headers from Kafka message");
}
if (dataStreamsManager.IsEnabled)
{
try
{
pathwayContext = dataStreamsManager.ExtractPathwayContext(headers);
}
catch (Exception ex)
{
Log.Error(ex, "Error extracting PathwayContext from Kafka message");
}
}
}
var (serviceName, serviceNameSource) = tracer.CurrentTraceSettings.Schema.Messaging.GetServiceNameMetadata(MessagingSchema.ServiceType.Kafka);
var tags = tracer.CurrentTraceSettings.Schema.Messaging.CreateKafkaTags(SpanKinds.Consumer);
scope = tracer.StartActiveInternal(operationName, parent: extractedContext.SpanContext, tags: tags, serviceName: serviceName, serviceNameSource: serviceNameSource);
tracer.TracerManager.Telemetry.IntegrationGeneratedSpan(KafkaConstants.IntegrationId);
string resourceName = $"Consume Topic {(string.IsNullOrEmpty(topic) ? "kafka" : topic)}";
var span = scope.Span;
span.Type = SpanTypes.Queue;
span.ResourceName = resourceName;
if (partition is not null)
{
tags.Partition = partition.ToString();
}
if (offset is not null)
{
tags.Offset = offset.ToString();
}
if (ConsumerCache.TryGetConsumerGroup(consumer, out var groupId, out var bootstrapServers, out var consumerClusterId))
{
tags.ConsumerGroup = groupId;
tags.BootstrapServers = bootstrapServers;
if (!StringUtil.IsNullOrEmpty(consumerClusterId))
{
tags.ClusterId = consumerClusterId;
}
}
if (message?.Instance is not null && message.Timestamp.Type != 0)
{
var consumeTime = span.StartTime.UtcDateTime;
var produceTime = message.Timestamp.UtcDateTime;
tags.MessageQueueTimeMs = Math.Max(0, (consumeTime - produceTime).TotalMilliseconds);
}
if (message?.Instance is not null && message.Value is null)
{
tags.Tombstone = "true";
}
if (!string.IsNullOrEmpty(topic))
{
tags.Topic = topic;
}
// Consumer spans should always be measured
span.SetTag(Tags.Measured, "1");
tags.SetAnalyticsSampleRate(KafkaConstants.IntegrationId, tracer.CurrentTraceSettings.Settings, enabledWithGlobalSetting: false);
if (dataStreamsManager.IsEnabled)
{
// TODO: we could pool these arrays to reduce allocations
// NOTE: the tags must be sorted in alphabetical order
string[] edgeTags;
if (!StringUtil.IsNullOrEmpty(consumerClusterId))
{
edgeTags = StringUtil.IsNullOrEmpty(topic)
? new[] { "direction:in", $"group:{groupId}", $"kafka_cluster_id:{consumerClusterId}", "type:kafka" }
: new[] { "direction:in", $"group:{groupId}", $"kafka_cluster_id:{consumerClusterId}", $"topic:{topic}", "type:kafka" };
}
else
{
edgeTags = StringUtil.IsNullOrEmpty(topic)
? new[] { "direction:in", $"group:{groupId}", "type:kafka" }
: new[] { "direction:in", $"group:{groupId}", $"topic:{topic}", "type:kafka" };
}
span.SetDataStreamsCheckpoint(
dataStreamsManager,
CheckpointKind.Consume,
edgeTags,
message?.Instance is null || dataStreamsManager.IsInDefaultState ? 0 : GetMessageSize(message),
tags.MessageQueueTimeMs == null ? 0 : (long)tags.MessageQueueTimeMs,
pathwayContext);
message?.Headers?.Remove(DataStreamsPropagationHeaders.TemporaryBase64PathwayContext); // remove eventual junk
if (!tracer.CurrentTraceSettings.Settings.KafkaCreateConsumerScopeEnabled && message?.Headers is not null && span.Context.PathwayContext != null)
{
// write the _new_ pathway (the "consume" checkpoint that we just set above) to the headers as a way to pass its value to an eventual
// call to SpanContextExtractor.Extract by a user who'd like to re-pair pathways after a batch consume.
// Note that this header only exists on the consume side, and Kafka never sees it.
var base64PathwayContext = Convert.ToBase64String(BitConverter.GetBytes(span.Context.PathwayContext.Value.Hash.Value));
message.Headers.Add(DataStreamsPropagationHeaders.TemporaryBase64PathwayContext, Encoding.UTF8.GetBytes(base64PathwayContext));
}
ApplyDataStreamsExtractors(dataStreamsManager, DataStreamsTransactionExtractor.Type.KafkaConsumeHeaders, message);
}
}
catch (Exception ex)
{
Log.Error(ex, "Error creating or populating scope.");
}
return scope;
}
internal static void CloseConsumerScope(Tracer tracer)
{
try
{
var settings = tracer.CurrentTraceSettings.Settings;
if (!settings.IsIntegrationEnabled(KafkaConstants.IntegrationId)
|| !settings.KafkaCreateConsumerScopeEnabled)
{
// integration disabled, skip this trace
return;
}
var activeScope = tracer.InternalActiveScope;
var currentSpan = activeScope?.Span;
if (currentSpan?.OperationName != tracer.CurrentTraceSettings.Schema.Messaging.GetInboundOperationName(MessagingSchema.OperationType.Kafka))
{
// Not currently in a consumer operation
return;
}
// TODO: record end-to-end time?
activeScope!.Dispose();
}
catch (Exception ex)
{
Log.Error(ex, "Error closing Kafka consumer scope");
}
}
internal static void ApplyDataStreamsExtractors<TMessage>(
DataStreamsManager dataStreamsManager,
DataStreamsTransactionExtractor.Type extractorType,
TMessage? message)
where TMessage : IMessage
{
if (message?.Instance == null)
{
return;
}
var extractors = dataStreamsManager.GetExtractorsByType(extractorType);
if (extractors != null && message.Headers != null)
{
foreach (var extractor in extractors)
{
if (message.Headers.TryGetLastBytes(extractor.Value, out var transactionId))
{
dataStreamsManager.TrackTransaction(Encoding.UTF8.GetString(transactionId), extractor.Name);
}
}
}
}
/// <summary>
/// Try to inject the prop
/// </summary>
/// <param name="span">Current span</param>
/// <param name="dataStreamsManager">The global data streams manager</param>
/// <param name="topic">Topic name</param>
/// <param name="message">The duck-typed Kafka Message object</param>
/// <param name="producer">The Kafka producer instance, used to look up cluster_id from the cache</param>
/// <typeparam name="TTopicPartitionMarker">The TopicPartition type (used optimisation purposes)</typeparam>
/// <typeparam name="TMessage">The type of the duck-type proxy</typeparam>
internal static void TryInjectHeaders<TTopicPartitionMarker, TMessage>(
Span span,
DataStreamsManager dataStreamsManager,
string topic,
TMessage message,
object producer)
where TMessage : IMessage
{
if (!_headersInjectionEnabled || message.Instance is null)
{
return;
}
try
{
if (message.Headers is null)
{
message.Headers = CachedMessageHeadersHelper<TTopicPartitionMarker>.CreateHeaders();
}
var adapter = new KafkaHeadersCollectionAdapter(message.Headers);
var context = new PropagationContext(span.Context, Baggage.Current);
Tracer.Instance.TracerManager.SpanContextPropagator.Inject(context, adapter);
if (dataStreamsManager.IsEnabled)
{
ProducerCache.TryGetProducer(producer, out _, out var producerClusterId);
string[] edgeTags;
if (!StringUtil.IsNullOrEmpty(producerClusterId))
{
edgeTags = StringUtil.IsNullOrEmpty(topic)
? ["direction:out", $"kafka_cluster_id:{producerClusterId}", "type:kafka"]
: ["direction:out", $"kafka_cluster_id:{producerClusterId}", $"topic:{topic}", "type:kafka"];
}
else
{
edgeTags = StringUtil.IsNullOrEmpty(topic)
? DefaultProduceEdgeTags
: ["direction:out", $"topic:{topic}", "type:kafka"];
}
var msgSize = dataStreamsManager.IsInDefaultState ? 0 : GetMessageSize(message);
// produce is always the start of the edge, so defaultEdgeStartMs is always 0
span.SetDataStreamsCheckpoint(dataStreamsManager, CheckpointKind.Produce, edgeTags, msgSize, 0);
// DSM context should NOT be injected state if the message value size is <= DSM header size (~34 bytes).
// This is needed to avoid situations when DSM context injection causes a significant
// percentage increase in overall message size, leading to capacity issues on the kafka server.
if (dataStreamsManager.IsInDefaultState && MessageSizeHelper.TryGetSize(message.Value) <= 34)
{
return;
}
dataStreamsManager.InjectPathwayContext(span.Context.PathwayContext, adapter);
ApplyDataStreamsExtractors(dataStreamsManager, DataStreamsTransactionExtractor.Type.KafkaProduceHeaders, message);
}
}
catch (Exception ex)
{
// don't keep trying if we run into problems
_headersInjectionEnabled = false;
Log.Warning(ex, "There was a problem injecting headers into the Kafka record. Disabling Headers injection");
}
}
internal static string? GetClusterId(string? bootstrapServers, object clientInstance)
{
if (StringUtil.IsNullOrEmpty(bootstrapServers))
{
return null;
}
if (ClusterIdCache.TryGetValue(bootstrapServers, out var cached))
{
return cached;
}
try
{
var kafkaAssembly = clientInstance.GetType().Assembly;
// DescribeClusterAsync and DescribeClusterOptions were added in Confluent.Kafka 2.3.0
var describeClusterOptionsType = kafkaAssembly.GetType("Confluent.Kafka.Admin.DescribeClusterOptions");
if (describeClusterOptionsType is null)
{
Log.Debug("Confluent.Kafka.Admin.DescribeClusterOptions not found; cluster_id tag requires Confluent.Kafka >= 2.3.0");
ClusterIdCache.TryAdd(bootstrapServers, string.Empty);
return string.Empty;
}
var builderType = kafkaAssembly.GetType("Confluent.Kafka.DependentAdminClientBuilder");
if (builderType is null)
{
Log.Error("Failed to find Confluent.Kafka.DependentAdminClientBuilder in the Confluent.Kafka assembly");
ClusterIdCache.TryAdd(bootstrapServers, string.Empty);
return string.Empty;
}
var clientHandle = clientInstance.DuckCast<IClientHandle>();
var handle = clientHandle.Handle;
if (handle is null)
{
Log.Error("Kafka client handle is null; unable to extract cluster_id");
return null;
}
var builder = Activator.CreateInstance(builderType, handle)!;
using var adminClient = builder.DuckCast<IAdminClientBuilder>().Build();
var clusterId = DescribeClusterWithTimeout(adminClient, describeClusterOptionsType);
ClusterIdCache.TryAdd(bootstrapServers, clusterId);
return clusterId;
}
catch (Exception ex)
{
Log.Warning(ex, "Error extracting cluster_id from Kafka metadata");
return null;
}
}
private static string? DescribeClusterWithTimeout(IAdminClient adminClient, Type describeClusterOptionsType)
{
var options = Activator.CreateInstance(describeClusterOptionsType)!;
options.DuckCast<IDescribeClusterOptions>().RequestTimeout = TimeSpan.FromSeconds(2);
var duckTask = adminClient.DescribeClusterAsync(options);
var describeResult = SafeGetResult<IDuckTypeTask<IDescribeClusterResult>, IDescribeClusterResult>(duckTask);
return describeResult?.ClusterId;
static TResult? SafeGetResult<TTask, TResult>(TTask task)
where TTask : IDuckTypeTask<TResult>
where TResult : IDescribeClusterResult
{
if (task.IsCompletedSuccessfully)
{
return task.Result;
}
var originalContext = SynchronizationContext.Current;
try
{
// Set the synchronization context to null to avoid deadlocks.
SynchronizationContext.SetSynchronizationContext(null);
// Wait synchronously for the task to complete.
return task.GetAwaiter().GetResult();
}
finally
{
// Restore the original synchronization context.
SynchronizationContext.SetSynchronizationContext(originalContext);
}
}
}
internal static void DisableHeadersIfUnsupportedBroker(Exception exception)
{
if (_headersInjectionEnabled && exception is not null && exception.Message.IndexOf("Unknown broker error", StringComparison.OrdinalIgnoreCase) != -1)
{
// If we get this exception, it likely means that the message format being used is pre-0.11
// We do not retry the failed message, we think this will have unnecessary complexity due to the likely rarity of this error
// We do not selectively disable headers injection, we disable it globally due to the likely rarity of this error
_headersInjectionEnabled = false;
Log.Error(exception, "Kafka Broker responded with UNKNOWN_SERVER_ERROR (-1). Please look at broker logs for more information. Tracer message header injection for Kafka is disabled.");
}
}
}
}