I am able to get the current active context, span, and associated trace ID with the following code. Is there anyway to get the rest of the child spans that are associated with the trace (go from traceId
to all of the associated spans)?
const opentelemetryApi = require('@opentelemetry/api');
const activeCtx = opentelemetryApi.context.active();
const span = opentelemetryApi.trace.getSpan(activeCtx);
const traceId = span.spanContext().traceId;
Spans have a child -> parent relationship and define the traceId
and parentSpanId
that they belong to. But if I have the root span, I don't see a way to navigate downwards for the child spans. Is there a way to navigate the DAG of spans in a trace?
Can I get a list of all of the spans? Maybe it's possible to setup a passthrough collector or processor to capture the spans that way? Maybe a way to listen just for spans from a certain traceId
?
Related to Is there a way to get the full trace of a request given a starting point from anywhere in the lifecycle of the trace? although there is no practical example of how to get your hands on a trace and all of the spans in that question.
You can see my full OpenTelemetry tracing.js
setup in https://github.com/matrix-org/matrix-public-archive/pull/27
My goal is to add a performance bar to the top of the page served to the client that shows off the slow API requests that happened in the backend. I would like to pull and re-use the data from the OpenTelemetry spans/traces.
require('./tracing.js');
const opentelemetryApi = require('@opentelemetry/api');
const express = require('express');
// Instrumented by `@opentelemetry/instrumentation-express`
const app = express();
app.get('/test', (req, res) => {
// Make some external API HTTP requests which create child spans thanks to `@opentelemetry/instrumentation-http`
const res = await fetch('https://matrix-client.matrix.org/_matrix/client/versions');
const activeCtx = opentelemetryApi.context.active();
const span = opentelemetryApi.trace.getSpan(activeCtx);
const traceId = span.spanContext().traceId;
// TODO: Find all spans associated with the `traceId`
res.send(`<section><h1>Performance bar</h1> <p>(Trace data)</p></section> other page HTML`);
});
app.listen(3000);