-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
358 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict' | ||
|
||
const web = require('./util/web') | ||
|
||
function createWrapUse (tracer, config) { | ||
config = web.normalizeConfig(config) | ||
|
||
function ddTrace (ctx, next) { | ||
if (web.active(ctx.req)) return next() | ||
|
||
web.instrument(tracer, config, ctx.req, ctx.res, 'koa.request') | ||
|
||
return next() | ||
.then(() => extractRoute(ctx)) | ||
.catch(e => { | ||
extractRoute(ctx) | ||
return Promise.reject(e) | ||
}) | ||
} | ||
|
||
return function wrapUse (use) { | ||
return function useWithTrace (fn) { | ||
if (!this._datadog_trace_patched) { | ||
this._datadog_trace_patched = true | ||
use.call(this, ddTrace) | ||
} | ||
|
||
return use.call(this, function (ctx, next) { | ||
web.reactivate(ctx.req) | ||
return fn.apply(this, arguments) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
function extractRoute (ctx) { | ||
if (ctx.matched) { | ||
ctx.matched | ||
.filter(layer => layer.methods.length > 0) | ||
.forEach(layer => { | ||
web.enterRoute(ctx.req, layer.path) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = [ | ||
{ | ||
name: 'koa', | ||
versions: ['2.x'], | ||
patch (Koa, tracer, config) { | ||
this.wrap(Koa.prototype, 'use', createWrapUse(tracer, config)) | ||
}, | ||
unpatch (Koa) { | ||
this.unwrap(Koa.prototype, 'use') | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
{ | ||
"name": "koa-router", | ||
"versions": ["7.x"] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
'use strict' | ||
|
||
const axios = require('axios') | ||
const getPort = require('get-port') | ||
const agent = require('./agent') | ||
const plugin = require('../../src/plugins/koa') | ||
|
||
wrapIt() | ||
|
||
describe('Plugin', () => { | ||
let tracer | ||
let Koa | ||
let appListener | ||
|
||
describe('express', () => { | ||
withVersions(plugin, 'koa', version => { | ||
let port | ||
|
||
beforeEach(() => { | ||
tracer = require('../..') | ||
}) | ||
|
||
afterEach(done => { | ||
appListener.close(() => done()) | ||
}) | ||
|
||
afterEach(() => { | ||
return agent.close() | ||
}) | ||
|
||
describe('without configuration', () => { | ||
beforeEach(() => { | ||
return agent.load(plugin, 'koa') | ||
}) | ||
|
||
beforeEach(() => { | ||
Koa = require(`./versions/koa@${version}`).get() | ||
return getPort().then(newPort => { | ||
port = newPort | ||
}) | ||
}) | ||
|
||
it('should do automatic instrumentation on middleware', done => { | ||
const app = new Koa() | ||
|
||
app.use((ctx) => { | ||
ctx.body = '' | ||
}) | ||
|
||
agent | ||
.use(traces => { | ||
expect(traces[0][0]).to.have.property('name', 'koa.request') | ||
expect(traces[0][0]).to.have.property('service', 'test') | ||
expect(traces[0][0]).to.have.property('type', 'http') | ||
expect(traces[0][0]).to.have.property('resource', 'GET') | ||
expect(traces[0][0].meta).to.have.property('span.kind', 'server') | ||
expect(traces[0][0].meta).to.have.property('http.url', `http://localhost:${port}/user`) | ||
expect(traces[0][0].meta).to.have.property('http.method', 'GET') | ||
expect(traces[0][0].meta).to.have.property('http.status_code', '200') | ||
}) | ||
.then(done) | ||
.catch(done) | ||
|
||
appListener = app.listen(port, 'localhost', () => { | ||
axios | ||
.get(`http://localhost:${port}/user`) | ||
.catch(done) | ||
}) | ||
}) | ||
|
||
it('should run middleware in the request scope', done => { | ||
if (process.env.DD_CONTEXT_PROPAGATION === 'false') return done() | ||
|
||
const app = new Koa() | ||
|
||
app.use((ctx, next) => { | ||
ctx.body = '' | ||
|
||
expect(tracer.scopeManager().active()).to.not.be.null | ||
|
||
return next() | ||
.then(() => { | ||
expect(tracer.scopeManager().active()).to.not.be.null | ||
done() | ||
}) | ||
.catch(done) | ||
}) | ||
|
||
appListener = app.listen(port, 'localhost', () => { | ||
axios | ||
.get(`http://localhost:${port}/app/user/123`) | ||
.catch(done) | ||
}) | ||
}) | ||
|
||
it('should reactivate the request span in middleware scopes', done => { | ||
if (process.env.DD_CONTEXT_PROPAGATION === 'false') return done() | ||
|
||
const app = new Koa() | ||
|
||
let span | ||
|
||
app.use((ctx, next) => { | ||
span = tracer.scopeManager().active().span() | ||
tracer.scopeManager().activate({}) | ||
return next() | ||
}) | ||
|
||
app.use((ctx) => { | ||
const scope = tracer.scopeManager().active() | ||
|
||
ctx.body = '' | ||
|
||
try { | ||
expect(scope).to.not.be.null | ||
expect(scope.span()).to.equal(span) | ||
done() | ||
} catch (e) { | ||
done(e) | ||
} | ||
}) | ||
|
||
getPort().then(port => { | ||
appListener = app.listen(port, 'localhost', () => { | ||
axios.get(`http://localhost:${port}/user`) | ||
.catch(done) | ||
}) | ||
}) | ||
}) | ||
|
||
withVersions(plugin, 'koa-router', routerVersion => { | ||
let Router | ||
|
||
beforeEach(() => { | ||
Router = require(`./versions/koa-router@${routerVersion}`).get() | ||
}) | ||
|
||
it('should do automatic instrumentation on routers', done => { | ||
const app = new Koa() | ||
const router = new Router() | ||
|
||
router.get('/user/:id', (ctx, next) => { | ||
ctx.body = '' | ||
}) | ||
|
||
app | ||
.use(router.routes()) | ||
.use(router.allowedMethods()) | ||
|
||
agent | ||
.use(traces => { | ||
expect(traces[0][0]).to.have.property('resource', 'GET /user/:id') | ||
expect(traces[0][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) | ||
}) | ||
}) | ||
|
||
it('should support nested routers', done => { | ||
const app = new Koa() | ||
const forums = new Router() | ||
const posts = new Router() | ||
|
||
posts.get('/:pid', (ctx, next) => { | ||
ctx.body = '' | ||
}) | ||
|
||
forums.use('/forums/:fid/posts', posts.routes(), posts.allowedMethods()) | ||
|
||
app.use(forums.routes()) | ||
|
||
agent | ||
.use(traces => { | ||
expect(traces[0][0]).to.have.property('resource', 'GET /forums/:fid/posts/:pid') | ||
expect(traces[0][0].meta).to.have.property('http.url', `http://localhost:${port}/forums/123/posts/456`) | ||
}) | ||
.then(done) | ||
.catch(done) | ||
|
||
appListener = app.listen(port, 'localhost', () => { | ||
axios | ||
.get(`http://localhost:${port}/forums/123/posts/456`) | ||
.catch(done) | ||
}) | ||
}) | ||
|
||
it('should support a router prefix', done => { | ||
const app = new Koa() | ||
const router = new Router({ | ||
prefix: '/user' | ||
}) | ||
|
||
router.get('/:id', (ctx, next) => { | ||
ctx.body = '' | ||
}) | ||
|
||
app | ||
.use(router.routes()) | ||
.use(router.allowedMethods()) | ||
|
||
agent | ||
.use(traces => { | ||
expect(traces[0][0]).to.have.property('resource', 'GET /user/:id') | ||
expect(traces[0][0].meta).to.have.property('http.url', `http://localhost:${port}/user/123`) | ||
}) | ||
.then(done) | ||
.catch(done) | ||
|
||
appListener = app.listen(port, 'localhost', () => { | ||
axios | ||
.get(`http://localhost:${port}/user/123`) | ||
.catch(done) | ||
}) | ||
}) | ||
|
||
it('should handle errors', done => { | ||
const app = new Koa() | ||
const router = new Router({ | ||
prefix: '/user' | ||
}) | ||
|
||
router.get('/:id', (ctx, next) => { | ||
throw new Error() | ||
}) | ||
|
||
app.silent = true | ||
app | ||
.use(router.routes()) | ||
.use(router.allowedMethods()) | ||
|
||
agent | ||
.use(traces => { | ||
expect(traces[0][0]).to.have.property('resource', 'GET /user/:id') | ||
expect(traces[0][0].meta).to.have.property('http.url', `http://localhost:${port}/user/123`) | ||
expect(traces[0][0].error).to.equal(1) | ||
}) | ||
.then(done) | ||
.catch(done) | ||
|
||
appListener = app.listen(port, 'localhost', () => { | ||
axios | ||
.get(`http://localhost:${port}/user/123`) | ||
.catch(() => {}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.