@lelemondev/sdk - v0.9.9
    Preparing search index...

    Function withObserve

    • Wrap an AWS Lambda handler with automatic trace flushing

      Always flushes before returning - Lambda freezes the container immediately after the handler returns, so this is required.

      Type Parameters

      • TEvent = unknown
      • TResult = unknown

      Parameters

      Returns LambdaHandler<TEvent, TResult>

      Wrapped handler that auto-flushes traces

      // API Gateway event
      export const handler = withObserve(async (event) => {
      const body = JSON.parse(event.body);
      const openai = observe(new OpenAI());
      const result = await openai.chat.completions.create({
      model: 'gpt-4',
      messages: [{ role: 'user', content: body.message }],
      });
      return {
      statusCode: 200,
      body: JSON.stringify(result.choices[0].message),
      };
      });
      // With typed events
      import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';

      export const handler = withObserve<APIGatewayProxyEvent, APIGatewayProxyResult>(
      async (event, context) => {
      return { statusCode: 200, body: 'OK' };
      }
      );