fastify-gcloud-trace
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -5,2 +5,8 @@ # Changelog | ||
## [1.0.4] - 2020-04-05 | ||
### Added | ||
- Increased test coverage | ||
## [1.0.3] - 2020-04-05 | ||
@@ -20,4 +26,10 @@ | ||
### Changed | ||
- Added `decorateRequest` function at the beginning to avoid the deoptimzation | ||
## [1.0.1] - 2020-04-05 | ||
### Added | ||
- Added the following default labels: `HTTP_METHOD_LABEL_KEY`, `HTTP_RESPONSE_CODE_LABEL_KEY`, `HTTP_SOURCE_IP`, and `STATUS_CODE` |
22
index.js
@@ -34,5 +34,5 @@ const fp = require('fastify-plugin') | ||
function isInvalidRootOption (options) { | ||
function isInvalidRootOption (options, reply) { | ||
if (!options.url || typeof options.url !== 'string') { | ||
console.warn('The url that is passed to rootSpanOption is not string') | ||
reply.log.error('The url that is passed to rootSpanOption is not string') | ||
return true | ||
@@ -42,3 +42,3 @@ } | ||
if (!options.method || typeof options.method !== 'string') { | ||
console.warn('The method that is passed to rootSpanOption is not string') | ||
reply.log.error('The method that is passed to rootSpanOption is not string') | ||
return true | ||
@@ -50,7 +50,15 @@ } | ||
function startTracer (traceApiOptions) { | ||
let tracer | ||
try { | ||
tracer = require('@google-cloud/trace-agent').start(traceApiOptions || {}) | ||
} catch (e) { | ||
tracer = require('@google-cloud/trace-agent').get(traceApiOptions || {}) | ||
} | ||
return tracer | ||
} | ||
function plugin (fastify, options, next) { | ||
const { traceApiOptions, tracePluginOptions = { enabled: true } } = options | ||
const trace = tracePluginOptions.enabled | ||
? require('@google-cloud/trace-agent').start(traceApiOptions || {}) | ||
: null | ||
const trace = tracePluginOptions.enabled ? startTracer(traceApiOptions || {}) : null | ||
@@ -61,3 +69,3 @@ fastify.decorateRequest('rootSpan', '') | ||
const rootSpanOption = buildRootOption(req, tracePluginOptions || {}) | ||
if (isInvalidRootOption(rootSpanOption)) { | ||
if (isInvalidRootOption(rootSpanOption, reply)) { | ||
done() | ||
@@ -64,0 +72,0 @@ return |
{ | ||
"name": "fastify-gcloud-trace", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Google Cloud Trace API Connector for Fastify", | ||
@@ -39,3 +39,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"fastify": "^2.13.0", | ||
"request": "^2.88.2", | ||
"rewire": "^5.0.0", | ||
"split2": "^3.1.1", | ||
"standard": "^14.3.3", | ||
@@ -42,0 +45,0 @@ "tap": "^14.10.7" |
const t = require('tap') | ||
const rewire = require('rewire') | ||
const split = require('split2') | ||
const request = require('request') | ||
const Fastify = require('fastify') | ||
const gtrace = rewire('../') | ||
const isRealSpan = gtrace.__get__('isRealSpan') | ||
const buildRootOption = gtrace.__get__('buildRootOption') | ||
const isInvalidRootOption = gtrace.__get__('isInvalidRootOption') | ||
@@ -24,126 +25,76 @@ t.test('isRealSpan()', t => { | ||
t.test('buildRootOption() with correct request and no tracePluginOptions', t => { | ||
const dummyRequest = { | ||
raw: { | ||
client: { | ||
parser: { | ||
incoming: { | ||
originalUrl: '/test/123', | ||
method: 'GET' | ||
} | ||
} | ||
} | ||
t.test('When you use http for GET method', t => { | ||
const stream = split(JSON.parse) | ||
const fastify = Fastify({ | ||
logger: { | ||
level: 'error', | ||
stream | ||
} | ||
} | ||
t.equal( | ||
buildRootOption(dummyRequest, {}).name, | ||
'/test/123', | ||
'buildRootOption() has url for its name' | ||
) | ||
t.equal( | ||
buildRootOption(dummyRequest, {}).url, | ||
'/test/123', | ||
'buildRootOption() has url for its url' | ||
) | ||
t.equal( | ||
buildRootOption(dummyRequest, {}).method, | ||
'GET', | ||
'buildRootOption() has GET for its method' | ||
) | ||
t.end() | ||
}) | ||
}) | ||
t.test('buildRootOption() with correct request and tracePluginOptions to override name', t => { | ||
const dummyRequest = { | ||
raw: { | ||
client: { | ||
parser: { | ||
incoming: { | ||
originalUrl: '/test/123', | ||
method: 'GET' | ||
} | ||
} | ||
fastify.register(gtrace) | ||
fastify.get('/user', (req, reply) => { | ||
reply.send({ hello: 'world' }) | ||
}) | ||
fastify.listen(0, err => { | ||
t.error(err) | ||
t.tearDown(() => fastify.close()) | ||
request( | ||
{ | ||
method: 'GET', | ||
url: `http://0.0.0.0:${fastify.server.address().port}/user` | ||
}, | ||
(err, res) => { | ||
t.error(err) | ||
t.strictEqual(res.statusCode, 200) | ||
t.end() | ||
} | ||
} | ||
} | ||
const tracePluginOptions = { | ||
nameOverride: req => 'test tracer name' | ||
} | ||
t.equal( | ||
buildRootOption(dummyRequest, tracePluginOptions).name, | ||
'test tracer name', | ||
'buildRootOption() has url for its name' | ||
) | ||
t.equal( | ||
buildRootOption(dummyRequest, tracePluginOptions).url, | ||
'/test/123', | ||
'buildRootOption() has url for its url' | ||
) | ||
t.equal( | ||
buildRootOption(dummyRequest, tracePluginOptions).method, | ||
'GET', | ||
'buildRootOption() has GET for its method' | ||
) | ||
t.end() | ||
) | ||
stream.on('data', log => { | ||
t.notMatch(log.msg, 'The url that is passed to rootSpanOption is not string') | ||
t.notMatch(log.msg, 'The method that is passed to rootSpanOption is not string') | ||
}) | ||
}) | ||
}) | ||
t.test('buildRootOption() with incorrect request and tracePluginOptions to override name', t => { | ||
const dummyRequest = { | ||
raw: { | ||
client: { | ||
parser: { | ||
incomings: { | ||
originalUrl: '/test/123', | ||
method: 'GET' | ||
} | ||
} | ||
} | ||
t.test('When you use http for POST method', t => { | ||
const stream = split(JSON.parse) | ||
const fastify = Fastify({ | ||
logger: { | ||
level: 'error', | ||
stream | ||
} | ||
} | ||
const tracePluginOptions = { | ||
nameOverride: req => 'test tracer name' | ||
} | ||
t.equal( | ||
buildRootOption(dummyRequest, tracePluginOptions).name, | ||
'test tracer name', | ||
'buildRootOption() has url for its name' | ||
) | ||
t.equal( | ||
buildRootOption(dummyRequest, tracePluginOptions).url, | ||
null, | ||
'buildRootOption() has url for its url' | ||
) | ||
t.equal( | ||
buildRootOption(dummyRequest, tracePluginOptions).method, | ||
null, | ||
'buildRootOption() has GET for its method' | ||
) | ||
t.end() | ||
}) | ||
}) | ||
t.test('isInvalidRootOption() when valid option is passed', t => { | ||
const option = { | ||
url: '/test/123', | ||
method: 'GET' | ||
} | ||
t.equal(isInvalidRootOption(option), false, 'isInvalidRootOption() should return false') | ||
t.end() | ||
}) | ||
t.tearDown(() => fastify.close()) | ||
t.test('isInvalidRootOption() when invalid url option is passed', t => { | ||
const option = { | ||
url: null, | ||
method: 'GET' | ||
} | ||
t.equal(isInvalidRootOption(option), true, 'isInvalidRootOption() should return true') | ||
t.end() | ||
}) | ||
fastify.register(gtrace) | ||
fastify.post('/user', (req, reply) => { | ||
reply.send({ hello: 'world' }) | ||
}) | ||
t.test('isInvalidRootOption() when invalid method option is passed', t => { | ||
const option = { | ||
url: '/test/123', | ||
method: { method: 'GET' } | ||
} | ||
t.equal(isInvalidRootOption(option), true, 'isInvalidRootOption() should return true') | ||
t.end() | ||
fastify.listen(0, err => { | ||
t.error(err) | ||
t.tearDown(() => fastify.close()) | ||
request( | ||
{ | ||
method: 'POST', | ||
url: `http://0.0.0.0:${fastify.server.address().port}/user` | ||
}, | ||
(err, res) => { | ||
t.error(err) | ||
t.strictEqual(res.statusCode, 200) | ||
t.end() | ||
} | ||
) | ||
stream.on('data', log => { | ||
t.notMatch(log.msg, 'The url that is passed to rootSpanOption is not string') | ||
t.notMatch(log.msg, 'The method that is passed to rootSpanOption is not string') | ||
}) | ||
}) | ||
}) |
13108
6
240