fastify-gcloud-trace
Advanced tools
Comparing version 1.0.4 to 2.0.0
@@ -5,2 +5,8 @@ # Changelog | ||
## [2.0.0] - 2020-04-05 | ||
### Changed | ||
- Avoid modifing request objects too much. Now you can access root span under `req.gtrace` property. This is not a backward compatible chage. | ||
## [1.0.4] - 2020-04-05 | ||
@@ -7,0 +13,0 @@ |
100
index.js
@@ -58,2 +58,15 @@ const fp = require('fastify-plugin') | ||
function initializeGtrace () { | ||
return { | ||
rootSpan: null, | ||
onRequestSpan: null, | ||
parsingSpan: null, | ||
validationSpan: null, | ||
handlerSpan: null, | ||
serializationSpan: null, | ||
onErrorSpan: null, | ||
onSendSpan: null | ||
} | ||
} | ||
function plugin (fastify, options, next) { | ||
@@ -63,3 +76,4 @@ const { traceApiOptions, tracePluginOptions = { enabled: true } } = options | ||
fastify.decorateRequest('rootSpan', '') | ||
const gtrace = initializeGtrace() | ||
fastify.decorateRequest('gtrace', gtrace) | ||
fastify.addHook('onRequest', (req, reply, done) => { | ||
@@ -75,7 +89,7 @@ if (trace) { | ||
if (isRealSpan(span)) { | ||
req.rootSpan = span | ||
req.rootSpan.addLabel(labels.HTTP_METHOD_LABEL_KEY, rootSpanOption.method) | ||
req.rootSpan.addLabel(labels.HTTP_SOURCE_IP, req.ip) | ||
req.gtrace.rootSpan = span | ||
req.gtrace.rootSpan.addLabel(labels.HTTP_METHOD_LABEL_KEY, rootSpanOption.method) | ||
req.gtrace.rootSpan.addLabel(labels.HTTP_SOURCE_IP, req.ip) | ||
req.onRequestSpan = req.rootSpan.createChildSpan({ name: 'onRequest' }) | ||
req.gtrace.onRequestSpan = req.gtrace.rootSpan.createChildSpan({ name: 'onRequest' }) | ||
} | ||
@@ -90,8 +104,8 @@ done() | ||
fastify.addHook('preParsing', (req, reply, done) => { | ||
if (req.onRequestSpan) { | ||
req.onRequestSpan.endSpan() | ||
if (req.gtrace.onRequestSpan) { | ||
req.gtrace.onRequestSpan.endSpan() | ||
} | ||
if (req.rootSpan) { | ||
req.parsing = req.rootSpan.createChildSpan({ name: 'Parsing' }) | ||
if (req.gtrace.rootSpan) { | ||
req.gtrace.parsingSpan = req.gtrace.rootSpan.createChildSpan({ name: 'Parsing' }) | ||
} | ||
@@ -102,8 +116,8 @@ done() | ||
fastify.addHook('preValidation', (req, reply, done) => { | ||
if (req.parsing) { | ||
req.parsing.endSpan() | ||
if (req.gtrace.parsingSpan) { | ||
req.gtrace.parsingSpan.endSpan() | ||
} | ||
if (req.rootSpan) { | ||
req.validation = req.rootSpan.createChildSpan({ name: 'Validation' }) | ||
if (req.gtrace.rootSpan) { | ||
req.gtrace.validationSpan = req.gtrace.rootSpan.createChildSpan({ name: 'Validation' }) | ||
} | ||
@@ -114,8 +128,8 @@ done() | ||
fastify.addHook('preHandler', (req, reply, done) => { | ||
if (req.validation) { | ||
req.validation.endSpan() | ||
if (req.gtrace.validationSpan) { | ||
req.gtrace.validationSpan.endSpan() | ||
} | ||
if (req.rootSpan) { | ||
req.handler = req.rootSpan.createChildSpan({ name: 'Handler' }) | ||
if (req.gtrace.rootSpan) { | ||
req.gtrace.handlerSpan = req.gtrace.rootSpan.createChildSpan({ name: 'Handler' }) | ||
} | ||
@@ -126,8 +140,8 @@ done() | ||
fastify.addHook('preSerialization', (req, reply, payload, done) => { | ||
if (req.handler) { | ||
req.handler.endSpan() | ||
if (req.gtrace.handlerSpan) { | ||
req.gtrace.handlerSpan.endSpan() | ||
} | ||
if (req.rootSpan) { | ||
req.serialization = req.rootSpan.createChildSpan({ name: 'Serialization' }) | ||
if (req.gtrace.rootSpan) { | ||
req.gtrace.serializationSpan = req.gtrace.rootSpan.createChildSpan({ name: 'Serialization' }) | ||
} | ||
@@ -138,20 +152,20 @@ done() | ||
fastify.addHook('onError', (req, reply, error, done) => { | ||
if (req.parsing) { | ||
req.parsing.endSpan() | ||
if (req.gtrace.parsingSpan) { | ||
req.gtrace.parsingSpan.endSpan() | ||
} | ||
if (req.validation) { | ||
req.validation.endSpan() | ||
if (req.gtrace.validationSpan) { | ||
req.gtrace.validationSpan.endSpan() | ||
} | ||
if (req.handler) { | ||
req.handler.endSpan() | ||
if (req.gtrace.handlerSpan) { | ||
req.gtrace.handlerSpan.endSpan() | ||
} | ||
if (req.serialization) { | ||
req.serialization.endSpan() | ||
if (req.gtrace.serializationSpan) { | ||
req.gtrace.serializationSpan.endSpan() | ||
} | ||
if (req.rootSpan) { | ||
req.onError = req.rootSpan.createChildSpan({ name: 'onError' }) | ||
if (req.gtrace.rootSpan) { | ||
req.gtrace.onErrorSpan = req.gtrace.rootSpan.createChildSpan({ name: 'onError' }) | ||
} | ||
@@ -162,12 +176,12 @@ done() | ||
fastify.addHook('onSend', (req, reply, payload, done) => { | ||
if (req.onError) { | ||
req.onError.endSpan() | ||
if (req.gtrace.onErrorSpan) { | ||
req.gtrace.onErrorSpan.endSpan() | ||
} | ||
if (req.serialization) { | ||
req.serialization.endSpan() | ||
if (req.gtrace.serializationSpan) { | ||
req.gtrace.serializationSpan.endSpan() | ||
} | ||
if (req.rootSpan) { | ||
req.onSend = req.rootSpan.createChildSpan({ name: 'onSend' }) | ||
if (req.gtrace.rootSpan) { | ||
req.gtrace.onSendSpan = req.gtrace.rootSpan.createChildSpan({ name: 'onSend' }) | ||
} | ||
@@ -178,10 +192,10 @@ done() | ||
fastify.addHook('onResponse', (req, reply, done) => { | ||
if (req.onSend) { | ||
req.onSend.endSpan() | ||
if (req.gtrace.onSendSpan) { | ||
req.gtrace.onSendSpan.endSpan() | ||
} | ||
if (req.rootSpan) { | ||
req.rootSpan.addLabel(labels.HTTP_RESPONSE_CODE_LABEL_KEY, reply.statusCode) // It is used internally on Stackdriver, but does not show under label for some reason | ||
req.rootSpan.addLabel(customLabels.STATUS_CODE, reply.statusCode) | ||
req.rootSpan.endSpan() | ||
if (req.gtrace.rootSpan) { | ||
req.gtrace.rootSpan.addLabel(labels.HTTP_RESPONSE_CODE_LABEL_KEY, reply.statusCode) // It is used internally on Stackdriver, but does not show under label for some reason | ||
req.gtrace.rootSpan.addLabel(customLabels.STATUS_CODE, reply.statusCode) | ||
req.gtrace.rootSpan.endSpan() | ||
} | ||
@@ -188,0 +202,0 @@ done() |
{ | ||
"name": "fastify-gcloud-trace", | ||
"version": "1.0.4", | ||
"version": "2.0.0", | ||
"description": "Google Cloud Trace API Connector for Fastify", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -47,8 +47,8 @@ # fastify-gcloud-trace | ||
This plugin attaches a Trace object to each request, and the object is accessible as `rootSpan` in a request object. Therefore, you can access the trace instance in your application code, and perform all functionalities defined in the Stackdriver Trace API. For example, you can create a childSpan in the following way. It is important to note that `rootSpan` is only defined when traceAPI generates a "traced" root span, so your application code has to handle the case where `rootSpan` is `null`. You can find the different types of span [here](https://googleapis.dev/nodejs/trace/latest/classes/UntracedRootSpanData.html). | ||
This plugin attaches a Trace object to each request, and the object is accessible as `rootSpan` in a `request.gtrace` object. Therefore, you can access the trace instance in your application code, and perform all functionalities defined in the Stackdriver Trace API. For example, you can create a childSpan in the following way. It is important to note that `rootSpan` is only defined when traceAPI generates a "traced" root span, so your application code has to handle the case where `rootSpan` is `null`. You can find the different types of span [here](https://googleapis.dev/nodejs/trace/latest/classes/UntracedRootSpanData.html). | ||
```js | ||
fastify.get('/foo', (req, reply) => { | ||
const span = req.rootSpan | ||
? req.rootSpan.createChildSpan({name: 'Perform Heavy Calculation'}) | ||
const span = req.gtrace.rootSpan | ||
? req.gtrace.rootSpan.createChildSpan({name: 'Perform Heavy Calculation'}) | ||
: null; | ||
@@ -55,0 +55,0 @@ // Do something |
14036
253