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

Initial addition of koa-route plugin #561

Merged
merged 2 commits into from
May 9, 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
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 @@ -68,7 +68,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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing a corresponding test.

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