fastify-opentelemetry
Advanced tools
Comparing version 1.0.2 to 1.0.3
44
index.js
@@ -47,4 +47,23 @@ const fp = require('fastify-plugin') | ||
function ignoreFromCondition (ignoreConditions, target) { | ||
return ( | ||
!target || | ||
ignoreConditions.some(condition => { | ||
if (typeof condition === 'string') { | ||
return condition === target | ||
} | ||
if (typeof condition === 'object') { | ||
return target.match(condition) | ||
} | ||
if (typeof condition === 'function') { | ||
return condition(target) | ||
} | ||
}) | ||
) | ||
} | ||
function plugin (fastify, options = { enabled: true, tracer: null }, next) { | ||
const { enabled, tracer } = options | ||
const { enabled, tracer, ignoreUrls = [], ignoreMethods = [] } = options | ||
const commonAttribute = { | ||
@@ -63,11 +82,16 @@ [attributesNames.FASTIFY_TYPE]: 'fastify.hook' | ||
const rootSpanOption = buildRootOption(req, options || {}) | ||
const rootSpan = tracer.startSpan(rootSpanOption.name, { parent: tracer.getCurrentSpan() }) | ||
rootSpan.setAttribute('method', rootSpanOption.method) | ||
rootSpan.setAttribute('url', rootSpanOption.url) | ||
const onRequestSpan = tracer.startSpan(spanNames.ON_REQUEST, { | ||
parent: rootSpan, | ||
...commonAttribute | ||
}) | ||
req.opentelemetry.rootSpan = rootSpan | ||
req.opentelemetry.onRequestSpan = onRequestSpan | ||
const shouldCreateSpan = | ||
!ignoreFromCondition(ignoreUrls, rootSpanOption.url) && | ||
!ignoreFromCondition(ignoreMethods, rootSpanOption.method) | ||
if (shouldCreateSpan) { | ||
const rootSpan = tracer.startSpan(rootSpanOption.name, { parent: tracer.getCurrentSpan() }) | ||
rootSpan.setAttribute('method', rootSpanOption.method) | ||
rootSpan.setAttribute('url', rootSpanOption.url) | ||
const onRequestSpan = tracer.startSpan(spanNames.ON_REQUEST, { | ||
parent: rootSpan, | ||
...commonAttribute | ||
}) | ||
req.opentelemetry.rootSpan = rootSpan | ||
req.opentelemetry.onRequestSpan = onRequestSpan | ||
} | ||
done() | ||
@@ -74,0 +98,0 @@ } else { |
{ | ||
"name": "fastify-opentelemetry", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "OpenTelemetry fastify automatic instrumentation package.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -74,2 +74,4 @@ # fastify-opentelemetry | ||
- `tracer` - OpenTelemetry API `tracer` object. This is a required property in order to use this plugin. | ||
- `ignoreUrls` - You can pass an array of conditions to avoid creating a tracer for a certain URL. The conditions can be either string, regex, or function. | ||
- `ignoreMethods` - Similar to ignoreUrls, you can pass an array of conditions to avoid creating a tracer for a certain request method. | ||
@@ -76,0 +78,0 @@ ## Limitations |
@@ -76,3 +76,3 @@ const t = require('tap') | ||
t.test('When you use http for GET method', t => { | ||
t.test('When you use http for POST method', t => { | ||
const stream = split(JSON.parse) | ||
@@ -126,1 +126,107 @@ const provider = new NodeTracerProvider() | ||
}) | ||
t.test('Test on ignoreUrls', t => { | ||
const stream = split(JSON.parse) | ||
const provider = new NodeTracerProvider() | ||
provider.addSpanProcessor( | ||
new SimpleSpanProcessor( | ||
new ZipkinExporter({ | ||
serviceName: 'fastify' | ||
}) | ||
) | ||
) | ||
const fastify = Fastify({ | ||
logger: { | ||
level: 'info', | ||
stream | ||
} | ||
}) | ||
fastify.register(fastifyOpentelemetry, { | ||
enabled: true, | ||
tracer, | ||
ignoreUrls: [/.*user.*/] | ||
}) | ||
fastify.get('/user', (req, reply) => { | ||
reply.send({ hello: 'world' }) | ||
}) | ||
let matchCount = 0 | ||
fastify.listen(0, err => { | ||
t.plan(4) | ||
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.strictEqual(matchCount, 0) | ||
} | ||
) | ||
stream.on('data', log => { | ||
if (log.msg.match(/New trace<[a-z0-9]+> was created/)) { | ||
matchCount += 1 | ||
} | ||
}) | ||
}) | ||
}) | ||
t.test('Test on ignoreMethods', t => { | ||
const stream = split(JSON.parse) | ||
const provider = new NodeTracerProvider() | ||
provider.addSpanProcessor( | ||
new SimpleSpanProcessor( | ||
new ZipkinExporter({ | ||
serviceName: 'fastify' | ||
}) | ||
) | ||
) | ||
const fastify = Fastify({ | ||
logger: { | ||
level: 'info', | ||
stream | ||
} | ||
}) | ||
fastify.register(fastifyOpentelemetry, { | ||
enabled: true, | ||
tracer, | ||
ignoreMethods: ['GET'] | ||
}) | ||
fastify.get('/user', (req, reply) => { | ||
reply.send({ hello: 'world' }) | ||
}) | ||
let matchCount = 0 | ||
fastify.listen(0, err => { | ||
t.plan(4) | ||
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.strictEqual(matchCount, 0) | ||
} | ||
) | ||
stream.on('data', log => { | ||
if (log.msg.match(/New trace<[a-z0-9]+> was created/)) { | ||
matchCount += 1 | ||
} | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16889
425
82