Skip to content

Commit

Permalink
Initial addition of koa-route plugin (#561)
Browse files Browse the repository at this point in the history
* Added koa-route as addition to existing koa plugin
* Added tests for koa-route
* Add test for web.enterRoute
  • Loading branch information
rishabh authored May 9, 2019
1 parent b4c3ab1 commit 62f0731
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/plugins/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ function createWrapUse (tracer, config) {
function ddTrace (ctx, next) {
web.instrument(tracer, config, ctx.req, ctx.res, 'koa.request')

web.beforeEnd(ctx.req, () => {
web.enterRoute(ctx.req, ctx.routePath)
})
return next()
}

Expand Down
4 changes: 3 additions & 1 deletion src/plugins/util/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ const web = {

// Add a route segment that will be used for the resource name.
enterRoute (req, path) {
req._datadog.paths.push(path)
if (typeof path === 'string') {
req._datadog.paths.push(path)
}
},

// Remove the current route segment.
Expand Down
4 changes: 4 additions & 0 deletions test/plugins/externals.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
}
],
"koa": [
{
"name": "koa-route",
"versions": [">=3.2"]
},
{
"name": "koa-websocket",
"versions": ["5.0.1"]
Expand Down
36 changes: 35 additions & 1 deletion test/plugins/koa.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,41 @@ describe('Plugin', () => {
})
})

withVersions(plugin, 'koa-route', routerVersion => {
let koaRouter

beforeEach(() => {
koaRouter = require(`../../versions/koa-route@${routerVersion}`).get()
})

it('should do automatic instrumentation on koa-route', done => {
const app = new Koa()

const getUser = (ctx, id) => {
ctx.body = ''
}

app
.use(koaRouter.get('/user/:id', getUser))

agent
.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('resource', 'GET /user/:id')
expect(spans[0].meta).to.have.property('http.url', `http://localhost:${port}/user/123`)
})
.then(done)
.catch(done)

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

withVersions(plugin, 'koa-router', routerVersion => {
let Router

Expand All @@ -237,7 +272,6 @@ describe('Plugin', () => {
agent
.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('resource', 'GET /user/:id')
expect(spans[0].meta).to.have.property('http.url', `http://localhost:${port}/user/123`)

Expand Down
11 changes: 11 additions & 0 deletions test/plugins/util/web.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,17 @@ describe('plugins/util/web', () => {
expect(span.context()._tags).to.have.property(RESOURCE_NAME, 'GET /foo/bar')
expect(span.context()._tags).to.have.property(HTTP_ROUTE, '/foo/bar')
})

it('should only add valid route segments to the span resource name', () => {
req.method = 'GET'

web.enterRoute(req)
web.enterRoute(req, 1337)
res.end()

expect(span.context()._tags).to.have.property(RESOURCE_NAME, 'GET')
expect(span.context()._tags).to.not.have.property(HTTP_ROUTE)
})
})

describe('exitRoute', () => {
Expand Down

0 comments on commit 62f0731

Please sign in to comment.