opentelemetry-js: AWS lambda - process is killed before the span is being exported

This is a simple scenario of lambda function

exports.handler = ( async ( event, ctx ) => {
    const span = tracer.startSpan('test');
    span.end();
    return 'done';
} );

The span is not exported correctly as lambda seems to kill the process before it gets exported When adding some timeout before the process get killed the span iss exported

exports.handler = ( async ( event, ctx ) => {
    const span = tracer.startSpan('test');
    span.end();
    await new Promise((resolve)=> {
        setTimeout(()=> {
            resolve();
        }, 500);
    })
    
    return 'done';
} );

So the idea is to implement some async method either on tracer or exporter or change the exporter method shutdown to be a promise and to be resolved when the last exports is sent successfully

WDYT ?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (17 by maintainers)

Most upvoted comments

I would open an issue on the contrib repo and ping @willarmiros or @anuraaga since they maintain that instrumentation

In an autoinstrumentation I would suggest you either:

  1. patch the handler, and prevent the wrapped handler from finishing until the export is complete
  2. patch the underlying runtime itself. This is more risky, but it turns out the API of the RAPIDClient (their internal name for the runtime environment) is much simpler and more straightforward. This is going to be what I do for https://github.com/open-telemetry/opentelemetry-js-contrib/issues/132

For this issue, I think it is sufficient to give the user a way to manually flush and not worry about wrapping handler or patching anything.