Skip to content

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:

bash
npm install @opentelemetry/api @opentelemetry/sdk-node @opentelemetry/exporter-trace-otlp-http

Start the SDK before the app, in its own file:

ts
// 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());
ts
// 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:

FieldValue
NameGET /orders/:id: the method and route pattern, so spans group by route
http.request.methodGET
http.route/orders/:id
url.path/orders/42 (no query string)
http.response.status_code200
server.address, user_agent.originalFrom 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 ​

OptionDefaultDescription
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.

Released under the MIT License.