@tigorlazuardi/otel-cloudflare - v1.1.1
    Preparing search index...

    Function traceHandler

    • Trace an incoming HTTP request with full instrumentation

      This is a standalone function for use in non-standard environments like SvelteKit. It provides the same functionality as the instrumented fetch handler:

      • Creates SERVER span with HTTP semantic convention attributes
      • Extracts traceparent from request headers for distributed tracing
      • Captures request/response body (truncated to 8KB, text-based content only)
      • Logs request summary with method, path, status, size, and duration
      • Adds traceparent header to response for downstream correlation
      • Automatically initializes OTLP and flushes telemetry via ctx.waitUntil

      Parameters

      • ctx: ExecutionContext

        Cloudflare ExecutionContext (provides waitUntil for non-blocking flush)

      • request: Request

        The incoming HTTP request

      • handler: (span: Span) => Promise<Response>

        The request handler function that receives the span

      • Optionaloptions: TraceHandlerOptions

        Optional configuration

      Returns Promise<Response>

      // SvelteKit hooks.server.ts
      import { traceHandler } from '@tigorlazuardi/otel-cloudflare';

      export const handle: Handle = async ({ event, resolve }) => {
      return traceHandler(
      event.platform!.context,
      event.request,
      () => resolve(event),
      { env: event.platform?.env, serviceName: 'my-service' }
      );
      };
      // Next.js with OpenNext Cloudflare
      import { traceHandler } from '@tigorlazuardi/otel-cloudflare';
      import { getCloudflareContext } from '@opennextjs/cloudflare';

      export async function middleware(request: NextRequest) {
      const { env, ctx } = await getCloudflareContext();
      return traceHandler(ctx, request, () => NextResponse.next(), { env, serviceName: 'my-app' });
      }