Skip to content

Commit

Permalink
wp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ysolomchenko committed Nov 12, 2024
1 parent 239877d commit 0fb4d99
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
32 changes: 18 additions & 14 deletions test/IntegrationTests/KafkaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ namespace IntegrationTests;
[Collection(KafkaCollection.Name)]
public class KafkaTests : TestHelper
{
private const string MessagingPublishOperationAttributeValue = "publish";
private const string MessagingPublishOperationAttributeValue = "send";
private const string MessagingReceiveOperationAttributeValue = "receive";
private const string MessagingPollOperationAttributeValue = "poll";
private const string MessagingSystemAttributeName = "messaging.system";
private const string MessagingOperationAttributeName = "messaging.operation";
private const string MessagingOperationNameAttributeName = "messaging.operation.name";
private const string MessagingOperationTypeAttributeName = "messaging.operation.type";
private const string MessagingDestinationAttributeName = "messaging.destination.name";
private const string MessagingClientIdAttributeName = "messaging.client_id";
private const string MessagingConsumerGroupNameAttributeName = "messaging.consumer.group.name";

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";
Expand Down Expand Up @@ -124,15 +126,15 @@ private static bool ValidateConsumeExceptionSpan(Span span, string topicName)
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) &&
var consumerGroupId = span.Attributes.Single(kv => kv.Key == MessagingConsumerGroupNameAttributeName).Value.StringValue;
return ValidateCommonAttributes(span.Attributes, topicName, KafkaConsumerClientIdAttributeValue, MessagingReceiveOperationAttributeValue, MessagingPollOperationAttributeValue, 0, expectedMessageKey) &&
kafkaMessageOffset == messageOffset &&
consumerGroupId == GetConsumerGroupIdAttributeValue(topicName);
}

private static bool ValidateBasicProduceExceptionSpan(Span span, string topicName)
{
return ValidateCommonAttributes(span.Attributes, topicName, KafkaProducerClientIdAttributeValue, MessagingPublishOperationAttributeValue, -1, KafkaMessageKeyAttributeValue) &&
return ValidateCommonAttributes(span.Attributes, topicName, KafkaProducerClientIdAttributeValue, MessagingPublishOperationAttributeValue, MessagingPublishOperationAttributeValue, -1, KafkaMessageKeyAttributeValue) &&
span.Status.Code == Status.Types.StatusCode.Error;
}

Expand All @@ -154,38 +156,40 @@ private static bool ValidateProducerSpan(Span span, string topicName, int partit
{
var isTombstone = span.Attributes.Single(kv => kv.Key == KafkaMessageTombstoneAttributeName).Value.BoolValue;

return ValidateCommonAttributes(span.Attributes, topicName, KafkaProducerClientIdAttributeValue, MessagingPublishOperationAttributeValue, partition, KafkaMessageKeyAttributeValue) &&
return ValidateCommonAttributes(span.Attributes, topicName, KafkaProducerClientIdAttributeValue, MessagingPublishOperationAttributeValue, 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)
private static bool ValidateCommonAttributes(IReadOnlyCollection<KeyValue> attributes, string topicName, string clientId, string operationName, string operationType, 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) &&
return ValidateBasicSpanAttributes(attributes, clientId, operationName, operationType) &&
messagingDestinationName == topicName &&
kafkaMessageKey == expectedMessageKey &&
kafkaPartition == partition;
}

private static bool ValidateBasicSpanAttributes(IReadOnlyCollection<KeyValue> attributes, string clientId, string operationName)
private static bool ValidateBasicSpanAttributes(IReadOnlyCollection<KeyValue> attributes, string clientId, string operationName, string operationType)
{
var messagingSystem = attributes.Single(kv => kv.Key == MessagingSystemAttributeName).Value.StringValue;
var messagingOperation = attributes.Single(kv => kv.Key == MessagingOperationAttributeName).Value.StringValue;
var messagingOperationName = attributes.Single(kv => kv.Key == MessagingOperationNameAttributeName).Value.StringValue;
var messagingOperationType = attributes.Single(kv => kv.Key == MessagingOperationTypeAttributeName).Value.StringValue;
var messagingClientId = attributes.Single(kv => kv.Key == MessagingClientIdAttributeName).Value.StringValue;

return messagingSystem == KafkaMessageSystemAttributeValue &&
messagingOperation == operationName &&
messagingOperationName == operationName &&
messagingOperationType == operationType &&
messagingClientId == clientId;
}

private static bool ValidatePropagation(ICollection<MockSpansCollector.Collected> collectedSpans, string topicName)
{
var expectedReceiveOperationName = $"{topicName} {MessagingReceiveOperationAttributeValue}";
var expectedPublishOperationName = $"{topicName} {MessagingPublishOperationAttributeValue}";
var expectedReceiveOperationName = $"{MessagingPollOperationAttributeValue} {topicName}";
var expectedPublishOperationName = $"{MessagingPublishOperationAttributeValue} {topicName}";
var producerSpans = collectedSpans
.Where(span =>
span.Span.Name == expectedPublishOperationName &&
Expand Down
23 changes: 13 additions & 10 deletions test/IntegrationTests/RabbitMqTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class RabbitMqTests : TestHelper

// Required messaging attributes set by the instrumentation
private const string MessagingSystemAttributeName = "messaging.system";
private const string MessagingOperationAttributeName = "messaging.operation";
private const string MessagingOperationNameAttributeName = "messaging.operation.name";
private const string MessagingOperationTypeAttributeName = "messaging.operation.type";
private const string MessagingDestinationAttributeName = "messaging.destination.name";

// Required RabbitMQ attributes set by the instrumentation
Expand Down Expand Up @@ -58,9 +59,9 @@ public void SubmitsTraces(string packageVersion)
collector.Expect("OpenTelemetry.AutoInstrumentation.RabbitMq", span => ValidateProducerSpan(span));
collector.Expect("OpenTelemetry.AutoInstrumentation.RabbitMq", span => ValidateProducerSpan(span));
collector.Expect("OpenTelemetry.AutoInstrumentation.RabbitMq", span => ValidateProducerSpan(span));
collector.Expect("OpenTelemetry.AutoInstrumentation.RabbitMq", span => ValidateConsumerSpan(span, "receive"));
collector.Expect("OpenTelemetry.AutoInstrumentation.RabbitMq", span => ValidateConsumerSpan(span, "deliver"));
collector.Expect("OpenTelemetry.AutoInstrumentation.RabbitMq", span => ValidateConsumerSpan(span, "deliver"));
collector.Expect("OpenTelemetry.AutoInstrumentation.RabbitMq", span => ValidateConsumerSpan(span, "receive", "receive"));
collector.Expect("OpenTelemetry.AutoInstrumentation.RabbitMq", span => ValidateConsumerSpan(span, "process", "consume"));
collector.Expect("OpenTelemetry.AutoInstrumentation.RabbitMq", span => ValidateConsumerSpan(span, "process", "consume"));

collector.ExpectCollected(collected => ValidatePropagation(collected));

Expand Down Expand Up @@ -91,34 +92,36 @@ bool VerifySingleMatchingConsumerSpan(MockSpansCollector.Collected producerSpan)
}
}

private static bool ValidateBasicSpanAttributes(IReadOnlyCollection<KeyValue> attributes, string operationName)
private static bool ValidateBasicSpanAttributes(IReadOnlyCollection<KeyValue> attributes, string operationName, string operationType)
{
var messagingSystem = attributes.Single(kv => kv.Key == MessagingSystemAttributeName).Value.StringValue;
var messagingOperation = attributes.Single(kv => kv.Key == MessagingOperationAttributeName).Value.StringValue;
var messagingOperationName = attributes.Single(kv => kv.Key == MessagingOperationNameAttributeName).Value.StringValue;
var messagingOperationType = attributes.Single(kv => kv.Key == MessagingOperationTypeAttributeName).Value.StringValue;
var destinationName = attributes.Single(kv => kv.Key == MessagingDestinationAttributeName).Value.StringValue;
var routingKey = attributes.Single(kv => kv.Key == RabbitMqRoutingKeyAttributeName).Value.StringValue;
var bodySize = attributes.Single(kv => kv.Key == MessagingBodySizeAttributeName).Value.IntValue;

return messagingSystem == "rabbitmq" &&
messagingOperation == operationName &&
messagingOperationName == operationName &&
messagingOperationType == operationType &&
destinationName == "amq.default" &&
routingKey == "hello" &&
bodySize == 13;
}

private bool ValidateConsumerSpan(Span span, string operationName)
private bool ValidateConsumerSpan(Span span, string operationName, string operationType)
{
var deliveryTag = span.Attributes.SingleOrDefault(kv => kv.Key == RabbitMqDeliveryTagAttributeName)?.Value.StringValue;
return span.Kind == Span.Types.SpanKind.Consumer &&
span.Links.Count == 1 &&
ValidateBasicSpanAttributes(span.Attributes, operationName) &&
ValidateBasicSpanAttributes(span.Attributes, operationName, operationType) &&
(operationName != "receive" || ValidateNetworkAttributes(span.Attributes)) &&
!string.IsNullOrEmpty(deliveryTag);
}

private bool ValidateProducerSpan(Span span)
{
return span.Kind == Span.Types.SpanKind.Producer && ValidateBasicSpanAttributes(span.Attributes, "publish") && ValidateNetworkAttributes(span.Attributes);
return span.Kind == Span.Types.SpanKind.Producer && ValidateBasicSpanAttributes(span.Attributes, "send", "send") && ValidateNetworkAttributes(span.Attributes);
}

private bool ValidateNetworkAttributes(IReadOnlyCollection<KeyValue> spanAttributes)
Expand Down

0 comments on commit 0fb4d99

Please sign in to comment.