openzipkin / zipkin-js

Zipkin instrumentation for Node.js and browsers
Apache License 2.0
565 stars 171 forks source link

Missing parent link #533

Open mafad-pandora opened 1 year ago

mafad-pandora commented 1 year ago

When multiple sequential calls to tracer.local are made, then only the span of the first tracer.local is linked to the parent span, while the spans of subsequent calls have no parent at all.

See the example program below, where I would expect a parent "GET" span with 3 child spans "my_db_operation 1", "my_db_operation 2" and "my_db_operation 3".

Instead the actual result there is a parent "GET" span with 1 child span "my_db_operation 1", then two more spans that have no parent: "my_db_operation 2" and "my_db_operation 3".

Screenshot 2022-11-10 at 16 32 27 Screenshot 2022-11-10 at 16 32 41
const express = require('express')
const app = express()
const port = 3000

const { Tracer, ExplicitContext, BatchRecorder, jsonEncoder } = require("zipkin");
const { HttpLogger } = require("zipkin-transport-http");
const CLSContext = require('zipkin-context-cls');
const zipkinMiddleware = require("zipkin-instrumentation-express").expressMiddleware;
const tracer = new Tracer({
  ctxImpl: new CLSContext('zipkin'), // implicit in-process context
  recorder: new BatchRecorder({
    logger: new HttpLogger({
      endpoint: 'http://jaeger-collector:9411/api/v2/spans',
      jsonEncoder: jsonEncoder.JSON_V2,
    }),
  }),
  localServiceName: "example-app",
});
app.use(zipkinMiddleware({ tracer }));

function my_db_operation(x) {
  console.log(`My DB operation ${x}`)
  ms = Math.floor(Math.random() * 600) + 50;
  return new Promise(resolve => setTimeout(resolve, ms));
}

app.get('/', async (req, res) => {
  await tracer.local("my_db_operation 1", () => my_db_operation(1));
  await tracer.local("my_db_operation 2", () => my_db_operation(2));
  await tracer.local("my_db_operation 3", () => my_db_operation(3));
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Maartens example app started and is listening on port ${port}`)
})