Mongoose
autotel-mongoose instruments Mongoose 8+ with stable semantic conventions, query text capture, and default PII redaction.
Installation
Section titled “Installation”npm install autotel autotel-mongoose mongooseBasic Usage
Section titled “Basic Usage”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)Hook Instrumentation
Section titled “Hook Instrumentation”instrumentMongoose(mongoose, { instrumentHooks: true });
userSchema.pre('save', async function () { this.set('name', this.get('name')?.trim()); // Pre/post hooks get their own spans});Configuration
Section titled “Configuration”| Option | Default | Description |
| ----------------------- | ----------- | -------------------------- |
| dbName | — | Database name |
| peerName | — | MongoDB host |
| peerPort | — | MongoDB port |
| captureCollectionName | true | Include collection in span |
| instrumentHooks | false | Wrap pre/post hooks |
| dbStatementSerializer | built-in | Custom query serializer |
| statementRedactor | 'default' | PII redaction preset |
Examples
Section titled “Examples”example-mongoose— Mongoose with query text capture, PII redaction, and hook instrumentation.
Semantic Conventions
Section titled “Semantic Conventions”Uses stable OTel conventions only:
db.system.namedb.operation.namedb.collection.namedb.namespacedb.query.textserver.address/server.port