@logtide/sveltekit
v0.7.0
Published
LogTide SDK integration for SvelteKit — handle, handleError, handleFetch hooks
Readme
Features
handlehook — automatic request spans with trace contexthandleErrorhook — capture unexpected errors with request contexthandleFetchhook — propagatetraceparentto server-side fetches- Client-side init — global error handler for the browser
- W3C Trace Context propagation (
traceparent) - Scope in
event.locals— access trace context in load functions - Full TypeScript support with strict types
Installation
npm install @logtide/sveltekit
# or
pnpm add @logtide/sveltekit
# or
yarn add @logtide/sveltekitQuick Start
1. Server Hooks
// src/hooks.server.ts
import { logtideHandle, logtideHandleError, logtideHandleFetch } from '@logtide/sveltekit/server';
export const handle = logtideHandle({
dsn: 'https://[email protected]',
// Or use apiUrl + apiKey instead of dsn:
// apiUrl: 'https://your-instance.com',
// apiKey: 'lp_your_key',
service: 'my-sveltekit-app',
environment: 'production',
});
export const handleError = logtideHandleError();
export const handleFetch = logtideHandleFetch();2. Client Hooks
// src/hooks.client.ts
import { initLogtide } from '@logtide/sveltekit/client';
initLogtide({
dsn: 'https://[email protected]',
// Or: apiUrl + apiKey instead of dsn
service: 'my-sveltekit-app',
});Server API
logtideHandle(options)
Creates a SvelteKit handle hook that:
- Extracts incoming
traceparentheader (or generates a new trace ID) - Creates a span for the request (e.g.
GET /dashboard) - Stores the scope and span ID in
event.locals - Finishes the span with
okorerrorbased on response status - Injects
traceparentinto the response headers
import { logtideHandle } from '@logtide/sveltekit/server';
export const handle = logtideHandle({
dsn: '...',
service: 'my-app',
});Locals available in load functions:
// In +page.server.ts or +layout.server.ts
export function load({ locals }) {
// locals.__logtideScope — the Scope object for this request
// locals.__logtideSpanId — the current span ID
}logtideHandleError()
Creates a SvelteKit handleError hook that captures unexpected errors with HTTP context and the request scope.
import { logtideHandleError } from '@logtide/sveltekit/server';
export const handleError = logtideHandleError();logtideHandleFetch()
Creates a SvelteKit handleFetch hook that injects traceparent into server-side fetch requests, enabling distributed tracing across services.
import { logtideHandleFetch } from '@logtide/sveltekit/server';
export const handleFetch = logtideHandleFetch();Client API
initLogtide(options)
Initialize LogTide on the client side. Installs GlobalErrorIntegration for unhandledrejection events.
import { initLogtide } from '@logtide/sveltekit/client';
initLogtide({
dsn: '...',
service: 'my-app',
});Exports
// Main entry — re-exports server and client
import { logtideHandle, logtideHandleError, logtideHandleFetch, initLogtide } from '@logtide/sveltekit';
// Server-specific
import { logtideHandle, logtideHandleError, logtideHandleFetch } from '@logtide/sveltekit/server';
// Client-specific
import { initLogtide } from '@logtide/sveltekit/client';License
MIT License - see LICENSE for details.
