@open-infra/telemetry-tanstack
v0.2.0
Published
TanStack Start request middleware, Router navigation and Query tracing for OpenInfra telemetry.
Maintainers
Readme
@open-infra/telemetry-tanstack
TanStack Start request middleware, Router navigation events, and Query cache spans, using the existing OpenInfra runtime. Separate entrypoints keep Node request-context code out of browser bundles.
// Server-only start configuration; initialize telemetry before accepting requests.
import { createStart } from '@tanstack/react-start';
import { tanstackStartTelemetry } from '@open-infra/telemetry-tanstack/start';
const middleware = tanstackStartTelemetry({ routeName: () => 'application.request' });
export const startInstance = createStart(() => ({ requestMiddleware: [middleware] }));Use instrumentTanStackRouter(router, runtime, { routeName }) from /router. The mapper must return a route template, not a path containing user identifiers. Use instrumentTanStackQuery(queryClient.getQueryCache(), runtime) from /query; it returns an unsubscribe function that also closes outstanding spans. Query keys, variables, results and cache data are never captured. At most 128 query spans are tracked simultaneously; individual fetch attempts end on success/error or cache removal.
The root entrypoint exports traceTanStackOperation(runtime, operationName, asyncAction) for explicit loader/server-function timing. Use fixed operation names. Helpers preserve action results and thrown errors, honor consent when a browser runtime is supplied, and reuse its privacy/export pipeline.
Start middleware is Node-only because it uses the server runtime's request context. Router and Query entrypoints work with compatible server or browser runtimes. Tested against Start 1.168.60, Router 1.170.41 and Query Core 5.104.1.
Official middleware guide: https://tanstack.com/start/latest/docs/framework/react/guide/middleware
Compatibility checks also pass with TanStack Start and Router 1.168.26, the versions pinned by Slego.
