exceptionless / Exceptionless.Net

Exceptionless clients for the .NET platform
https://exceptionless.com
Other
554 stars 142 forks source link

Events are not sent in AWS Lambda (ASP.NET Core Web API) . #215

Closed hmvs closed 3 years ago

hmvs commented 5 years ago

Hi guys.

As far as AWS lambda freezes all background threads right after returning results, exceptionless is not working, seems to be because of Timers also get frozen.

 [
  {
    "date": "2019-08-19T14:47:33.2301596+00:00",
    "level": {
      "name": "Trace",
      "ordinal": 0
    },
    "source": "DefaultEventQueue",
    "message": "Processing queue...",
    "exception": null
  },
  {
    "date": "2019-08-19T14:47:23.0338945+00:00",
    "level": {
      "name": "Info",
      "ordinal": 2
    },
    "source": "DefaultEventQueue",
    "message": "Sent 1 events to \"https://collector.exceptionless.io\".",
    "exception": null
  },
  {
    "date": "2019-08-19T14:43:09.9984337+00:00",
    "level": {
      "name": "Trace",
      "ordinal": 0
    },
    "source": "DefaultEventQueue",
    "message": "Processing queue...",
    "exception": null
  },
  {
    "date": "2019-08-19T14:42:43.3919108+00:00",
    "level": {
      "name": "Trace",
      "ordinal": 0
    },
    "source": "ExceptionlessClient",
    "message": "Submitting event: type=log",
    "exception": null
  },
  {
    "date": "2019-08-19T14:42:43.3906358+00:00",
    "level": {
      "name": "Trace",
      "ordinal": 0
    },
    "source": "DuplicateCheckerPlugin",
    "message": "Enqueueing event with hash: 5055887 to cache.",
    "exception": null
  },
  {
    "date": "2019-08-19T14:42:43.3903982+00:00",
    "level": {
      "name": "Trace",
      "ordinal": 0
    },
    "source": "DuplicateCheckerPlugin",
    "message": "Checking event: Unknown error responding to request: InvalidOperationException:\nSystem.InvalidOperationException: It is our exception that we throw to test\n   at My.Api.Controllers.ValuesController.Get(Int32 id) with hash: 5055887",
    "exception": null
  },
  {
    "date": "2019-08-19T14:42:41.9262751+00:00",
    "level": {
      "name": "Trace",
      "ordinal": 0
    },
    "source": "DefaultEventQueue",
    "message": "Processing queue...",
    "exception": null
  }
]

2019-08-19T14:47:23.0338945+00:00 I invoked Lambda with a parameter that triggers ProcessQueue() method.

See: related

niemyjski commented 5 years ago

You could call ProcessQueue inside of here:

context.Response.OnCompleted(() => { // Perform slow code here. return Task.CompletedTask; });

or on the last line of your function. Did that solve it?

hmvs commented 5 years ago

I've tried these two variants. Both of them responds only when the whole code executes, including part in OnCompleted block.

[HttpGet("{id}")]
public IActionResult Get(int id) {
    this.HttpContext.Response.OnCompleted(() => {
         Startup.ExceptionlessClient.ProcessQueue();
         for (long i = 0; i <= 8000000000; i++) {
         }
    return Task.CompletedTask;
     });    
     return Ok("exit ok");
}
[HttpGet("{id}")]
public IActionResult Get(int id) {
    this.HttpContext.Response.OnCompleted(() => {
        return Task.WhenAll(Startup.ExceptionlessClient.ProcessQueueAsync(), Task.Delay(20000));
        });
return Ok("exit ok");
}
niemyjski commented 4 years ago

The inner for loop won't do anything because the compiler will remove it. You'd be better to just call ProcessQueueAsync and await it otherwise a deadlock could occur. Were you able to get any further on this issue?

niemyjski commented 4 years ago

Were you able to get any further on this issue?

hmvs commented 4 years ago

Unfortunately, no. Just thought we need some very lightweight client.

niemyjski commented 3 years ago

What happens if you call ExceptionlessClient.Default.Startup() in your app. Does it always report the events?

niemyjski commented 3 years ago

We just made some huge improvements to our ASP.NET Core client to better support functions. Can you please upgrade to https://github.com/exceptionless/Exceptionless.Net/releases/tag/v4.6.2 and let us know if you run into any issues.

ejsmith commented 3 years ago

Take a look at these sample apps as well:

https://github.com/exceptionless/Exceptionless.Net/tree/master/samples/Exceptionless.SampleLambda

https://github.com/exceptionless/Exceptionless.Net/tree/master/samples/Exceptionless.SampleLambdaAspNetCore