Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit 6d0106c

Browse files
author
Nir Hadassi
authored
feat: mongoose instrumentation (#63)
1 parent dbf57cd commit 6d0106c

File tree

16 files changed

+1102
-0
lines changed

16 files changed

+1102
-0
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ jobs:
1616
- name: Build
1717
run: yarn build
1818

19+
- name: Start MongoDB for unit tests
20+
run: docker run -d -p 27017:27017 -v ~/data:/data/db mongo
21+
1922
- name: Test
2023
run: yarn test
2124

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ js extensions for the [open-telemetry](https://opentelemetry.io/) project, from
2121
- [opentelemetry-instrumentation-typeorm](./packages/instrumentation-typeorm) - auto instrumentation for [`TypeORM`](https://typeorm.io/) [![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-typeorm.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-typeorm)
2222
- [opentelemetry-instrumentation-sequelize](./packages/instrumentation-sequelize) - auto instrumentation for [`Sequelize`](https://sequelize.org/)
2323
[![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-sequelize.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-sequelize)
24+
- [opentelemetry-instrumentation-mongoose](./packages/instrumentation-mongoose) - auto instrumentation for [`mongoose`](https://mongoosejs.com/)
25+
[![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-mongoose.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-mongoose)
2426

2527
## Compatibility with opentelemetry versions
2628
### Instrumentations in this repo are using opentelemetry [Instrumentation API](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-instrumentation).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './aws-sdk';
2+
export * from './types';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './kafkajs';
2+
export * from './types';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Copyright 2021 @ Aspecto
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# OpenTelemetry Mongoose Instrumentation for Node.js
2+
[![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-mongoose.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-mongoose)
3+
4+
This package is heavily based on [@wdalmut/opentelemetry-plugin-mongoose](https://github.com/wdalmut/opentelemetry-plugin-mongoose).
5+
This module provides automatic instrumentation for [`mongoose`](https://mongoosejs.com/) and follows otel [DB Semantic Conventions](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md).
6+
7+
## Installation
8+
9+
```
10+
npm install --save opentelemetry-instrumentation-mongoose
11+
```
12+
13+
## Usage
14+
For further automatic instrumentation instruction see the [@opentelemetry/instrumentation](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-instrumentation) package.
15+
16+
```js
17+
const { NodeTracerProvider } = require('@opentelemetry/node');
18+
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
19+
const { SequelizeInstrumentation } = require('opentelemetry-instrumentation-mongoose');
20+
21+
registerInstrumentations({
22+
traceProvider,
23+
instrumentations: [
24+
new MongooseInstrumentation({
25+
// see under for available configuration
26+
})
27+
]
28+
});
29+
```
30+
31+
### Mongoose Instrumentation Options
32+
33+
Mongoose instrumentation has few options available to choose from. You can set the following (all optional):
34+
35+
| Options | Type | Description |
36+
| -------------- | -------------------------------------- | ----------------------------------------------------------------------------------------------- |
37+
| `suppressInternalInstrumentation` | `boolean` | Mongoose operation use mongodb under the hood. Setting this to true will hide the underlying mongodb spans (if instrumented). |
38+
| `responseHook` | `MongooseResponseCustomAttributesFunction` | Hook called before response is returned, which allows to add custom attributes to span. |
39+
| `dbStatementSerializer` | `DbStatementSerializer` | Mongoose instrumentation will serialize `db.statement` using the specified function.
40+
41+
### Custom `db.statement` Serializer
42+
43+
By default, this instrumentation does not populate the `db.statement` attribute.
44+
If you pass `dbStatementSerializer` while using this plugin, the return value of the serializer will be placed in the `db.statement`.
45+
46+
Serializer meets the following interfaces:
47+
```ts
48+
interface SerializerPayload {
49+
condition?: any;
50+
options?: any;
51+
updates?: any;
52+
document?: any;
53+
aggregatePipeline?: any;
54+
}
55+
56+
type DbStatementSerializer = (operation: string, payload: SerializerPayload) => string;
57+
```
58+
Please make sure `dbStatementSerializer` is error proof, as errors are not handled while executing this function.
59+
60+
---
61+
62+
This extension (and many others) was developed by [Aspecto](https://www.aspecto.io/) with ❤️
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"name": "opentelemetry-instrumentation-mongoose",
3+
"version": "0.0.0",
4+
"description": "open telemetry instrumentation for the `mongoose` module",
5+
"keywords": [
6+
"mongoose",
7+
"mongodb",
8+
"opentelemetry"
9+
],
10+
"author": {
11+
"name": "Aspecto",
12+
"email": "[email protected]",
13+
"url": "https://aspecto.io"
14+
},
15+
"homepage": "https://github.com/aspecto-io/opentelemetry-ext-js",
16+
"license": "Apache-2.0",
17+
"main": "dist/src/index.js",
18+
"files": [
19+
"dist/**/*.js",
20+
"dist/**/*.js.map",
21+
"dist/**/*.d.ts",
22+
"LICENSE",
23+
"README.md"
24+
],
25+
"repository": {
26+
"type": "git",
27+
"url": "https://github.com/aspecto-io/opentelemetry-ext-js.git"
28+
},
29+
"scripts": {
30+
"build": "tsc",
31+
"prepare": "yarn run build",
32+
"test": "mocha",
33+
"watch": "tsc -w",
34+
"version:update": "node ../../scripts/version-update.js",
35+
"version": "yarn run version:update"
36+
},
37+
"bugs": {
38+
"url": "https://github.com/aspecto-io/opentelemetry-ext-js/issues"
39+
},
40+
"dependencies": {
41+
"@opentelemetry/api": "^0.15.0",
42+
"@opentelemetry/instrumentation": "^0.15.0",
43+
"@opentelemetry/semantic-conventions": "^0.15.0"
44+
},
45+
"devDependencies": {
46+
"@opentelemetry/node": "^0.15.0",
47+
"@opentelemetry/tracing": "^0.15.0",
48+
"@types/mocha": "^8.2.0",
49+
"expect": "^26.6.2",
50+
"mocha": "^8.3.0",
51+
"mongodb": "^3.6.4",
52+
"mongoose": "5.11.15",
53+
"ts-node": "^9.1.1",
54+
"typescript": "^4.0.3"
55+
},
56+
"mocha": {
57+
"extension": [
58+
"ts"
59+
],
60+
"spec": "test/**/*.spec.ts",
61+
"require": "ts-node/register"
62+
}
63+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './mongoose';
2+
export * from './types';

0 commit comments

Comments
 (0)