question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Events are not sent in AWS Lambda

See original GitHub issue

I have discovered some issues when using SDK on AWS Lambda/API Gateway

The following message is logged to sentry

[HttpGet("/health")]
[ProducesResponseType((int) HttpStatusCode.OK)]
public async Task HealthCheck()
{
    _logger.LogCritical("test");
    await Task.Delay(TimeSpan.FromSeconds(10));
}

while this does not

[HttpGet("/health")]
[ProducesResponseType((int) HttpStatusCode.OK)]
public IActionResult HealthCheck()
{
    _logger.LogCritical("test");
    return Ok()
}

The problem might be related to https://github.com/aws/aws-lambda-dotnet/issues/239. The thread is killed right after the response is sent.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:28 (17 by maintainers)

github_iconTop GitHub Comments

3reactions
madmoxcommented, Oct 21, 2019

Final note regarding the middleware: you should use context.Response.OnCompleted instead of flushing after await this._next(context);, otherwise you might miss any event logged after the middleware returns - e.g. middlewares higher in the ASP.NET pipeline, or any error logged by the lambda integration when marshalling the response to API gateway:

public class SentryEventsFlusherMiddleware
{
    private readonly RequestDelegate _next;

    public SentryEventsFlusherMiddleware(RequestDelegate next)
    {
        this._next = next;
    }

    public async Task InvokeAsync(HttpContext context, ISentryClient sentryClient)
    {
        context.Response.OnCompleted(async () =>
        {
            await sentryClient.FlushAsync(timeout: TimeSpan.FromSeconds(10));
        });

        await this._next(context);
    }
}

For non-ASP.NET lambdas, I ended up writing a basic try/catch/finally block in the function:

public class Function
{
    private readonly ILogger _logger;
    private readonly ISentryClient _sentryClient;

    public Function()
    {
        // Initialize dependency injection, configure services & logging...
    }

    public async Task ProcessSQSEvent(SQSEvent sqsEvent)
    {
        try
        {
            // Your code
        }
        catch (Exception ex)
        {
            this._logger.LogError(ex, ex.Message);
            throw; // Let AWS move SQS messages to deadletter
        }
        finally
        {
            await this._sentryClient.FlushAsync(timeout: TimeSpan.FromSeconds(10));
        }
    }
}

This all worked for me with 2.0.0-beta4.

1reaction
bruno-garciacommented, Oct 21, 2019

I’d suggest using 2.0.0-beta4. The only reason it’s not 2.0.0 (non preview) is that I want to fit some more “breaking” changes in it before but we’re currently busy with a Android SDK so this is on hold.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot invocation issues in Lambda
Learn how to troubleshoot common invocation issues in Lambda.
Read more >
Why can AWS Lambda not read a test event sent to it via ...
I am writing an application on AWS Lambda in Python that expects to receive information that gets placed in the event dictionary. def...
Read more >
Lambda Events not triggering EventBridge destination
If you are using Lambda Destination to trigger EventBridge that will not work. The reason is that Lambda Destinations works only for Asynchronous...
Read more >
Event captured from AWS lambda are not showing up
Event captured from AWS lambda are not showing up. Hi, I'm using your node sdk to capture events from my backend that runs...
Read more >
Event loops and idle connections: why is my lambda not ...
The response isn't sent to the invoker until all event loop tasks are finished. You can configure the runtime to send the response...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found