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

add support for fastify #614

Merged
merged 6 commits into from
Jul 8, 2019
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
9 changes: 9 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ jobs:
environment:
- PLUGINS=express

node-fastify:
<<: *node-plugin-base
docker:
- image: node:8
environment:
- PLUGINS=fastify

node-generic-pool:
<<: *node-plugin-base
docker:
Expand Down Expand Up @@ -561,6 +568,7 @@ workflows:
- node-dns
- node-elasticsearch
- node-express
- node-fastify
- node-generic-pool
- node-graphql
- node-hapi
Expand Down Expand Up @@ -648,6 +656,7 @@ workflows:
- node-dns
- node-elasticsearch
- node-express
- node-fastify
- node-generic-pool
- node-graphql
- node-hapi
Expand Down
2 changes: 2 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ tracer.use('pg', {
<h5 id="express-tags"></h5>
<h5 id="express-config"></h5>
<h5 id="generic-pool"></h5>
<h5 id="fastify"></h5>
<h5 id="graphql"></h5>
<h5 id="graphql-tags"></h5>
<h5 id="graphql-config"></h5>
Expand Down Expand Up @@ -270,6 +271,7 @@ tracer.use('pg', {
* [dns](./interfaces/plugins.dns.html)
* [elasticsearch](./interfaces/plugins.elasticsearch.html)
* [express](./interfaces/plugins.express.html)
* [fastify](./interfaces/plugins.fastify.html)
* [generic-pool](./interfaces/plugins.generic_pool.html)
* [graphql](./interfaces/plugins.graphql.html)
* [hapi](./interfaces/plugins.hapi.html)
Expand Down
2 changes: 2 additions & 0 deletions docs/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ tracer.use('dns');
tracer.use('elasticsearch');
tracer.use('express');
tracer.use('express', httpServerOptions);
tracer.use('fastify');
tracer.use('fastify', httpServerOptions);
tracer.use('generic-pool');
tracer.use('graphql', graphqlOptions);
tracer.use('graphql', { variables: ['foo', 'bar'] });
Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ interface Plugins {
"dns": plugins.dns;
"elasticsearch": plugins.elasticsearch;
"express": plugins.express;
"fastify": plugins.fastify;
"generic-pool": plugins.generic_pool;
"graphql": plugins.graphql;
"hapi": plugins.hapi;
Expand Down Expand Up @@ -566,6 +567,12 @@ declare namespace plugins {
*/
interface express extends HttpServer {}

/**
* This plugin automatically instruments the
* [fastify](https://www.fastify.io/) module.
*/
interface fastify extends HttpServer {}

/**
* This plugin patches the [generic-pool](https://github.com/coopernurse/node-pool)
* module to bind the callbacks the the caller context.
Expand Down
76 changes: 76 additions & 0 deletions packages/datadog-plugin-fastify/src/fastify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict'

const web = require('../../dd-trace/src/plugins/util/web')

function createWrapFastify (tracer, config) {
config = web.normalizeConfig(config)

return function wrapFastify (fastify) {
const fastifyWithTrace = function fastifyWithTrace () {
return fastify._datadog_wrapper.apply(this, arguments)
}

fastify._datadog_wrapper = function () {
const app = fastify.apply(this, arguments)

app.addHook('onRequest', createOnRequest(tracer, config))
app.addHook('preHandler', preHandler)

return app
}

return fastifyWithTrace
}
}

function createOnRequest (tracer, config) {
return function onRequest (request, reply, next) {
const req = getReq(request)
const res = getRes(reply)
const name = 'fastify.request'

return web.instrument(tracer, config, req, res, name, () => next())
}
}

function preHandler (request, reply, next) {
reply.send = wrapSend(reply.send)

next()
}

function wrapSend (send) {
return function sendWithTrace (payload) {
const req = getReq(this.request)

web.addError(req, payload)

return send.apply(this, arguments)
}
}

function unwrapFastify (fastify) {
fastify._datadog_wrapper = fastify
}

function getReq (request) {
return request.req || request
}

function getRes (reply) {
return reply.res || reply
}

module.exports = [
{
name: 'fastify',
versions: ['>=1'],
patch (fastify, tracer, config) {
// `fastify` is a function so we return a wrapper that will replace its export.
return createWrapFastify(tracer, config)(fastify)
},
unpatch (fastify) {
unwrapFastify(fastify)
}
}
]
34 changes: 34 additions & 0 deletions packages/datadog-plugin-fastify/src/find-my-way.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const web = require('../../dd-trace/src/plugins/util/web')

function createWrapOn () {
return function wrapOn (on) {
return function onWithTrace (method, path, opts) {
const index = typeof opts === 'function' ? 2 : 3
const handler = arguments[index]

arguments[index] = function (req) {
web.patch(req)
web.enterRoute(req, path)

return handler.apply(this, arguments)
}

return on.apply(this, arguments)
}
}
}

module.exports = [
{
name: 'find-my-way',
versions: ['>=1'],
patch (Router, tracer, config) {
this.wrap(Router.prototype, 'on', createWrapOn(tracer, config))
},
unpatch (Router) {
this.unwrap(Router.prototype, 'on')
}
}
]
6 changes: 6 additions & 0 deletions packages/datadog-plugin-fastify/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

module.exports = [].concat(
require('./fastify'),
require('./find-my-way')
)
199 changes: 199 additions & 0 deletions packages/datadog-plugin-fastify/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
'use strict'

const axios = require('axios')
const getPort = require('get-port')
const agent = require('../../dd-trace/test/plugins/agent')
const plugin = require('../src')

wrapIt()

describe('Plugin', () => {
let tracer
let fastify
let app

describe('fastify', () => {
withVersions(plugin, 'fastify', version => {
beforeEach(() => {
tracer = require('../../dd-trace')
})

afterEach(() => {
app.close()
})

describe('without configuration', () => {
before(() => {
return agent.load(plugin, 'fastify')
})

after(() => {
return agent.close()
})

beforeEach(() => {
fastify = require(`../../../versions/fastify@${version}`).get()
app = fastify()
})

it('should do automatic instrumentation on the app routes', done => {
app.get('/user', (request, reply) => {
reply.send()
})

getPort().then(port => {
agent
.use(traces => {
const spans = traces[0]

expect(spans[0]).to.have.property('name', 'fastify.request')
expect(spans[0]).to.have.property('service', 'test')
expect(spans[0]).to.have.property('type', 'web')
expect(spans[0]).to.have.property('resource', 'GET /user')
expect(spans[0].meta).to.have.property('span.kind', 'server')
expect(spans[0].meta).to.have.property('http.url', `http://localhost:${port}/user`)
expect(spans[0].meta).to.have.property('http.method', 'GET')
expect(spans[0].meta).to.have.property('http.status_code', '200')
})
.then(done)
.catch(done)

app.listen(port, 'localhost', () => {
axios
.get(`http://localhost:${port}/user`)
.catch(done)
})
})
})

it('should do automatic instrumentation on route full syntax', done => {
app.route({
method: 'GET',
url: '/user/:id',
handler: (request, reply) => {
reply.send()
}
})

getPort().then(port => {
agent
.use(traces => {
const spans = traces[0]

expect(spans[0]).to.have.property('name', 'fastify.request')
expect(spans[0]).to.have.property('service', 'test')
expect(spans[0]).to.have.property('type', 'web')
expect(spans[0]).to.have.property('resource', 'GET /user/:id')
expect(spans[0].meta).to.have.property('span.kind', 'server')
expect(spans[0].meta).to.have.property('http.url', `http://localhost:${port}/user/123`)
expect(spans[0].meta).to.have.property('http.method', 'GET')
expect(spans[0].meta).to.have.property('http.status_code', '200')
})
.then(done)
.catch(done)

app.listen(port, 'localhost', () => {
axios
.get(`http://localhost:${port}/user/123`)
.catch(done)
})
})
})

it('should run handlers in the request scope', done => {
if (process.env.DD_CONTEXT_PROPAGATION === 'false') return done()

app.use((req, res, next) => {
expect(tracer.scope().active()).to.not.be.null
next()
})

app.get('/user', (request, reply) => {
expect(tracer.scope().active()).to.not.be.null
reply.send()
})

getPort().then(port => {
app.listen(port, 'localhost', () => {
axios.get(`http://localhost:${port}/user`)
.then(() => done())
.catch(done)
})
})
})

it('should run middleware in the request scope', done => {
if (process.env.DD_CONTEXT_PROPAGATION === 'false') return done()

app.use((req, res, next) => {
expect(tracer.scope().active()).to.not.be.null
next()
})

app.get('/user', (request, reply) => reply.send())

getPort().then(port => {
app.listen(port, 'localhost', () => {
axios.get(`http://localhost:${port}/user`)
.then(() => done())
.catch(done)
})
})
})

it('should run hooks in the request scope', done => {
if (process.env.DD_CONTEXT_PROPAGATION === 'false') return done()

app.addHook('onRequest', (request, reply, next) => {
expect(tracer.scope().active()).to.not.be.null
next()
})

app.addHook('onResponse', (request, reply, next) => {
expect(tracer.scope().active()).to.not.be.null
next ? next() : reply()
})

app.get('/user', (request, reply) => reply.send())

getPort().then(port => {
app.listen(port, 'localhost', () => {
axios.get(`http://localhost:${port}/user`)
.then(() => done())
.catch(done)
})
})
})

it('should handle reply errors', done => {
let error

app.get('/user', (request, reply) => {
reply.send(error = new Error('boom'))
})

getPort().then(port => {
agent
.use(traces => {
const spans = traces[0]

expect(spans[0]).to.have.property('name', 'fastify.request')
expect(spans[0]).to.have.property('resource', 'GET /user')
expect(spans[0].meta).to.have.property('error.type', error.name)
expect(spans[0].meta).to.have.property('error.msg', error.message)
expect(spans[0].meta).to.have.property('error.stack', error.stack)
})
.then(done)
.catch(done)

app.listen(port, 'localhost', () => {
axios
.get(`http://localhost:${port}/user`)
.catch(() => {})
})
})
})
})
})
})
})
Loading