Distributed trace, parent transaction ending before child distributed span comes back

Hello

I have a distributed trace that is basically a main transaction that calls an external endpoint that is responsible for some Mongo DB integration. The thing is, I'm trying to understand why currently my main transaction's trace is ending before the call to the external endpoint comes back (see image).

That could be because of some javascript-promises-async-await error in my code, but I did some debugging and I think things are chained correctly. Anyway here is my code:

Controller:

exports.generateDistributedTransaction = async (req, res, next) => {
    const simulationRequest = res.locals.simulationRequest;
    const simulation = new Simulation();
    await simulation.generateDistributedTransaction(simulationRequest);
    res.status(200).send();
};

Service:

async generateDistributedTransaction(simulationRequest) {
    const datasets = ['users', 'todos', 'comments', 'albuns', 'photos'];
    const randomIndex = Math.floor(Math.random() * (datasets.length));
    const usersFetchAndSaveEndpoint = `http://external-service:3000/api/${datasets[randomIndex]}/fetch`;
    await this.init(simulationRequest, false);
    return await axios.get(usersFetchAndSaveEndpoint);
}

Should I insert the traceparent in some header when calling the external service?

Should I wrap the externa call in a manually created transaction or span?

Here is what it looks like currently:

Thank you for your help.

Never mind... just solved it.

The problem was not in my main app, but in the external service: it was returning too early. I was calling mongo.connect(url, callback) and this immediatly returned to the external service's controller which then ended the request.

I now got the mongo.connect() wrapped around a Promise and am waiting until it does its thing to resolve() and then res.send() to the main app.

Here is how it looks now:

1 Like