Skip to content

@flarelog/sdk


@flarelog/sdk / pagesFunction

Function: pagesFunction()

pagesFunction<T>(logger, handler): PagesFunctionHandler<T>

Defined in: frameworks/cf-workers.ts:113

Wrap a Cloudflare Pages Function handler with automatic OTel instrumentation.

Pages Functions run on the Workers runtime but use a different API shape:

  • Receives a single context object with request, env, waitUntil, etc.
  • Extracts W3C traceparent from incoming headers (or starts a new trace)
  • Creates a SPAN_KIND_SERVER span: GET /api/users
  • Sets http.request.method, url.path, url.full, http.response.status_code, etc.
  • All logs emitted inside the handler carry the span's traceId + spanId
  • Records exceptions on the span and sets span status
  • Flushes telemetry via context.waitUntil() (with blocking fallback for tests)

Honors the same OPTIONS/HEAD and ignorePaths bypass as workerFetch — see FlareLogConfig.ignorePaths for details.

Type Parameters

T

T = Response

Parameters

logger

FlareLog

handler

PagesFunctionHandler<T>

Returns

PagesFunctionHandler<T>

Examples

typescript
// functions/api/hello.ts
import { flarelog, pagesFunction } from "@flarelog/sdk";

const logger = flarelog({ apiKey: "fl_your_key" });

export const onRequest = pagesFunction(logger, async (context) => {
  logger.info("Hello from Pages", { url: context.request.url });
  return new Response("Hello from Pages Functions!");
});

With middleware

typescript
// functions/_middleware.ts
import { flarelog, pagesFunction } from "@flarelog/sdk";

const logger = flarelog({});

export const onRequest = pagesFunction(logger, async (context) => {
  logger.info("Middleware running");
  return context.next();
});

Released under the MIT License.