-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKafkaTests.cs
214 lines (178 loc) · 11.6 KB
/
KafkaTests.cs
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using IntegrationTests.Helpers;
using OpenTelemetry.Proto.Common.V1;
using OpenTelemetry.Proto.Trace.V1;
using Xunit.Abstractions;
namespace IntegrationTests;
[Collection(KafkaCollection.Name)]
public class KafkaTests : TestHelper
{
private const string MessagingPublishOperationAttributeValue = "publish";
private const string MessagingReceiveOperationAttributeValue = "receive";
private const string MessagingSystemAttributeName = "messaging.system";
private const string MessagingOperationAttributeName = "messaging.operation";
private const string MessagingDestinationAttributeName = "messaging.destination.name";
private const string MessagingClientIdAttributeName = "messaging.client_id";
private const string KafkaMessageSystemAttributeValue = "kafka";
private const string KafkaConsumerGroupAttributeName = "messaging.kafka.consumer.group";
private const string KafkaMessageKeyAttributeName = "messaging.kafka.message.key";
private const string KafkaMessageKeyAttributeValue = "testkey";
private const string KafkaDestinationPartitionAttributeName = "messaging.kafka.destination.partition";
private const string KafkaMessageOffsetAttributeName = "messaging.kafka.message.offset";
private const string KafkaMessageTombstoneAttributeName = "messaging.kafka.message.tombstone";
private const string KafkaInstrumentationScopeName = "OpenTelemetry.AutoInstrumentation.Kafka";
private const string KafkaProducerClientIdAttributeValue = "rdkafka#producer-1";
private const string KafkaConsumerClientIdAttributeValue = "rdkafka#consumer-2";
// https://github.com/confluentinc/confluent-kafka-dotnet/blob/07de95ed647af80a0db39ce6a8891a630423b952/src/Confluent.Kafka/Offset.cs#L36C44-L36C44
private const int InvalidOffset = -1001;
public KafkaTests(ITestOutputHelper testOutputHelper)
: base("Kafka", testOutputHelper)
{
}
[Theory]
[Trait("Category", "EndToEnd")]
[Trait("Containers", "Linux")]
[MemberData(nameof(LibraryVersion.GetPlatformVersions), nameof(LibraryVersion.Kafka), MemberType = typeof(LibraryVersion))]
public void SubmitsTraces(string packageVersion)
{
var topicName = $"test-topic-{packageVersion}";
using var collector = new MockSpansCollector(Output);
SetExporter(collector);
// Failed produce attempts made before topic is created.
collector.Expect(KafkaInstrumentationScopeName, span => span.Kind == Span.Types.SpanKind.Producer && ValidateResultProcessingProduceExceptionSpan(span, topicName), "Failed Produce attempt with delivery handler set.");
collector.Expect(KafkaInstrumentationScopeName, span => span.Kind == Span.Types.SpanKind.Producer && ValidateProduceExceptionSpan(span, topicName), "Failed Produce attempt without delivery handler set.");
collector.Expect(KafkaInstrumentationScopeName, span => span.Kind == Span.Types.SpanKind.Producer && ValidateResultProcessingProduceExceptionSpan(span, topicName), "Failed ProduceAsync attempt.");
if (packageVersion == string.Empty || Version.Parse(packageVersion) != new Version(1, 4, 0))
{
// Failed consume attempt.
collector.Expect(
KafkaInstrumentationScopeName,
span => span.Kind == Span.Types.SpanKind.Consumer && ValidateConsumeExceptionSpan(span, topicName),
"Failed Consume attempt.");
}
// Successful produce attempts after topic was created with admin client.
collector.Expect(KafkaInstrumentationScopeName, span => span.Kind == Span.Types.SpanKind.Producer && ValidateProducerSpan(span, topicName, 0), "Successful ProduceAsync attempt.");
collector.Expect(KafkaInstrumentationScopeName, span => span.Kind == Span.Types.SpanKind.Producer && ValidateProducerSpan(span, topicName, -1), "Successful Produce attempt without delivery handler set.");
collector.Expect(KafkaInstrumentationScopeName, span => span.Kind == Span.Types.SpanKind.Producer && ValidateProducerSpan(span, topicName, 0), "Successful Produce attempt with delivery handler set.");
// Successful produce attempt after topic was created for tombstones.
collector.Expect(KafkaInstrumentationScopeName, span => span.Kind == Span.Types.SpanKind.Producer && ValidateProducerSpan(span, topicName, -1, true), "Successful sync Publish attempt with a tombstone.");
// Successful consume attempts.
collector.Expect(KafkaInstrumentationScopeName, span => span.Kind == Span.Types.SpanKind.Consumer && ValidateConsumerSpan(span, topicName, 0), "First successful Consume attempt.");
collector.Expect(KafkaInstrumentationScopeName, span => span.Kind == Span.Types.SpanKind.Consumer && ValidateConsumerSpan(span, topicName, 1), "Second successful Consume attempt.");
collector.Expect(KafkaInstrumentationScopeName, span => span.Kind == Span.Types.SpanKind.Consumer && ValidateConsumerSpan(span, topicName, 2), "Third successful Consume attempt.");
collector.ExpectCollected(collection => ValidatePropagation(collection, topicName));
EnableBytecodeInstrumentation();
RunTestApplication(new TestSettings
{
PackageVersion = packageVersion,
Arguments = $"--topic-name {topicName}"
});
collector.AssertExpectations();
}
[Theory]
[Trait("Category", "EndToEnd")]
[Trait("Containers", "Linux")]
[MemberData(nameof(LibraryVersion.GetPlatformVersions), nameof(LibraryVersion.Kafka), MemberType = typeof(LibraryVersion))]
// ReSharper disable once InconsistentNaming
public void NoSpansForPartitionEOF(string packageVersion)
{
var topicName = $"test-topic2-{packageVersion}";
using var collector = new MockSpansCollector(Output);
SetExporter(collector);
EnableBytecodeInstrumentation();
RunTestApplication(new TestSettings
{
PackageVersion = packageVersion,
Arguments = $"--topic-name {topicName} --consume-only"
});
collector.AssertEmpty(TimeSpan.FromSeconds(10));
}
private static string GetConsumerGroupIdAttributeValue(string topicName)
{
return $"test-consumer-group-{topicName}";
}
private static bool ValidateConsumeExceptionSpan(Span span, string topicName)
{
return ValidateConsumerSpan(span, topicName, InvalidOffset, null) &&
span.Status.Code == Status.Types.StatusCode.Error;
}
private static bool ValidateConsumerSpan(Span span, string topicName, int messageOffset, string? expectedMessageKey = KafkaMessageKeyAttributeValue)
{
var kafkaMessageOffset = span.Attributes.SingleOrDefault(kv => kv.Key == KafkaMessageOffsetAttributeName)?.Value.IntValue;
var consumerGroupId = span.Attributes.Single(kv => kv.Key == KafkaConsumerGroupAttributeName).Value.StringValue;
return ValidateCommonAttributes(span.Attributes, topicName, KafkaConsumerClientIdAttributeValue, MessagingReceiveOperationAttributeValue, 0, expectedMessageKey) &&
kafkaMessageOffset == messageOffset &&
consumerGroupId == GetConsumerGroupIdAttributeValue(topicName);
}
private static bool ValidateBasicProduceExceptionSpan(Span span, string topicName)
{
return ValidateCommonAttributes(span.Attributes, topicName, KafkaProducerClientIdAttributeValue, MessagingPublishOperationAttributeValue, -1, KafkaMessageKeyAttributeValue) &&
span.Status.Code == Status.Types.StatusCode.Error;
}
private static bool ValidateProduceExceptionSpan(Span span, string topicName)
{
return ValidateBasicProduceExceptionSpan(span, topicName) &&
span.Attributes.Count(kv => kv.Key == KafkaMessageOffsetAttributeName) == 0;
}
private static bool ValidateResultProcessingProduceExceptionSpan(Span span, string topicName)
{
// DeliveryResult processing results in offset being set.
var offset = span.Attributes.SingleOrDefault(kv => kv.Key == KafkaMessageOffsetAttributeName)?.Value.IntValue;
return ValidateBasicProduceExceptionSpan(span, topicName) &&
offset == InvalidOffset;
}
private static bool ValidateProducerSpan(Span span, string topicName, int partition, bool tombstoneExpected = false)
{
var isTombstone = span.Attributes.Single(kv => kv.Key == KafkaMessageTombstoneAttributeName).Value.BoolValue;
return ValidateCommonAttributes(span.Attributes, topicName, KafkaProducerClientIdAttributeValue, MessagingPublishOperationAttributeValue, partition, KafkaMessageKeyAttributeValue) &&
isTombstone == tombstoneExpected &&
span.Status is null;
}
private static bool ValidateCommonAttributes(IReadOnlyCollection<KeyValue> attributes, string topicName, string clientId, string operationName, int partition, string? expectedMessageKey)
{
var messagingDestinationName = attributes.SingleOrDefault(kv => kv.Key == MessagingDestinationAttributeName)?.Value.StringValue;
var kafkaMessageKey = attributes.SingleOrDefault(kv => kv.Key == KafkaMessageKeyAttributeName)?.Value.StringValue;
var kafkaPartition = attributes.SingleOrDefault(kv => kv.Key == KafkaDestinationPartitionAttributeName)?.Value.IntValue;
return ValidateBasicSpanAttributes(attributes, clientId, operationName) &&
messagingDestinationName == topicName &&
kafkaMessageKey == expectedMessageKey &&
kafkaPartition == partition;
}
private static bool ValidateBasicSpanAttributes(IReadOnlyCollection<KeyValue> attributes, string clientId, string operationName)
{
var messagingSystem = attributes.Single(kv => kv.Key == MessagingSystemAttributeName).Value.StringValue;
var messagingOperation = attributes.Single(kv => kv.Key == MessagingOperationAttributeName).Value.StringValue;
var messagingClientId = attributes.Single(kv => kv.Key == MessagingClientIdAttributeName).Value.StringValue;
return messagingSystem == KafkaMessageSystemAttributeValue &&
messagingOperation == operationName &&
messagingClientId == clientId;
}
private static bool ValidatePropagation(ICollection<MockSpansCollector.Collected> collectedSpans, string topicName)
{
var expectedReceiveOperationName = $"{topicName} {MessagingReceiveOperationAttributeValue}";
var expectedPublishOperationName = $"{topicName} {MessagingPublishOperationAttributeValue}";
var producerSpans = collectedSpans
.Where(span =>
span.Span.Name == expectedPublishOperationName &&
!span.Span.Attributes.Single(attr => attr.Key == KafkaMessageTombstoneAttributeName).Value.BoolValue &&
span.Span.Status is null);
return producerSpans.Select(
producerSpan =>
GetMatchingConsumerSpan(collectedSpans, producerSpan.Span, expectedReceiveOperationName))
.All(consumerSpan => consumerSpan is not null);
}
private static MockSpansCollector.Collected? GetMatchingConsumerSpan(ICollection<MockSpansCollector.Collected> collectedSpans, Span producerSpan, string expectedReceiveOperationName)
{
return collectedSpans
.SingleOrDefault(span =>
{
var parentLinksCount = span.Span.Links.Count(
link =>
link.TraceId == producerSpan.TraceId &&
link.SpanId == producerSpan.SpanId);
return span.Span.Name == expectedReceiveOperationName &&
parentLinksCount == 1;
});
}
}