mongoose-xray
Advanced tools
Comparing version 3.0.0 to 3.0.1
@@ -16,3 +16,10 @@ const AWSXRay = require('aws-xray-sdk-core'); | ||
schema.pre(operation, function (next) { | ||
exports.createAggregateSubsegment(operation, this, options); | ||
const subsegment = exports.createAggregateSubsegment( | ||
operation, | ||
this, | ||
options | ||
); | ||
if (subsegment) { | ||
this.xRaySubsegmentId = subsegment.id; | ||
} | ||
next(); | ||
@@ -22,3 +29,3 @@ }); | ||
schema.post(operation, function (docs, next) { | ||
closeCurrentSegment(); | ||
closeCurrentSegment(this.xRaySubsegmentId); | ||
next(); | ||
@@ -28,3 +35,3 @@ }); | ||
schema.post(operation, function (err, docs, next) { | ||
handleSegmentError(err); | ||
handleSegmentError(err, this.xRaySubsegmentId); | ||
next(); | ||
@@ -43,4 +50,7 @@ }); | ||
if (parent) { | ||
const subsegment = parent.addNewSubsegment(aggregate.model().modelName); | ||
subsegment.addMetadata('operation', operation); | ||
const subsegment = parent.addNewSubsegment( | ||
`${aggregate.model().modelName}-${operation}` | ||
); | ||
subsegment.addAnnotation('model', aggregate.model().modelName); | ||
subsegment.addAnnotation('operation', operation); | ||
if (options && options.verbose) { | ||
@@ -50,3 +60,4 @@ subsegment.addMetadata('options', aggregate.options); | ||
} | ||
return subsegment; | ||
} | ||
}; |
@@ -17,3 +17,10 @@ const AWSXRay = require('aws-xray-sdk-core'); | ||
schema.pre(operation, function (next) { | ||
exports.createDocumentSubsegment(operation, this, options); | ||
const subsegment = exports.createDocumentSubsegment( | ||
operation, | ||
this, | ||
options | ||
); | ||
if (subsegment) { | ||
this.xRaySubsegmentId = subsegment.id; | ||
} | ||
next(); | ||
@@ -23,3 +30,3 @@ }); | ||
schema.post(operation, function (doc, next) { | ||
closeCurrentSegment(); | ||
closeCurrentSegment(this.xRaySubsegmentId); | ||
next(); | ||
@@ -29,3 +36,3 @@ }); | ||
schema.post(operation, function (err, doc, next) { | ||
handleSegmentError(err); | ||
handleSegmentError(err, this.xRaySubsegmentId); | ||
next(); | ||
@@ -45,8 +52,12 @@ }); | ||
if (parent) { | ||
const subsegment = parent.addNewSubsegment(document.constructor.modelName); | ||
subsegment.addMetadata('operation', operation); | ||
const subsegment = parent.addNewSubsegment( | ||
`${document.constructor.modelName}-${operation}` | ||
); | ||
subsegment.addAnnotation('model', document.constructor.modelName); | ||
subsegment.addAnnotation('operation', operation); | ||
if (options && options.verbose) { | ||
subsegment.addMetadata('document', JSON.stringify(document)); | ||
} | ||
return subsegment; | ||
} | ||
}; |
@@ -15,3 +15,9 @@ const AWSXRay = require('aws-xray-sdk-core'); | ||
schema.pre(operation, function (next) { | ||
exports.createModelSubsegment(operation, this); | ||
const subsegment = exports.createModelSubsegment(operation, this); | ||
if (subsegment) { | ||
if (!this.xRaySubsegmentIds) { | ||
this.xRaySubsegmentIds = []; | ||
} | ||
this.xRaySubsegmentIds.unshift(subsegment.id); | ||
} | ||
next(); | ||
@@ -21,3 +27,5 @@ }); | ||
schema.post(operation, function (result, next) { | ||
closeCurrentSegment(); | ||
if (this.xRaySubsegmentIds && this.xRaySubsegmentIds.length) { | ||
closeCurrentSegment(this.xRaySubsegmentIds.pop()); | ||
} | ||
next(); | ||
@@ -27,3 +35,5 @@ }); | ||
schema.post(operation, function (err, result, next) { | ||
handleSegmentError(err); | ||
if (this.xRaySubsegmentIds && this.xRaySubsegmentIds.length) { | ||
handleSegmentError(err, this.xRaySubsegmentIds.pop()); | ||
} | ||
next(); | ||
@@ -41,5 +51,10 @@ }); | ||
if (parent) { | ||
const subsegment = parent.addNewSubsegment(model.modelName); | ||
subsegment.addMetadata('operation', operation); | ||
const subsegment = parent.addNewSubsegment( | ||
`${model.modelName}-${operation}` | ||
); | ||
subsegment.addAnnotation('model', model.modelName); | ||
subsegment.addAnnotation('operation', operation); | ||
return subsegment; | ||
} | ||
}; |
@@ -30,3 +30,10 @@ const AWSXRay = require('aws-xray-sdk-core'); | ||
schema.pre(operation, function (next) { | ||
exports.createQuerySubsegment(operation, this, options); | ||
const subsegment = exports.createQuerySubsegment( | ||
operation, | ||
this, | ||
options | ||
); | ||
if (subsegment) { | ||
this.xRaySubsegmentId = subsegment.id; | ||
} | ||
next(); | ||
@@ -36,3 +43,3 @@ }); | ||
schema.post(operation, function (result, next) { | ||
closeCurrentSegment(); | ||
closeCurrentSegment(this.xRaySubsegmentId); | ||
next(); | ||
@@ -42,3 +49,3 @@ }); | ||
schema.post(operation, function (err, result, next) { | ||
handleSegmentError(err); | ||
handleSegmentError(err, this.xRaySubsegmentId); | ||
next(); | ||
@@ -58,4 +65,7 @@ }); | ||
if (parent) { | ||
const subsegment = parent.addNewSubsegment(query.model.modelName); | ||
subsegment.addMetadata('operation', operation); | ||
const subsegment = parent.addNewSubsegment( | ||
`${query.model.modelName}-${operation}` | ||
); | ||
subsegment.addAnnotation('model', query.model.modelName); | ||
subsegment.addAnnotation('operation', operation); | ||
subsegment.addMetadata('filter', query.getFilter()); | ||
@@ -67,3 +77,4 @@ if (options && options.verbose) { | ||
} | ||
return subsegment; | ||
} | ||
}; |
@@ -5,7 +5,13 @@ const AWSXRay = require('aws-xray-sdk-core'); | ||
* Closes the current xray segment | ||
* @param subsegmentId The subsegment to match | ||
*/ | ||
exports.closeCurrentSegment = () => { | ||
const segment = AWSXRay.getSegment(); | ||
if (segment && !segment.isClosed()) { | ||
segment.close(); | ||
exports.closeCurrentSegment = (subsegmentId) => { | ||
if (subsegmentId) { | ||
const segment = AWSXRay.getSegment(); | ||
if (segment) { | ||
const subsegment = segment.subsegments.find((x) => x.id === subsegmentId); | ||
if (subsegment && !subsegment.isClosed()) { | ||
subsegment.close(); | ||
} | ||
} | ||
} | ||
@@ -17,10 +23,14 @@ }; | ||
* @param {Error} err The error | ||
* @param subsegmentId The subsegment to match | ||
*/ | ||
exports.handleSegmentError = (err) => { | ||
const segment = AWSXRay.getSegment(); | ||
if (segment) { | ||
if (segment.isClosed()) { | ||
segment.addError(err); | ||
} else { | ||
segment.close(err); | ||
exports.handleSegmentError = (err, subsegmentId) => { | ||
if (subsegmentId) { | ||
const segment = AWSXRay.getSegment(); | ||
if (segment) { | ||
const subsegment = segment.subsegments.find((x) => x.id === subsegmentId); | ||
if (subsegment.isClosed()) { | ||
subsegment.addError(err); | ||
} else { | ||
subsegment.close(err); | ||
} | ||
} | ||
@@ -27,0 +37,0 @@ } |
{ | ||
"name": "mongoose-xray", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "AWS-Xray plugin for Mongoose", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -14,4 +14,6 @@ # mongoose-xray | ||
Simply register as a normal mongoose plugin | ||
Simply register as a normal mongoose plugin. | ||
*Note* that the plugin must be added before a connection is established. | ||
```js | ||
@@ -18,0 +20,0 @@ |
13417
281
61