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

fix koa integration returning 404 with legacy middleware #393

Merged
merged 3 commits into from
Dec 12, 2018
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"jsdoc:watch": "cd docs && yarn && ./node_modules/.bin/gulp jsdoc:watch",
"lint": "eslint . && node scripts/check_licenses.js",
"services": "node ./scripts/install_plugin_modules && node test/setup/services",
"tdd": "yarn services && mocha --watch 'test/setup/**/*.js'",
"test": "yarn services && cov8 --include \"src/**/*.js\" -- mocha --exit 'test/setup/all.js' 'test/**/*.spec.js'",
"tdd": "yarn services && NO_DEPRECATION=* mocha --watch 'test/setup/**/*.js'",
"test": "yarn services && NO_DEPRECATION=* cov8 --include \"src/**/*.js\" -- mocha --exit 'test/setup/all.js' 'test/**/*.spec.js'",
"test:core": "mocha --exit --exclude \"test/plugins/**/*.spec.js\" --file test/setup/core.js \"test/**/*.spec.js\"",
"test:plugins": "yarn services && cov8 --include \"src/**/*.js\" -- mocha --exit --file \"test/setup/all.js\" \"test/plugins/@($(echo $PLUGINS)).spec.js\" \"test/plugins/@($(echo $PLUGINS))/**/*.spec.js\"",
"test:plugins": "yarn services && NO_DEPRECATION=* cov8 --include \"src/**/*.js\" -- mocha --exit --file \"test/setup/all.js\" \"test/plugins/@($(echo $PLUGINS)).spec.js\" \"test/plugins/@($(echo $PLUGINS))/**/*.spec.js\"",
"leak:core": "node ./scripts/install_plugin_modules && (cd test/leak && yarn) && NODE_PATH=./test/leak/node_modules node --no-warnings ./node_modules/.bin/tape 'test/leak/{,!(node_modules|plugins)/**/}/*.js'",
"leak:plugins": "yarn services && (cd test/leak && yarn) && NODE_PATH=./test/leak/node_modules node --no-warnings ./node_modules/.bin/tape \"test/leak/plugins/@($(echo $PLUGINS)).js\""
},
Expand Down
9 changes: 7 additions & 2 deletions src/plugins/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ function createWrapUse (tracer, config) {
}

return function wrapUse (use) {
return function useWithTrace (fn) {
return function useWithTrace () {
if (!this._datadog_trace_patched) {
this._datadog_trace_patched = true
use.call(this, ddTrace)
}

return use.call(this, function (ctx, next) {
const result = use.apply(this, arguments)
const fn = this.middleware.pop()

this.middleware.push(function (ctx, next) {
web.reactivate(ctx.req)
return fn.apply(this, arguments)
})

return result
}
}
}
Expand Down
31 changes: 30 additions & 1 deletion test/plugins/koa.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Plugin', () => {
before(() => agent.load(plugin, 'koa'))
after(() => agent.close())

it('should do automatic instrumentation on middleware', done => {
it('should do automatic instrumentation on 2.x middleware', done => {
const app = new Koa()

app.use((ctx) => {
Expand Down Expand Up @@ -60,6 +60,35 @@ describe('Plugin', () => {
})
})

it('should do automatic instrumentation on 1.x middleware', done => {
const app = new Koa()

app.use(function * (next) {
this.body = ''
yield next
})

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()

Expand Down