Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongodb instrumentation: nested span is not generated when using callback #390

Closed
AndriyPashchak opened this issue Mar 17, 2021 · 0 comments · Fixed by #403
Closed

mongodb instrumentation: nested span is not generated when using callback #390

AndriyPashchak opened this issue Mar 17, 2021 · 0 comments · Fixed by #403
Assignees
Labels
bug Something isn't working

Comments

@AndriyPashchak
Copy link

What version of OpenTelemetry are you using?

"@opentelemetry/api": "0.18.0"
"@opentelemetry/core": "0.18.0"
"@opentelemetry/instrumentation-mongodb": "0.14.0"

What version of Node are you using?

12.20.0

What did you do?

import { SimpleSpanProcessor } from '@opentelemetry/tracing';
import { NodeTracerProvider } from '@opentelemetry/node';
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';

import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb';

const serviceName = 'otel-mongodb-test';

const provider = new NodeTracerProvider();

registerInstrumentations({
    instrumentations: [
        new HttpInstrumentation(),
        new MongoDBInstrumentation(),
    ],
    tracerProvider: provider,
});

const zipkinExporter = new ZipkinExporter({
    serviceName,
    url: 'http://localhost:9411/api/v2/spans'
});

provider.addSpanProcessor(new SimpleSpanProcessor(zipkinExporter));
provider.register();
const tracer = provider.getTracer(serviceName);

import * as http from 'http';
import * as mongodb from 'mongodb';

let collection;
const mongodbConnect = async () => {
    try {
        const client = await mongodb.MongoClient.connect('mongodb://localhost:27017');
        collection = client.db('test-db').collection('users');
        console.log('\nSuccessfully connected to mongoDB!\n');
    } catch (err) {
        console.log(`Error while connecting to mongoDB ${err}`, err);
        throw err;
    }
};

mongodbConnect();

http.createServer(function (_, res) {
    const doc = { name: 'Rick' };

    collection.insertOne(doc, function () {
        collection.findOne({}, function () {
            res.end();
        });
    });
}).listen(9090);

What did you expect to see?

The new mongodb.find span being generated as a child of the mongodb.insert span.

What did you see instead?

Screenshot 2021-03-17

Additional context

It happens when a callback for a parent mongodb.insert operation is used.

When I'm using Promise.then instead of a callback a span for a find operation is generated as expected.

The following example works fine:

collection.insertOne(doc).then(function () {
        collection.findOne({}, function () {
            res.end();
        });
    });
@AndriyPashchak AndriyPashchak added the bug Something isn't working label Mar 17, 2021
@obecny obecny self-assigned this Mar 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants