I trying to migrate my Node.js application from jaeger-client to @opentelemetry/* packages
In my Node.js application i have a simple http server and i want to create span on each response.
With jaeger-client i did it in the following way:
import { FORMAT_HTTP_HEADERS } from 'opentracing';
import { initTracerFromEnv } from 'jaeger-client';
const tracer = initTracerFromEnv(/* some options here */);
// app is expressjs app
app.get('/home', req => {
const rootSpan = tracer.startSpan('response', {
childOf: tracer.extract(FORMAT_HTTP_HEADERS, req.headers),
});
// ...make child spans of rootSpan
});
I want to connect my root span of response with spans from other application, that performs request to my Node.js application server. About the same as I would do it with jaeger-client.
How can i make it with OpenTelemetry instead of jeager-client and opentracing? Is it possible to create child spans manually, without auto instrumentations?
You can do the Context Propagation by extracting the parent context from the request headers and creating the child span with it, as follows:
const opentelemetry = require('@opentelemetry/api');
const {SpanKind, ROOT_CONTEXT} = require("@opentelemetry/api");
app.get('/home', req => {
// Get incoming context from headers
const remoteCtx = opentelemetry.propagation.extract(ROOT_CONTEXT, req.headers);
// ...make child spans of remoteSpan
// Create child span passing parent context
const childSpan = tracer.startSpan(
'childSpan',
remoteCtx
);
// ... Do important stuff
// End the span
childSpan.end();
});