-
Notifications
You must be signed in to change notification settings - Fork 14
[dotnet] Implement .NET system test for SNS #4897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fc9dc70
Implement .NET system test for SNS
robcarlan-datadog 526b32f
.NET uses topic without arn for SNS
robcarlan-datadog 6ea068a
fix linting
robcarlan-datadog da53447
Debug SQS by logging message body & attributes
robcarlan-datadog bfe70a5
Merge branch 'main' into rob.carlan/dsm-sns-dotnet-test
robcarlan-datadog c7f688d
Merge branch 'main' into rob.carlan/dsm-sns-dotnet-test
robcarlan-datadog 7e15f59
empty commit
robcarlan-datadog d48690a
Merge branch 'main' into rob.carlan/dsm-sns-dotnet-test
robcarlan-datadog 8786d50
Stop consuming SQS messages after first found
robcarlan-datadog 150aa38
simplify logic even more
robcarlan-datadog 577f811
Ensure message body matches
robcarlan-datadog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,15 @@ | |
using Microsoft.AspNetCore.Http; | ||
using System.Collections.Generic; | ||
using System; | ||
using System.IO; | ||
using System.Net; | ||
using System.Globalization; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Amazon.SQS; | ||
using Amazon.SQS.Model; | ||
using Amazon.SimpleNotificationService; | ||
using Amazon.SimpleNotificationService.Model; | ||
using RabbitMQ.Client; | ||
|
||
namespace weblog | ||
|
@@ -25,6 +28,7 @@ public void Register(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routeBui | |
string routing_key = context.Request.Query["routing_key"]!; | ||
string group = context.Request.Query["group"]!; | ||
string message = context.Request.Query["message"]!; | ||
string topic = context.Request.Query["topic"]!; | ||
|
||
Console.WriteLine("Hello World! Received dsm call with integration " + integration); | ||
if ("kafka".Equals(integration)) { | ||
|
@@ -57,6 +61,13 @@ public void Register(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routeBui | |
Console.WriteLine($"[SQS] Begin consuming DSM message: {message}"); | ||
await Task.Run(() => SqsConsumer.DoWork(queue, message)); | ||
await context.Response.WriteAsync("ok"); | ||
} else if ("sns".Equals(integration)) | ||
{ | ||
Console.WriteLine($"[SNS] Begin producing DSM message: {message}"); | ||
await Task.Run(() => SnsProducer.DoWork(queue, topic, message)); | ||
Console.WriteLine($"[SNS] Begin consuming DSM message: {message}"); | ||
await Task.Run(() => SnsConsumer.DoWork(queue, message)); | ||
await context.Response.WriteAsync("ok"); | ||
} else { | ||
await context.Response.WriteAsync("unknown integration: " + integration); | ||
} | ||
|
@@ -308,4 +319,183 @@ public static async Task DoWork(string queue, string message) | |
} | ||
} | ||
} | ||
|
||
class SnsProducer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This practically follows the Java implementation as close as possible |
||
{ | ||
public static async Task DoWork(string queue, string topic, string message) | ||
{ | ||
string? awsUrl = Environment.GetEnvironmentVariable("SYSTEM_TESTS_AWS_URL"); | ||
|
||
IAmazonSimpleNotificationService snsClient; | ||
IAmazonSQS sqsClient; | ||
if (!string.IsNullOrEmpty(awsUrl)) | ||
{ | ||
// If SYSTEM_TESTS_AWS_URL is set, use it for ServiceURL | ||
snsClient = new AmazonSimpleNotificationServiceClient(new AmazonSimpleNotificationServiceConfig { ServiceURL = awsUrl }); | ||
sqsClient = new AmazonSQSClient(new AmazonSQSConfig { ServiceURL = awsUrl }); | ||
} | ||
else | ||
{ | ||
// If SYSTEM_TESTS_AWS_URL is not set, create default clients | ||
snsClient = new AmazonSimpleNotificationServiceClient(); | ||
sqsClient = new AmazonSQSClient(); | ||
} | ||
|
||
// Create SNS topic | ||
Console.WriteLine($"[SNS] Produce: Creating topic {topic}"); | ||
CreateTopicResponse createTopicResponse = await snsClient.CreateTopicAsync(topic); | ||
string topicArn = createTopicResponse.TopicArn; | ||
|
||
// Create SQS queue | ||
Console.WriteLine($"[SNS] Produce: Creating queue {queue}"); | ||
CreateQueueResponse createQueueResponse = await sqsClient.CreateQueueAsync(queue); | ||
string queueUrl = createQueueResponse.QueueUrl; | ||
|
||
// Get queue ARN | ||
GetQueueAttributesResponse queueAttributes = await sqsClient.GetQueueAttributesAsync(new GetQueueAttributesRequest | ||
{ | ||
QueueUrl = queueUrl, | ||
AttributeNames = new List<string> { "QueueArn" } | ||
}); | ||
string queueArn = queueAttributes.Attributes["QueueArn"]; | ||
|
||
// Set queue policy to allow SNS to send messages | ||
string policy = $@"{{ | ||
""Version"": ""2012-10-17"", | ||
""Id"": ""{queueArn}/SQSDefaultPolicy"", | ||
""Statement"": [ | ||
{{ | ||
""Sid"": ""Allow-SNS-SendMessage"", | ||
""Effect"": ""Allow"", | ||
""Principal"": {{ | ||
""Service"": ""sns.amazonaws.com"" | ||
}}, | ||
""Action"": ""sqs:SendMessage"", | ||
""Resource"": ""{queueArn}"", | ||
""Condition"": {{ | ||
""ArnEquals"": {{ | ||
""aws:SourceArn"": ""{topicArn}"" | ||
}} | ||
}} | ||
}} | ||
] | ||
}}"; | ||
|
||
await sqsClient.SetQueueAttributesAsync(new SetQueueAttributesRequest | ||
{ | ||
QueueUrl = queueUrl, | ||
Attributes = new Dictionary<string, string> | ||
{ | ||
{ "Policy", policy } | ||
} | ||
}); | ||
|
||
// Subscribe queue to topic | ||
await snsClient.SubscribeAsync(new SubscribeRequest | ||
{ | ||
TopicArn = topicArn, | ||
Protocol = "sqs", | ||
Endpoint = queueArn, | ||
Attributes = new Dictionary<string, string> | ||
{ | ||
{ "RawMessageDelivery", "true" } | ||
} | ||
}); | ||
|
||
using (Datadog.Trace.Tracer.Instance.StartActive("SnsProduce")) | ||
{ | ||
// Publish message to SNS topic | ||
await snsClient.PublishAsync(new PublishRequest | ||
{ | ||
TopicArn = topicArn, | ||
Message = message | ||
}); | ||
Console.WriteLine($"[SNS] Done with producing message: {message}"); | ||
} | ||
} | ||
} | ||
|
||
class SnsConsumer | ||
{ | ||
public static async Task DoWork(string queue, string message) | ||
{ | ||
string? awsUrl = Environment.GetEnvironmentVariable("SYSTEM_TESTS_AWS_URL"); | ||
|
||
IAmazonSQS sqsClient; | ||
if (!string.IsNullOrEmpty(awsUrl)) | ||
{ | ||
// If SYSTEM_TESTS_AWS_URL is set, use it for ServiceURL | ||
sqsClient = new AmazonSQSClient(new AmazonSQSConfig { ServiceURL = awsUrl }); | ||
} | ||
else | ||
{ | ||
// If SYSTEM_TESTS_AWS_URL is not set, create a default client | ||
sqsClient = new AmazonSQSClient(); | ||
} | ||
|
||
// Create queue | ||
Console.WriteLine($"[SNS] Consume: Creating queue {queue}"); | ||
CreateQueueResponse responseCreate = await sqsClient.CreateQueueAsync(queue); | ||
var qUrl = responseCreate.QueueUrl; | ||
|
||
Console.WriteLine($"[SNS] looking for messages in queue {qUrl}"); | ||
|
||
bool continueProcessing = true; | ||
|
||
while (continueProcessing) | ||
{ | ||
using (Datadog.Trace.Tracer.Instance.StartActive("SnsConsume")) | ||
{ | ||
var result = await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest | ||
{ | ||
QueueUrl = qUrl, | ||
MaxNumberOfMessages = 1, | ||
WaitTimeSeconds = 1 | ||
}); | ||
|
||
if (result == null || result.Messages.Count == 0) | ||
{ | ||
Console.WriteLine("[SNS] No messages to consume at this time"); | ||
await Task.Delay(1000); | ||
continue; | ||
} | ||
|
||
var receivedMessage = result.Messages[0]; | ||
Console.WriteLine("[SNS] Message dump:"); | ||
Console.WriteLine($" MessageId: {receivedMessage.MessageId}"); | ||
Console.WriteLine($" ReceiptHandle: {receivedMessage.ReceiptHandle}"); | ||
Console.WriteLine($" MD5OfBody: {receivedMessage.MD5OfBody}"); | ||
Console.WriteLine($" Body: {receivedMessage.Body}"); | ||
|
||
if (receivedMessage.Attributes != null && receivedMessage.Attributes.Count > 0) | ||
{ | ||
Console.WriteLine(" Attributes:"); | ||
foreach (var attr in receivedMessage.Attributes) | ||
{ | ||
Console.WriteLine($" {attr.Key}: {attr.Value}"); | ||
} | ||
} | ||
|
||
if (receivedMessage.MessageAttributes != null && receivedMessage.MessageAttributes.Count > 0) | ||
{ | ||
Console.WriteLine(" MessageAttributes:"); | ||
foreach (var attr in receivedMessage.MessageAttributes) | ||
{ | ||
Console.WriteLine($" {attr.Key}: {attr.Value.StringValue}"); | ||
} | ||
} | ||
|
||
// Check if the message body matches directly | ||
if (receivedMessage.Body == message) | ||
{ | ||
Console.WriteLine($"[SNS] Consumed message from {qUrl}: {receivedMessage.Body}"); | ||
continueProcessing = false; | ||
break; | ||
} | ||
|
||
await Task.Delay(1000); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SNS api for .NET & Java allow sending messages just by topic name, can't deduce full ARN for these tracers