Skip to content

Mongoose

autotel-mongoose instruments Mongoose 8+ with stable semantic conventions, query text capture, and default PII redaction.

Terminal window
npm install autotel autotel-mongoose mongoose
import mongoose from 'mongoose';
import { init } from 'autotel';
import { instrumentMongoose } from 'autotel-mongoose';
init({ service: 'my-app', endpoint: 'http://localhost:4318' });
instrumentMongoose(mongoose, { dbName: 'myapp' });
const User = mongoose.model(
'User',
new mongoose.Schema({ name: String, email: String }),
);
await User.findOne({ email: 'alice@example.com' }).exec();
// Span: "find users" with db.query.text (PII redacted)
instrumentMongoose(mongoose, { instrumentHooks: true });
userSchema.pre('save', async function () {
this.set('name', this.get('name')?.trim());
// Pre/post hooks get their own spans
});
OptionDefaultDescription
dbNameDatabase name
peerNameMongoDB host
peerPortMongoDB port
captureCollectionNametrueInclude collection in span
instrumentHooksfalseWrap pre/post hooks
dbStatementSerializerbuilt-inCustom query serializer
statementRedactor'default'PII redaction preset
  • example-mongoose — Mongoose with query text capture, PII redaction, and hook instrumentation.

Uses stable OTel conventions only:

  • db.system.name
  • db.operation.name
  • db.collection.name
  • db.namespace
  • db.query.text
  • server.address / server.port