Skip to content
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

[feature request] Add something similar to AWSLambdaWrapper for SQS Consumers that are not Lambda #2244

Open
xactlance opened this issue Oct 23, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@xactlance
Copy link

Component

None

Is your feature request related to a problem?

I had to get custom to implement extracting traceparent + tracestate from my SQS consuming BackgroundService. A lot of it's inspiration came from AWSLambdaWrapper.Trace. I would like the same thing as AWSLambdaWrapper.Trace for non-lambda SQS consumers.

What is the expected behavior?

There is a Trace extraction and propagation wrapper/helper for non-Lambda SQS Consumers (BackgroundService, CLI tool, whatever).

Which alternative solutions or features have you considered?

public static void Main(string[] args)
{
    Host
        .CreateDefaultBuilder(args)
        .ConfigureServices((context, services) =>
        {
            var activitySource = new ActivitySource(Assembly.GetExecutingAssembly().FullName!);
            services.AddSingleton(activitySource);
            services.AddAWSService<IAmazonSimpleNotificationService>();
            services.AddAWSService<IAmazonSQS>();
            services
                .AddOpenTelemetry()
                .WithTracing(tracerBuilder =>
                {
                    tracerBuilder
                        .AddSource(activitySource.Name)
                        .AddAWSInstrumentation();
                });
            services.AddSingleton<IHostedService, Service>();
        })
        .Build()
        .Run();
}

public class Service : BackgroundService
{
    private readonly ActivitySource _activitySource;
    private readonly IAmazonSQS _sqsClient;
    private readonly IAmazonSimpleNotificationService _snsClient;

    public Service(ActivitySource activitySource, IAmazonSQS sqsClient, IAmazonSimpleNotificationService snsClient)
    {
        _activitySource = activitySource;
        _sqsClient = sqsClient;
        _snsClient = snsClient;
    }

    protected async override Task ExecuteAsync(CancellationToken cancellationToken)
    {
        var receiveMessageResponse = await _sqsClient.ReceiveMessageAsync(
            new ReceiveMessageRequest 
            {
                QueueUrl = "queue-url",
                MessageAttributeNames = ["traceparent", "tracestate"],
            },
            cancellationToken);

        if (!receiveMessageResponse.Messages.Any())
        {
            return;
        }

        foreach (var message in receiveMessageResponse.Messages)
        {
            // Extract incoming trace information from the SQSMessage, if there is any.
            var parentContext = TraceContext.Extract(message);
            // Start an Activity, which is .NETs way of starting Traces. If there was a trace extracted from the
            // incoming SQSMessage then that trace will be continued. If not, then a new trace will be started.
            using (_activitySource.StartActivity("ExecuteAsync", ActivityKind.Server, parentContext.ActivityContext))
            {
                // This call will now automatically include traceparent and tracestate in MessageAttributes
                // thanks to the "AddAWSInstrumentation(...)".
                await _snsClient.PublishAsync(...);
            }
        }
    }
}

Additional context

No response

@xactlance xactlance added the enhancement New feature or request label Oct 23, 2024
@Kielek
Copy link
Contributor

Kielek commented Oct 23, 2024

@ppittle, @birojnayak, @srprash, please check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants