Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node Mongoose Plugin #44

Merged
merged 3 commits into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Library | Plugin Name
| [`PostgreSQL`](https://github.com/brianc/node-postgres) | `pg` |
| [`pg-cursor`](https://github.com/brianc/node-postgres) | `pg-cursor` |
| [`MongoDB`](https://github.com/mongodb/node-mongodb-native) | `mongodb` |
| [`Mongoose`](https://github.com/Automattic/mongoose) | `mongoose` |
| [`RabbitMQ`](https://github.com/squaremo/amqp.node) | `amqplib` |

### Compatible Libraries
Expand All @@ -88,7 +89,6 @@ Library | Underlying Plugin Name
| [`request`](https://github.com/request/request) | `http` / `https` |
| [`request-promise`](https://github.com/request/request-promise) | `http` / `https` |
| [`koa`](https://github.com/koajs/koa) | `http` / `https` |
| [`mongoose`](https://github.com/Automattic/mongoose) | `mongodb` |

## Contact Us
* Submit [an issue](https://github.com/apache/skywalking/issues/new) by using [Nodejs] as title prefix.
Expand Down
125 changes: 125 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@
"amqplib": "^0.7.0",
"axios": "^0.21.0",
"express": "^4.17.1",
"grpc-tools": "^1.10.0",
"grpc_tools_node_protoc_ts": "^4.0.0",
"grpc-tools": "^1.10.0",
"jest": "^26.6.3",
"mongodb": "^3.6.4",
"mongoose": "^5.12.2",
"mysql": "^2.18.1",
"pg": "^8.5.1",
"prettier": "^2.0.5",
Expand Down
2 changes: 1 addition & 1 deletion src/config/AgentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type AgentConfig = {
export function finalizeConfig(config: AgentConfig): void {
const escapeRegExp = (s: string) => s.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");

config.reDisablePlugins = RegExp(`^(?:${config.disablePlugins!.split(',').map((s) => escapeRegExp(s.trim()) + 'Plugin\\.js').join('|')})$`, 'i');
config.reDisablePlugins = RegExp(`^(?:${config.disablePlugins!.split(',').map((s) => escapeRegExp(s.trim())).join('|')})$`, 'i');

const ignoreSuffix =`^.+(?:${config.ignoreSuffix!.split(',').map((s) => escapeRegExp(s.trim())).join('|')})$`;
const ignorePath = '^(?:' + config.traceIgnorePath!.split(',').map(
Expand Down
4 changes: 3 additions & 1 deletion src/core/PluginInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ export default class PluginInstaller {
};
};

isPluginEnabled = (name: string): boolean => !name.match(config.reDisablePlugins);

install(): void {
fs.readdirSync(this.pluginDir)
.filter((file) => !(file.endsWith('.d.ts') || file.endsWith('.js.map')))
.forEach((file) => {
if (file.match(config.reDisablePlugins)) {
if (file.replace(/(?:Plugin)?\.js$/i, '').match(config.reDisablePlugins)) {
logger.info(`Plugin ${file} not installed because it is disabled`);
return;
}
Expand Down
9 changes: 5 additions & 4 deletions src/plugins/MongoDBPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,13 @@ class MongoDBPlugin implements SwPlugin {
return;

Cls.prototype[operation] = function(...args: any[]) {
const spans = ContextManager.spans;
let span = spans[spans.length - 1];
let span = ContextManager.currentSpan;

// XXX: mongodb calls back into itself at this level in several places, for this reason we just do a normal call
// if this is detected instead of opening a new span. This should not affect secondary db calls being recorded
// from a cursor since this span is kept async until the cursor is closed, at which point it is stoppped.

if (span?.component === Component.MONGODB && (span as any).mongodbInCall) // mongodb has called into itself internally, span instanceof ExitSpan assumed
if ((span as any)?.mongodbInCall) // mongodb has called into itself internally
return _original.apply(this, args);

let host = '???';
Expand All @@ -297,7 +296,9 @@ class MongoDBPlugin implements SwPlugin {
span.start();

try {
span.component = Component.MONGODB;
if (span.component === Component.UNKNOWN) // in case mongoose sitting on top
span.component = Component.MONGODB;

span.layer = SpanLayer.DATABASE;
span.peer = host;

Expand Down
Loading