Skip to content

Tracing

@granit/tracing provides framework-agnostic OpenTelemetry utilities for browser tracing — mirroring Granit.Observability on the .NET backend. It exports a getTraceContext() function that reads the active span’s trace and span IDs for correlation with @granit/logger-otlp.

@granit/react-tracing wraps this into a TracingProvider that initializes the OpenTelemetry WebTracerProvider with auto-instrumentations (fetch, XHR, document-load) and an OTLP HTTP exporter, plus hooks for creating custom spans.

Peer dependencies: @opentelemetry/api, @opentelemetry/sdk-trace-web, @opentelemetry/exporter-trace-otlp-http, react ^19

  • Directory@granit/tracing/ TraceContext type, getTraceContext utility (framework-agnostic)
    • @granit/react-tracing TracingProvider, useTracer, useSpan hooks
PackageRoleDepends on
@granit/tracinggetTraceContext(), TraceContext, TracingConfig@opentelemetry/api
@granit/react-tracingTracingProvider, useTracer, useSpan@granit/tracing, @opentelemetry/sdk-trace-web, @opentelemetry/exporter-trace-otlp-http, react
import { TracingProvider } from '@granit/react-tracing';
function App({ children }) {
return (
<TracingProvider config={{
serviceName: 'guava-front',
serviceVersion: '1.0.0',
exporter: { url: '/v1/traces' },
}}>
{children}
</TracingProvider>
);
}
interface TraceContext {
traceId: string;
spanId: string;
}
interface TracingExporterConfig {
url: string;
headers?: Record<string, string>;
}
interface TracingConfig {
serviceName: string;
serviceVersion?: string;
exporter: TracingExporterConfig;
instrumentFetch?: boolean; // default: true
instrumentXhr?: boolean; // default: true
instrumentDocumentLoad?: boolean; // default: true
additionalInstrumentations?: Instrumentation[];
}

Reads the active span from the OpenTelemetry global context. Safe to call from non-React code — used as the getTraceContext callback for @granit/logger-otlp.

function getTraceContext(): TraceContext | undefined;

Initializes the OpenTelemetry WebTracerProvider with:

  • Auto-instrumentations: fetch, XMLHttpRequest, document-load
  • OTLP HTTP exporter with gated startup (probes the collector — if unavailable, disables export for the session with a warning instead of continuous errors)
  • Graceful shutdown on unmount (flushes pending spans)
<TracingProvider config={{
serviceName: 'guava-front',
exporter: { url: '/v1/traces' },
}}>
{children}
</TracingProvider>

Returns the OpenTelemetry Tracer instance from the provider.

function useTracer(): Tracer;

Helpers for creating custom spans within React components.

interface UseSpanReturn {
withSpan: <T>(name: string, fn: (span: Span) => T | Promise<T>) => Promise<T>;
createSpan: (name: string, options?: SpanOptions) => Span;
}
function useSpan(): UseSpanReturn;
  • withSpan — Executes a function within a new span. The span is automatically ended on completion or rejection. Errors are recorded on the span.
  • createSpan — Creates a span for manual control. You must call span.end() yourself.
CategoryKey exportsPackage
TypesTraceContext, TracingConfig, TracingExporterConfig@granit/tracing
UtilitygetTraceContext()@granit/tracing
ProviderTracingProvider@granit/react-tracing
HooksuseTracer(), useSpan()@granit/react-tracing