@grand-board/otel-cloudflare - v6.11.0
    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

      IMPORTANT: Call initOTLP() before using this function to set up collectors. Call flush() after the request completes to export telemetry.

      Parameters

      Returns Promise<Response>

      // SvelteKit hooks.server.ts
      import { initOTLP, traceHandler } from '@grand-board/otel-cloudflare';

      export async function handle({ event, resolve }) {
      const ctx = initOTLP(event.platform?.env, 'my-service');

      try {
      return await traceHandler(event.request, async (span) => {
      // Add custom attributes to span
      span.setAttribute('user.id', event.locals.userId);
      return resolve(event);
      });
      } finally {
      if (event.platform?.context?.waitUntil) {
      event.platform.context.waitUntil(ctx.flush());
      } else {
      await ctx.flush();
      }
      }
      }
      // With ignoreUrls option
      return await traceHandler(
      event.request,
      (span) => resolve(event),
      { ignoreUrls: ["/health", /^/api/internal//] }
      );