Tracing
app.useTracing() creates an OpenTelemetry span for every request, so you can follow a request through your services in Jaeger, Grafana Tempo, Honeycomb, Datadog or any other OTLP backend.
Megh only creates spans, through @opentelemetry/api. You choose the SDK and where the spans are sent.
Setup
Install the API, the Node SDK and an exporter:
npm install @opentelemetry/api @opentelemetry/sdk-node @opentelemetry/exporter-trace-otlp-httpStart the SDK before the app, in its own file:
// src/tracing.ts
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
const sdk = new NodeSDK({
serviceName: 'orders-api',
traceExporter: new OTLPTraceExporter(), // OTEL_EXPORTER_OTLP_ENDPOINT, default http://localhost:4318
});
sdk.start();
process.on('SIGTERM', () => sdk.shutdown());// src/app.ts
import './tracing';
import { Megh } from '@meghjs/core';
const app = Megh.createApplication();
app.useTracing({ ignore: ['/health', '/ready'] });
app.useControllers([OrderController]);
app.run(4000);To see spans without a backend, use the SDK's ConsoleSpanExporter as the traceExporter; each span is printed when the request ends.
What a span holds
One SERVER span per request:
| Field | Value |
|---|---|
| Name | GET /orders/:id: the method and route pattern, so spans group by route |
http.request.method | GET |
http.route | /orders/:id |
url.path | /orders/42 (no query string) |
http.response.status_code | 200 |
server.address, user_agent.original | From the request |
A span is marked as an error when the handler throws (with the exception recorded) or the response is a 5xx.
If the request has a traceparent header, the span continues that trace, so a call from another traced service shows up as one trace.
Options
| Option | Default | Description |
|---|---|---|
name | "megh" | Tracer name |
ignore | [] | Paths not traced: exact strings or regular expressions, e.g. ['/health', /^\/assets\//] |
Calling useTracing() twice, or after app.run(), throws. Without @opentelemetry/api installed, it throws with the install command.
Not traced yet
Spans cover HTTP requests only. Database queries, queue jobs and messaging don't create spans of their own yet.