autotel-mongoose 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,150 @@
1
+ # autotel-mongoose
2
+
3
+ OpenTelemetry instrumentation for Mongoose with stable semantic conventions, `db.query.text` capture, and default PII redaction.
4
+
5
+ ## What It Adds
6
+
7
+ - Captures `db.query.text` by default using `JSON.stringify`
8
+ - Redacts PII by default using Autotel's `'default'` redactor preset
9
+ - Supports custom `dbStatementSerializer` functions with the same payload shape as the OpenTelemetry MongoDB plugin
10
+ - Uses stable semantic conventions only:
11
+ - `db.system.name`
12
+ - `db.operation.name`
13
+ - `db.collection.name`
14
+ - `db.namespace`
15
+ - `db.query.text`
16
+ - `server.address`
17
+ - `server.port`
18
+ - Uses stable span names like `find users`
19
+
20
+ ## Installation
21
+
22
+ Install `autotel-mongoose`, `autotel`, and `mongoose`:
23
+
24
+ ```bash
25
+ npm install autotel autotel-mongoose mongoose
26
+ ```
27
+
28
+ This package supports Mongoose 8+.
29
+
30
+ ## Basic Usage
31
+
32
+ ```typescript
33
+ import mongoose from 'mongoose';
34
+ import { init } from 'autotel';
35
+ import { instrumentMongoose } from 'autotel-mongoose';
36
+
37
+ init({
38
+ service: 'my-app',
39
+ endpoint: 'http://localhost:4318',
40
+ });
41
+
42
+ instrumentMongoose(mongoose, {
43
+ dbName: 'myapp',
44
+ });
45
+
46
+ const userSchema = new mongoose.Schema({
47
+ name: String,
48
+ email: String,
49
+ });
50
+
51
+ const User = mongoose.model('User', userSchema);
52
+
53
+ await mongoose.connect(process.env.MONGODB_URI!);
54
+ await User.findOne({ email: 'alice@example.com' }).exec();
55
+ ```
56
+
57
+ ## Hook Instrumentation
58
+
59
+ Schema hook instrumentation is optional and disabled by default.
60
+
61
+ If you enable it, call `instrumentMongoose()` before defining schemas so `pre` and `post` hooks can be wrapped:
62
+
63
+ ```typescript
64
+ import mongoose from 'mongoose';
65
+ import { instrumentMongoose } from 'autotel-mongoose';
66
+
67
+ instrumentMongoose(mongoose, {
68
+ instrumentHooks: true,
69
+ });
70
+
71
+ const userSchema = new mongoose.Schema({
72
+ name: String,
73
+ });
74
+
75
+ userSchema.pre('save', async function () {
76
+ this.set('name', this.get('name')?.trim());
77
+ });
78
+ ```
79
+
80
+ ## Configuration
81
+
82
+ ```typescript
83
+ import type { InstrumentMongooseConfig } from 'autotel-mongoose';
84
+
85
+ const config: InstrumentMongooseConfig = {
86
+ dbName: 'myapp',
87
+ peerName: 'mongodb.internal',
88
+ peerPort: 27017,
89
+ tracerName: 'autotel-mongoose',
90
+ captureCollectionName: true,
91
+ instrumentHooks: false,
92
+ dbStatementSerializer: false,
93
+ statementRedactor: 'default',
94
+ };
95
+ ```
96
+
97
+ ## Statement Capture
98
+
99
+ By default, this package serializes query payloads into `db.query.text` and redacts sensitive values before they are added to the span.
100
+
101
+ You can disable statement capture entirely:
102
+
103
+ ```typescript
104
+ instrumentMongoose(mongoose, {
105
+ dbStatementSerializer: false,
106
+ });
107
+ ```
108
+
109
+ You can also provide a custom serializer:
110
+
111
+ ```typescript
112
+ instrumentMongoose(mongoose, {
113
+ dbStatementSerializer(operation, payload) {
114
+ return JSON.stringify({
115
+ operation,
116
+ condition: payload.condition,
117
+ updates: payload.updates,
118
+ });
119
+ },
120
+ });
121
+ ```
122
+
123
+ ## Redaction
124
+
125
+ PII redaction is enabled by default through Autotel's `'default'` preset.
126
+
127
+ You can provide a custom redactor config or disable redaction:
128
+
129
+ ```typescript
130
+ instrumentMongoose(mongoose, {
131
+ statementRedactor: false,
132
+ });
133
+ ```
134
+
135
+ ## Exported API
136
+
137
+ - `instrumentMongoose(mongoose, config?)`
138
+ - `InstrumentMongooseConfig`
139
+ - `SerializerPayload`
140
+
141
+ ## Notes
142
+
143
+ - Query and aggregate operations are traced automatically
144
+ - Instance methods like `save()` and `deleteOne()` are traced
145
+ - Static methods like `create()`, `insertMany()`, `aggregate()`, and `bulkWrite()` are traced
146
+ - Hook spans use `SpanKind.INTERNAL`
147
+
148
+ ## License
149
+
150
+ MIT