Frontend client
@planetmoondrop/logger-client is a tiny helper that keeps x-loki-trace-id flowing across browser → backend calls using Axios.
It requires no configuration – just attach it to your Axios instance once at startup.
On the frontend (browser / React / React Native / Next.js):
npm
npm install @planetmoondrop/logger-client axiosThe client has a single peer dependency:
axios >= 0.21.0
Basic usage
import axios from "axios";
import { attachSessionInterceptor } from "@planetmoondrop/logger-client";
export const api = axios.create({
baseURL: "https://api.example.com",
});
// Attach once at app startup (e.g. in a Next.js custom client or React root).
const { requestId, responseId } = attachSessionInterceptor(api);After this:
- Every response that includes
x-loki-trace-idstores the value in session‑scoped storage. - Every outgoing request includes the current
x-loki-trace-idheader.
On the backend, LokiLoggerModule reads the header and continues the same trace across services.
How it works
- Picks a storage backend:
- Browser:
sessionStorage(viaBrowserSessionStorage). - Non‑browser (SSR/Node): in‑memory
Map(MemoryStorage).
- Browser:
- Seeds a 32‑char random hex ID if none exists.
- Installs two Axios interceptors:
- Request interceptor
- Reads the stored trace ID.
- Injects it as
x-loki-trace-id.
- Response interceptor
- Reads
x-loki-trace-idfrom response headers. - If present, overwrites the stored trace ID with the backend‑assigned value.
- Reads
- Request interceptor
This ensures:
- The first outgoing request already carries a trace ID (even before the backend sends one).
- Once the backend generates its own ID, it becomes the canonical value.
- A same‑tab refresh reuses the existing trace ID (because
sessionStoragesurvives refreshes).
Backend requirements
For the correlation to work:
- Your backend must:
- Use
LokiLoggerModule.apply(app). - Keep
traceHeaderat the default'x-loki-trace-id'(or update both sides consistently). - Expose
x-loki-trace-idandx-parent-span-idusingapp.enableCors({ exposedHeaders: [...] }).
- Use
With that in place, frontend sessions show up as the first span in a Loki/Tempo trace, and all gateway + downstream logs share the same traceId.
What to do next
- Configure backend — register
LokiLoggerModule, enable CORS headers, and callapply(app). - Attach the Axios interceptor so trace IDs continue from the browser.
- Run Docker stack — start Loki, Tempo, Prometheus, and Grafana with Docker Compose.
Need help or want to support the project? Visit Support us.
Last updated on