@opentelemetry/instrumentation-mongoose 0.31.0
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/LICENSE +201 -0
- package/README.md +69 -0
- package/build/src/index.d.ts +3 -0
- package/build/src/index.js +30 -0
- package/build/src/index.js.map +1 -0
- package/build/src/mongoose.d.ts +21 -0
- package/build/src/mongoose.js +216 -0
- package/build/src/mongoose.js.map +1 -0
- package/build/src/types.d.ts +34 -0
- package/build/src/types.js +3 -0
- package/build/src/types.js.map +1 -0
- package/build/src/utils.d.ts +7 -0
- package/build/src/utils.js +79 -0
- package/build/src/utils.js.map +1 -0
- package/build/src/version.d.ts +2 -0
- package/build/src/version.js +21 -0
- package/build/src/version.js.map +1 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [2022] OpenTelemetry Authors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# OpenTelemetry mongoose Instrumentation for Node.js
|
|
2
|
+
|
|
3
|
+
[![NPM Published Version][npm-img]][npm-url]
|
|
4
|
+
[![Apache License][license-image]][license-image]
|
|
5
|
+
|
|
6
|
+
This module provides automatic instrumentation for the [`mongoose`](https://github.com/Automattic/mongoose) module, which may be loaded using the [`@opentelemetry/sdk-trace-node`](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-sdk-trace-node) package and is included in the [`@opentelemetry/auto-instrumentations-node`](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-node) bundle.
|
|
7
|
+
|
|
8
|
+
If total installation size is not constrained, it is recommended to use the [`@opentelemetry/auto-instrumentations-node`](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-node) bundle with [@opentelemetry/sdk-node](`https://www.npmjs.com/package/@opentelemetry/sdk-node`) for the most seamless instrumentation experience.
|
|
9
|
+
|
|
10
|
+
Compatible with OpenTelemetry JS API and SDK `1.0+`.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install --save @opentelemetry/instrumentation-mongoose
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Supported Versions
|
|
19
|
+
|
|
20
|
+
- `>=5.9.7 <7`
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
To load a specific plugin, specify it in the registerInstrumentations's configuration:
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
|
|
28
|
+
const { MongooseInstrumentation } = require('@opentelemetry/instrumentation-mongoose');
|
|
29
|
+
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
|
|
30
|
+
|
|
31
|
+
const provider = new NodeTracerProvider();
|
|
32
|
+
provider.register();
|
|
33
|
+
|
|
34
|
+
registerInstrumentations({
|
|
35
|
+
instrumentations: [
|
|
36
|
+
new MongooseInstrumentation(),
|
|
37
|
+
],
|
|
38
|
+
})
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Migration From opentelemetry-instrumentation-mongoose
|
|
42
|
+
|
|
43
|
+
This instrumentation was originally published and maintained under the name `"opentelemetry-instrumentation-mongoose"` in [this repo](https://github.com/aspecto-io/opentelemetry-ext-js).
|
|
44
|
+
|
|
45
|
+
Few breaking changes were made during porting to the contrib repo to align with conventions:
|
|
46
|
+
|
|
47
|
+
### Hook Info
|
|
48
|
+
|
|
49
|
+
The instrumentation's config `responseHook` functions signature changed, so the second function parameter is info object, containing the relevant hook data.
|
|
50
|
+
|
|
51
|
+
### `moduleVersionAttributeName` config option
|
|
52
|
+
|
|
53
|
+
The `moduleVersionAttributeName` config option is removed. To add the mongoose package version to spans, use the `moduleVersion` attribute in hook info for `responseHook` function.
|
|
54
|
+
|
|
55
|
+
## Useful links
|
|
56
|
+
|
|
57
|
+
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
|
|
58
|
+
- For more about OpenTelemetry JavaScript: <https://github.com/open-telemetry/opentelemetry-js>
|
|
59
|
+
- For help or feedback on this project, join us in [GitHub Discussions][discussions-url]
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
Apache 2.0 - See [LICENSE][license-url] for more information.
|
|
64
|
+
|
|
65
|
+
[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
|
|
66
|
+
[license-url]: https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/LICENSE
|
|
67
|
+
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
|
|
68
|
+
[npm-url]: https://www.npmjs.com/package/@opentelemetry/instrumentation-mongoose
|
|
69
|
+
[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Finstrumentation-mongoose.svg
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
/*
|
|
14
|
+
* Copyright The OpenTelemetry Authors
|
|
15
|
+
*
|
|
16
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
17
|
+
* you may not use this file except in compliance with the License.
|
|
18
|
+
* You may obtain a copy of the License at
|
|
19
|
+
*
|
|
20
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
21
|
+
*
|
|
22
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
23
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
24
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
25
|
+
* See the License for the specific language governing permissions and
|
|
26
|
+
* limitations under the License.
|
|
27
|
+
*/
|
|
28
|
+
__exportStar(require("./mongoose"), exports);
|
|
29
|
+
__exportStar(require("./types"), exports);
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA;;;;;;;;;;;;;;GAcG;AACH,6CAA2B;AAC3B,0CAAwB"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type * as mongoose from 'mongoose';
|
|
2
|
+
import { MongooseInstrumentationConfig } from './types';
|
|
3
|
+
import { InstrumentationBase, InstrumentationModuleDefinition } from '@opentelemetry/instrumentation';
|
|
4
|
+
export declare const _STORED_PARENT_SPAN: unique symbol;
|
|
5
|
+
export declare class MongooseInstrumentation extends InstrumentationBase<any> {
|
|
6
|
+
protected _config: MongooseInstrumentationConfig;
|
|
7
|
+
constructor(config?: MongooseInstrumentationConfig);
|
|
8
|
+
setConfig(config?: MongooseInstrumentationConfig): void;
|
|
9
|
+
protected init(): InstrumentationModuleDefinition<any>;
|
|
10
|
+
protected patch(moduleExports: typeof mongoose, moduleVersion: string | undefined): typeof mongoose;
|
|
11
|
+
protected unpatch(moduleExports: typeof mongoose): void;
|
|
12
|
+
private patchAggregateExec;
|
|
13
|
+
private patchQueryExec;
|
|
14
|
+
private patchOnModelMethods;
|
|
15
|
+
private patchModelAggregate;
|
|
16
|
+
private patchAndCaptureSpanContext;
|
|
17
|
+
private _startSpan;
|
|
18
|
+
private _handleResponse;
|
|
19
|
+
private _callOriginalFunction;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=mongoose.d.ts.map
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MongooseInstrumentation = exports._STORED_PARENT_SPAN = void 0;
|
|
4
|
+
/*
|
|
5
|
+
* Copyright The OpenTelemetry Authors
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
const api_1 = require("@opentelemetry/api");
|
|
20
|
+
const core_1 = require("@opentelemetry/core");
|
|
21
|
+
const utils_1 = require("./utils");
|
|
22
|
+
const instrumentation_1 = require("@opentelemetry/instrumentation");
|
|
23
|
+
const version_1 = require("./version");
|
|
24
|
+
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
25
|
+
const contextCaptureFunctions = [
|
|
26
|
+
'remove',
|
|
27
|
+
'deleteOne',
|
|
28
|
+
'deleteMany',
|
|
29
|
+
'find',
|
|
30
|
+
'findOne',
|
|
31
|
+
'estimatedDocumentCount',
|
|
32
|
+
'countDocuments',
|
|
33
|
+
'count',
|
|
34
|
+
'distinct',
|
|
35
|
+
'where',
|
|
36
|
+
'$where',
|
|
37
|
+
'findOneAndUpdate',
|
|
38
|
+
'findOneAndDelete',
|
|
39
|
+
'findOneAndReplace',
|
|
40
|
+
'findOneAndRemove',
|
|
41
|
+
];
|
|
42
|
+
// when mongoose functions are called, we store the original call context
|
|
43
|
+
// and then set it as the parent for the spans created by Query/Aggregate exec()
|
|
44
|
+
// calls. this bypass the unlinked spans issue on thenables await operations.
|
|
45
|
+
exports._STORED_PARENT_SPAN = Symbol('stored-parent-span');
|
|
46
|
+
class MongooseInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
47
|
+
constructor(config = {}) {
|
|
48
|
+
super('@opentelemetry/instrumentation-mongoose', version_1.VERSION, Object.assign({}, config));
|
|
49
|
+
}
|
|
50
|
+
setConfig(config = {}) {
|
|
51
|
+
this._config = Object.assign({}, config);
|
|
52
|
+
}
|
|
53
|
+
init() {
|
|
54
|
+
const module = new instrumentation_1.InstrumentationNodeModuleDefinition('mongoose', ['>=5.9.7 <7'], this.patch.bind(this), this.unpatch.bind(this));
|
|
55
|
+
return module;
|
|
56
|
+
}
|
|
57
|
+
patch(moduleExports, moduleVersion) {
|
|
58
|
+
this._wrap(moduleExports.Model.prototype, 'save', this.patchOnModelMethods('save', moduleVersion));
|
|
59
|
+
// mongoose applies this code on moudle require:
|
|
60
|
+
// Model.prototype.$save = Model.prototype.save;
|
|
61
|
+
// which captures the save function before it is patched.
|
|
62
|
+
// so we need to apply the same logic after instrumenting the save function.
|
|
63
|
+
moduleExports.Model.prototype.$save = moduleExports.Model.prototype.save;
|
|
64
|
+
this._wrap(moduleExports.Model.prototype, 'remove', this.patchOnModelMethods('remove', moduleVersion));
|
|
65
|
+
this._wrap(moduleExports.Query.prototype, 'exec', this.patchQueryExec(moduleVersion));
|
|
66
|
+
this._wrap(moduleExports.Aggregate.prototype, 'exec', this.patchAggregateExec(moduleVersion));
|
|
67
|
+
contextCaptureFunctions.forEach((funcName) => {
|
|
68
|
+
this._wrap(moduleExports.Query.prototype, funcName, this.patchAndCaptureSpanContext(funcName));
|
|
69
|
+
});
|
|
70
|
+
this._wrap(moduleExports.Model, 'aggregate', this.patchModelAggregate());
|
|
71
|
+
return moduleExports;
|
|
72
|
+
}
|
|
73
|
+
unpatch(moduleExports) {
|
|
74
|
+
this._diag.debug('mongoose instrumentation: unpatch mongoose');
|
|
75
|
+
this._unwrap(moduleExports.Model.prototype, 'save');
|
|
76
|
+
// revert the patch for $save which we applyed by aliasing it to patched `save`
|
|
77
|
+
moduleExports.Model.prototype.$save = moduleExports.Model.prototype.save;
|
|
78
|
+
this._unwrap(moduleExports.Model.prototype, 'remove');
|
|
79
|
+
this._unwrap(moduleExports.Query.prototype, 'exec');
|
|
80
|
+
this._unwrap(moduleExports.Aggregate.prototype, 'exec');
|
|
81
|
+
contextCaptureFunctions.forEach((funcName) => {
|
|
82
|
+
this._unwrap(moduleExports.Query.prototype, funcName);
|
|
83
|
+
});
|
|
84
|
+
this._unwrap(moduleExports.Model, 'aggregate');
|
|
85
|
+
}
|
|
86
|
+
patchAggregateExec(moduleVersion) {
|
|
87
|
+
const self = this;
|
|
88
|
+
this._diag.debug('patched mongoose Aggregate exec function');
|
|
89
|
+
return (originalAggregate) => {
|
|
90
|
+
return function exec(callback) {
|
|
91
|
+
var _a;
|
|
92
|
+
if (self._config.requireParentSpan &&
|
|
93
|
+
api_1.trace.getSpan(api_1.context.active()) === undefined) {
|
|
94
|
+
return originalAggregate.apply(this, arguments);
|
|
95
|
+
}
|
|
96
|
+
const parentSpan = this[exports._STORED_PARENT_SPAN];
|
|
97
|
+
const attributes = {};
|
|
98
|
+
if (self._config.dbStatementSerializer) {
|
|
99
|
+
attributes[semantic_conventions_1.SemanticAttributes.DB_STATEMENT] =
|
|
100
|
+
self._config.dbStatementSerializer('aggregate', {
|
|
101
|
+
options: this.options,
|
|
102
|
+
aggregatePipeline: this._pipeline,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
const span = self._startSpan(this._model.collection, (_a = this._model) === null || _a === void 0 ? void 0 : _a.modelName, 'aggregate', attributes, parentSpan);
|
|
106
|
+
return self._handleResponse(span, originalAggregate, this, arguments, callback, moduleVersion);
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
patchQueryExec(moduleVersion) {
|
|
111
|
+
const self = this;
|
|
112
|
+
this._diag.debug('patched mongoose Query exec function');
|
|
113
|
+
return (originalExec) => {
|
|
114
|
+
return function exec(callback) {
|
|
115
|
+
if (self._config.requireParentSpan &&
|
|
116
|
+
api_1.trace.getSpan(api_1.context.active()) === undefined) {
|
|
117
|
+
return originalExec.apply(this, arguments);
|
|
118
|
+
}
|
|
119
|
+
const parentSpan = this[exports._STORED_PARENT_SPAN];
|
|
120
|
+
const attributes = {};
|
|
121
|
+
if (self._config.dbStatementSerializer) {
|
|
122
|
+
attributes[semantic_conventions_1.SemanticAttributes.DB_STATEMENT] =
|
|
123
|
+
self._config.dbStatementSerializer(this.op, {
|
|
124
|
+
condition: this._conditions,
|
|
125
|
+
updates: this._update,
|
|
126
|
+
options: this.options,
|
|
127
|
+
fields: this._fields,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
const span = self._startSpan(this.mongooseCollection, this.model.modelName, this.op, attributes, parentSpan);
|
|
131
|
+
return self._handleResponse(span, originalExec, this, arguments, callback, moduleVersion);
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
patchOnModelMethods(op, moduleVersion) {
|
|
136
|
+
const self = this;
|
|
137
|
+
this._diag.debug(`patching mongoose Model '${op}' operation`);
|
|
138
|
+
return (originalOnModelFunction) => {
|
|
139
|
+
return function method(options, callback) {
|
|
140
|
+
if (self._config.requireParentSpan &&
|
|
141
|
+
api_1.trace.getSpan(api_1.context.active()) === undefined) {
|
|
142
|
+
return originalOnModelFunction.apply(this, arguments);
|
|
143
|
+
}
|
|
144
|
+
const serializePayload = { document: this };
|
|
145
|
+
if (options && !(options instanceof Function)) {
|
|
146
|
+
serializePayload.options = options;
|
|
147
|
+
}
|
|
148
|
+
const attributes = {};
|
|
149
|
+
if (self._config.dbStatementSerializer) {
|
|
150
|
+
attributes[semantic_conventions_1.SemanticAttributes.DB_STATEMENT] =
|
|
151
|
+
self._config.dbStatementSerializer(op, serializePayload);
|
|
152
|
+
}
|
|
153
|
+
const span = self._startSpan(this.constructor.collection, this.constructor.modelName, op, attributes);
|
|
154
|
+
if (options instanceof Function) {
|
|
155
|
+
callback = options;
|
|
156
|
+
options = undefined;
|
|
157
|
+
}
|
|
158
|
+
return self._handleResponse(span, originalOnModelFunction, this, arguments, callback, moduleVersion);
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
// we want to capture the otel span on the object which is calling exec.
|
|
163
|
+
// in the special case of aggregate, we need have no function to path
|
|
164
|
+
// on the Aggregate object to capture the context on, so we patch
|
|
165
|
+
// the aggregate of Model, and set the context on the Aggregate object
|
|
166
|
+
patchModelAggregate() {
|
|
167
|
+
const self = this;
|
|
168
|
+
this._diag.debug('patched mongoose model aggregate function');
|
|
169
|
+
return (original) => {
|
|
170
|
+
return function captureSpanContext() {
|
|
171
|
+
const currentSpan = api_1.trace.getSpan(api_1.context.active());
|
|
172
|
+
const aggregate = self._callOriginalFunction(() => original.apply(this, arguments));
|
|
173
|
+
if (aggregate)
|
|
174
|
+
aggregate[exports._STORED_PARENT_SPAN] = currentSpan;
|
|
175
|
+
return aggregate;
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
patchAndCaptureSpanContext(funcName) {
|
|
180
|
+
const self = this;
|
|
181
|
+
this._diag.debug(`patching mongoose query ${funcName} function`);
|
|
182
|
+
return (original) => {
|
|
183
|
+
return function captureSpanContext() {
|
|
184
|
+
this[exports._STORED_PARENT_SPAN] = api_1.trace.getSpan(api_1.context.active());
|
|
185
|
+
return self._callOriginalFunction(() => original.apply(this, arguments));
|
|
186
|
+
};
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
_startSpan(collection, modelName, operation, attributes, parentSpan) {
|
|
190
|
+
return this.tracer.startSpan(`mongoose.${modelName}.${operation}`, {
|
|
191
|
+
kind: api_1.SpanKind.CLIENT,
|
|
192
|
+
attributes: Object.assign(Object.assign(Object.assign({}, attributes), utils_1.getAttributesFromCollection(collection)), { [semantic_conventions_1.SemanticAttributes.DB_OPERATION]: operation, [semantic_conventions_1.SemanticAttributes.DB_SYSTEM]: 'mongoose' }),
|
|
193
|
+
}, parentSpan ? api_1.trace.setSpan(api_1.context.active(), parentSpan) : undefined);
|
|
194
|
+
}
|
|
195
|
+
_handleResponse(span, exec, originalThis, args, callback, moduleVersion = undefined) {
|
|
196
|
+
const self = this;
|
|
197
|
+
if (callback instanceof Function) {
|
|
198
|
+
return self._callOriginalFunction(() => utils_1.handleCallbackResponse(callback, exec, originalThis, span, self._config.responseHook, moduleVersion));
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
const response = self._callOriginalFunction(() => exec.apply(originalThis, args));
|
|
202
|
+
return utils_1.handlePromiseResponse(response, span, self._config.responseHook, moduleVersion);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
_callOriginalFunction(originalFunction) {
|
|
206
|
+
var _a;
|
|
207
|
+
if ((_a = this._config) === null || _a === void 0 ? void 0 : _a.suppressInternalInstrumentation) {
|
|
208
|
+
return api_1.context.with(core_1.suppressTracing(api_1.context.active()), originalFunction);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
return originalFunction();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
exports.MongooseInstrumentation = MongooseInstrumentation;
|
|
216
|
+
//# sourceMappingURL=mongoose.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongoose.js","sourceRoot":"","sources":["../../src/mongoose.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;GAcG;AACH,4CAM4B;AAC5B,8CAAsD;AAGtD,mCAIiB;A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D,OAAO,aAAO,CAAC,IAAI,CAAC,sBAAe,CAAC,aAAO,CAAC,MAAM,EAAE,CAAC,EAAE,gBAAgB,CAAC,CAAC;SAC1E;aAAM;YACL,OAAO,gBAAgB,EAAE,CAAC;SAC3B;IACH,CAAC;CACF;AAnTD,0DAmTC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Span } from '@opentelemetry/api';
|
|
2
|
+
import { InstrumentationConfig } from '@opentelemetry/instrumentation';
|
|
3
|
+
export interface SerializerPayload {
|
|
4
|
+
condition?: any;
|
|
5
|
+
options?: any;
|
|
6
|
+
updates?: any;
|
|
7
|
+
document?: any;
|
|
8
|
+
aggregatePipeline?: any;
|
|
9
|
+
fields?: any;
|
|
10
|
+
}
|
|
11
|
+
export declare type DbStatementSerializer = (operation: string, payload: SerializerPayload) => string;
|
|
12
|
+
export interface ResponseInfo {
|
|
13
|
+
moduleVersion: string | undefined;
|
|
14
|
+
response: any;
|
|
15
|
+
}
|
|
16
|
+
export declare type MongooseResponseCustomAttributesFunction = (span: Span, responseInfo: ResponseInfo) => void;
|
|
17
|
+
export interface MongooseInstrumentationConfig extends InstrumentationConfig {
|
|
18
|
+
/**
|
|
19
|
+
* Mongoose operation use mongodb under the hood.
|
|
20
|
+
* If mongodb instrumentation is enabled, a mongoose operation will also create
|
|
21
|
+
* a mongodb operation describing the communication with mongoDB servers.
|
|
22
|
+
* Setting the `suppressInternalInstrumentation` config value to `true` will
|
|
23
|
+
* cause the instrumentation to suppress instrumentation of underlying operations,
|
|
24
|
+
* effectively causing mongodb spans to be non-recordable.
|
|
25
|
+
*/
|
|
26
|
+
suppressInternalInstrumentation?: boolean;
|
|
27
|
+
/** Custom serializer function for the db.statement tag */
|
|
28
|
+
dbStatementSerializer?: DbStatementSerializer;
|
|
29
|
+
/** hook for adding custom attributes using the response payload */
|
|
30
|
+
responseHook?: MongooseResponseCustomAttributesFunction;
|
|
31
|
+
/** Set to true if you do not want to collect traces that start with mongoose */
|
|
32
|
+
requireParentSpan?: boolean;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { SpanAttributes, Span } from '@opentelemetry/api';
|
|
2
|
+
import type { Collection } from 'mongoose';
|
|
3
|
+
import { MongooseResponseCustomAttributesFunction } from './types';
|
|
4
|
+
export declare function getAttributesFromCollection(collection: Collection): SpanAttributes;
|
|
5
|
+
export declare function handlePromiseResponse(execResponse: any, span: Span, responseHook?: MongooseResponseCustomAttributesFunction, moduleVersion?: string | undefined): any;
|
|
6
|
+
export declare function handleCallbackResponse(callback: Function, exec: Function, originalThis: any, span: Span, responseHook?: MongooseResponseCustomAttributesFunction, moduleVersion?: string | undefined): any;
|
|
7
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handleCallbackResponse = exports.handlePromiseResponse = exports.getAttributesFromCollection = void 0;
|
|
4
|
+
/*
|
|
5
|
+
* Copyright The OpenTelemetry Authors
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
const api_1 = require("@opentelemetry/api");
|
|
20
|
+
const instrumentation_1 = require("@opentelemetry/instrumentation");
|
|
21
|
+
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
22
|
+
function getAttributesFromCollection(collection) {
|
|
23
|
+
return {
|
|
24
|
+
[semantic_conventions_1.SemanticAttributes.DB_MONGODB_COLLECTION]: collection.name,
|
|
25
|
+
[semantic_conventions_1.SemanticAttributes.DB_NAME]: collection.conn.name,
|
|
26
|
+
[semantic_conventions_1.SemanticAttributes.DB_USER]: collection.conn.user,
|
|
27
|
+
[semantic_conventions_1.SemanticAttributes.NET_PEER_NAME]: collection.conn.host,
|
|
28
|
+
[semantic_conventions_1.SemanticAttributes.NET_PEER_PORT]: collection.conn.port,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
exports.getAttributesFromCollection = getAttributesFromCollection;
|
|
32
|
+
function setErrorStatus(span, error = {}) {
|
|
33
|
+
span.recordException(error);
|
|
34
|
+
span.setStatus({
|
|
35
|
+
code: api_1.SpanStatusCode.ERROR,
|
|
36
|
+
message: `${error.message} ${error.code ? `\nMongoose Error Code: ${error.code}` : ''}`,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function applyResponseHook(span, response, responseHook, moduleVersion = undefined) {
|
|
40
|
+
if (!responseHook) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
instrumentation_1.safeExecuteInTheMiddle(() => responseHook(span, { moduleVersion, response }), e => {
|
|
44
|
+
if (e) {
|
|
45
|
+
api_1.diag.error('mongoose instrumentation: responseHook error', e);
|
|
46
|
+
}
|
|
47
|
+
}, true);
|
|
48
|
+
}
|
|
49
|
+
function handlePromiseResponse(execResponse, span, responseHook, moduleVersion = undefined) {
|
|
50
|
+
if (!(execResponse instanceof Promise)) {
|
|
51
|
+
applyResponseHook(span, execResponse, responseHook, moduleVersion);
|
|
52
|
+
span.end();
|
|
53
|
+
return execResponse;
|
|
54
|
+
}
|
|
55
|
+
return execResponse
|
|
56
|
+
.then(response => {
|
|
57
|
+
applyResponseHook(span, response, responseHook, moduleVersion);
|
|
58
|
+
return response;
|
|
59
|
+
})
|
|
60
|
+
.catch(err => {
|
|
61
|
+
setErrorStatus(span, err);
|
|
62
|
+
throw err;
|
|
63
|
+
})
|
|
64
|
+
.finally(() => span.end());
|
|
65
|
+
}
|
|
66
|
+
exports.handlePromiseResponse = handlePromiseResponse;
|
|
67
|
+
function handleCallbackResponse(callback, exec, originalThis, span, responseHook, moduleVersion = undefined) {
|
|
68
|
+
return exec.apply(originalThis, [
|
|
69
|
+
(err, response) => {
|
|
70
|
+
err
|
|
71
|
+
? setErrorStatus(span, err)
|
|
72
|
+
: applyResponseHook(span, response, responseHook, moduleVersion);
|
|
73
|
+
span.end();
|
|
74
|
+
return callback(err, response);
|
|
75
|
+
},
|
|
76
|
+
]);
|
|
77
|
+
}
|
|
78
|
+
exports.handleCallbackResponse = handleCallbackResponse;
|
|
79
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;GAcG;AACH,4CAAgF;AAGhF,oEAAwE;AACxE,8EAAyE;AAEzE,SAAgB,2BAA2B,CACzC,UAAsB;IAEtB,OAAO;QACL,CAAC,yCAAkB,CAAC,qBAAqB,CAAC,EAAE,UAAU,CAAC,IAAI;QAC3D,CAAC,yCAAkB,CAAC,OAAO,CAAC,EAAE,UAAU,CAAC,IAAI,CAAC,IAAI;QAClD,CAAC,yCAAkB,CAAC,OAAO,CAAC,EAAE,UAAU,CAAC,IAAI,CAAC,IAAI;QAClD,CAAC,yCAAkB,CAAC,aAAa,CAAC,EAAE,UAAU,CAAC,IAAI,CAAC,IAAI;QACxD,CAAC,yCAAkB,CAAC,aAAa,CAAC,EAAE,UAAU,CAAC,IAAI,CAAC,IAAI;KACzD,CAAC;AACJ,CAAC;AAVD,kEAUC;AAED,SAAS,cAAc,CAAC,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MAAM,GAAG,CAAC;IACZ,CAAC,CAAC;SACD,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;AAC/B,CAAC;AAtBD,sDAsBC;AAED,SAAgB,sBAAsB,CACpC,QAAkB,EAClB,IAAc,EACd,YAAiB,EACjB,IAAU,EACV,YAAuD,EACvD,gBAAoC,SAAS;IAE7C,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,EAAE;QAC9B,CAAC,GAAU,EAAE,QAAa,EAAE,EAAE;YAC5B,GAAG;gBACD,CAAC,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC;gBAC3B,CAAC,CAAC,iBAAiB,CAAC,IAAI,EAAE,QAAQ,EAAE,YAAY,EAAE,aAAa,CAAC,CAAC;YACnE,IAAI,CAAC,GAAG,EAAE,CAAC;YACX,OAAO,QAAS,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;QAClC,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAjBD,wDAiBC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright The OpenTelemetry Authors
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.VERSION = void 0;
|
|
19
|
+
// this is autogenerated file, see scripts/version-update.js
|
|
20
|
+
exports.VERSION = '0.31.0';
|
|
21
|
+
//# sourceMappingURL=version.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,4DAA4D;AAC/C,QAAA,OAAO,GAAG,QAAQ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@opentelemetry/instrumentation-mongoose",
|
|
3
|
+
"version": "0.31.0",
|
|
4
|
+
"description": "OpenTelemetry automatic instrumentation package for mongoose",
|
|
5
|
+
"main": "build/src/index.js",
|
|
6
|
+
"types": "build/src/index.d.ts",
|
|
7
|
+
"repository": "open-telemetry/opentelemetry-js-contrib",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"docker:start": "docker run -e MONGODB_DB=opentelemetry-tests -e MONGODB_PORT=27017 -e MONGODB_HOST=127.0.0.1 -p 27017:27017 --rm mongo",
|
|
10
|
+
"test": "ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/*.test.ts'",
|
|
11
|
+
"test-all-versions": "tav",
|
|
12
|
+
"tdd": "npm run test -- --watch-extensions ts --watch",
|
|
13
|
+
"clean": "rimraf build/*",
|
|
14
|
+
"lint": "eslint . --ext .ts",
|
|
15
|
+
"lint:fix": "eslint . --ext .ts --fix",
|
|
16
|
+
"precompile": "tsc --version && lerna run version --scope @opentelemetry/instrumentation-mongoose --include-dependencies",
|
|
17
|
+
"prewatch": "npm run precompile",
|
|
18
|
+
"prepare": "npm run compile",
|
|
19
|
+
"version:update": "node ../../../scripts/version-update.js",
|
|
20
|
+
"compile": "npm run version:update && tsc -p ."
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"mongodb",
|
|
24
|
+
"mongoose",
|
|
25
|
+
"orm",
|
|
26
|
+
"instrumentation",
|
|
27
|
+
"nodejs",
|
|
28
|
+
"opentelemetry",
|
|
29
|
+
"tracing"
|
|
30
|
+
],
|
|
31
|
+
"author": "OpenTelemetry Authors",
|
|
32
|
+
"license": "Apache-2.0",
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=14.0"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"build/src/**/*.js",
|
|
38
|
+
"build/src/**/*.js.map",
|
|
39
|
+
"build/src/**/*.d.ts"
|
|
40
|
+
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@opentelemetry/api": "^1.0.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@opentelemetry/api": "^1.0.0",
|
|
49
|
+
"@opentelemetry/contrib-test-utils": "^0.32.0",
|
|
50
|
+
"@opentelemetry/sdk-trace-base": "^1.3.1",
|
|
51
|
+
"@types/mocha": "8.2.3",
|
|
52
|
+
"@types/node": "16.11.21",
|
|
53
|
+
"expect": "27.4.2",
|
|
54
|
+
"gts": "3.1.0",
|
|
55
|
+
"mocha": "7.2.0",
|
|
56
|
+
"mongoose": "6.5.2",
|
|
57
|
+
"nyc": "15.1.0",
|
|
58
|
+
"rimraf": "3.0.2",
|
|
59
|
+
"test-all-versions": "5.0.1",
|
|
60
|
+
"ts-mocha": "10.0.0",
|
|
61
|
+
"typescript": "4.3.5"
|
|
62
|
+
},
|
|
63
|
+
"dependencies": {
|
|
64
|
+
"@opentelemetry/core": "^1.0.0",
|
|
65
|
+
"@opentelemetry/instrumentation": "^0.32.0",
|
|
66
|
+
"@opentelemetry/semantic-conventions": "^1.0.0"
|
|
67
|
+
},
|
|
68
|
+
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-mongoose#readme",
|
|
69
|
+
"gitHead": "35226602b92a7587f16a1eb959e4f3b3948f6e9d"
|
|
70
|
+
}
|