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 does not generate traces if it is executed outside of HTTP calls. #1910

Open
jgonzalezhesstec opened this issue Jan 19, 2024 · 2 comments · May be fixed by #2658
Open
Labels

Comments

@jgonzalezhesstec
Copy link

What version of OpenTelemetry are you using?

1.7.0

What version of Node are you using?

20.8.0

What did you do?

I register a console exporter and simply execute a call to mongodb when the program starts. I add a setTimeout to wait for some time for the traces to arrive, but they never end up occurring.
However, when running the same code within an HTTP call with Express, the traces are generated perfectly.

const { MongoDBInstrumentation } = require('@opentelemetry/instrumentation-mongodb');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { ConsoleSpanExporter, SimpleSpanProcessor } = require("@opentelemetry/sdk-trace-base");

const exporter = new ConsoleSpanExporter();
const provider = new NodeTracerProvider();

registerInstrumentations({
  instrumentations: [
    new MongoDBInstrumentation({
      enhancedDatabaseReporting: true 
    }),
  ],
});

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
provider.register();

const { MongoClient } = require('mongodb');

const uri = 'mongodb://mongodb:27017';
const client = new MongoClient(uri);

client.connect()
  .then(client => {
    const db = client.db('test');
    db.collection('testCollection').findOne()
      .then(result => {
        console.log(result);
        client.close();
      });
  });

setTimeout(() => {
  process.exit(0);
}, 10000);

No traces comes to the console.

However executing the mongdb query inside an HTTP handler so many traces appear.

const { MongoDBInstrumentation } = require('@opentelemetry/instrumentation-mongodb');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { ConsoleSpanExporter, SimpleSpanProcessor } = require("@opentelemetry/sdk-trace-base");
const { HttpInstrumentation } = require("@opentelemetry/instrumentation-http");
const { ExpressInstrumentation } = require("@opentelemetry/instrumentation-express");



const exporter = new ConsoleSpanExporter();
const provider = new NodeTracerProvider();

registerInstrumentations({
  instrumentations: [
		new HttpInstrumentation(),
		new ExpressInstrumentation(),
		new MongoDBInstrumentation({ enhancedDatabaseReporting: true }),
	],
});

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
provider.register();

const { MongoClient } = require('mongodb');
const express = require('express')
const http = require('http');

const uri = 'mongodb://mongodb:27017';
const client = new MongoClient(uri);
const app = express()
const port = 4000

client.connect()
  .then(client => {
    const db = client.db('test');

    app.get('/', (req, res) => {
      db.collection('testCollection').findOne()
        .then(result => {
          console.log(result);
        });
    });
  })

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
});

setTimeout(() => {
	http.get('http://localhost:4000')
}, 5000)

Is this the regular working mode?

The dependencies are:

"dependencies": {
    "@opentelemetry/instrumentation": "^0.47.0",
    "@opentelemetry/instrumentation-express": "^0.34.1",
    "@opentelemetry/instrumentation-http": "^0.47.0",
    "@opentelemetry/instrumentation-mongodb": "^0.38.1",
    "@opentelemetry/sdk-trace-base": "^1.20.0",
    "@opentelemetry/sdk-trace-node": "^1.20.0",
    "express": "^4.18.2",
    "mongodb": "^6.3.0"
  }

What did you expect to see?

see the mongodb traces produced

What did you see instead?

Nothing

Additional context

@jgonzalezhesstec jgonzalezhesstec added the bug Something isn't working label Jan 19, 2024
@pichlermarc pichlermarc added pkg:instrumentation-mongodb priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect labels Jan 24, 2024
@jgonzalezhesstec
Copy link
Author

Recently I have discovered that mongoDB instrumentation doesn't work when it haven't a parent span, doesn't matter what span type is. I used a manual span to instrument it and suddenly appeared.

@Flarna
Copy link
Member

Flarna commented Jan 30, 2024

That's not limited to mongodb. Most client instrumentations require a parent span. Some instrumentations like ioredis have a config option do change the behavior.

Main idea is that usually a service is triggered via request resulting in some server/consumer span which groups all the client requests. Background activities are quite often not of interest to monitor the service performance and sometimes result in a lot "span noise".

As a result this is not a bug, it was a design decision at instrumentation creation time.

I guess docs could be improved. Also adding a config option would be likely a good enhancement.

@Flarna Flarna added enhancement New feature or request and removed bug Something isn't working priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect labels Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants