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

[DO NOT MERGE] env var to disable all middleware spans (v5.x image) #5052

Draft
wants to merge 1 commit into
base: v5.x
Choose a base branch
from
Draft
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
313 changes: 159 additions & 154 deletions packages/datadog-plugin-express/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1502,209 +1502,214 @@
})
})

describe('with configuration for middleware disabled', () => {
before(() => {
return agent.load(['express', 'http'], [{
middleware: false
}, { client: false }])
})

after(() => {
return agent.close({ ritmReset: false })
})
middlewareDisabledTest('plugin', [{ middleware: false }, { client: false }], {})
middlewareDisabledTest('tracer', [{}, { client: false }], { middleware: false })

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

it('should not activate a scope per middleware', done => {
const app = express()

let span

app.use((req, res, next) => {
span = tracer.scope().active()
next()
function middlewareDisabledTest(description, pluginConfig, tracerConfig) {
describe(`with configuration for middleware disabled via ${description} config`, () => {
before(() => {
return agent.load(['express', 'http'], pluginConfig, tracerConfig)
})

app.get('/user', (req, res) => {
res.status(200).send()
try {
expect(tracer.scope().active()).to.equal(span).and.to.not.be.null
done()
} catch (e) {
done(e)
}
after(() => {
return agent.close({ ritmReset: false })
})

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

axios.get(`http://localhost:${port}/user`)
.catch(done)
beforeEach(() => {
express = require(`../../../versions/express@${version}`).get()
console.log('loaded express', description)
})
})

it('should not do automatic instrumentation on middleware', done => {
const app = express()
it('should not activate a scope per middleware', done => {
const app = express()

app.use((req, res, next) => {
next()
})
let span

app.get('/user', (req, res, next) => {
res.status(200).send()
})

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port
app.use((req, res, next) => {
span = tracer.scope().active()
next()
})

agent
.use(traces => {
const spans = sort(traces[0])
app.get('/user', (req, res) => {
res.status(200).send()
try {
expect(tracer.scope().active()).to.equal(span).and.to.not.be.null
done()
} catch (e) {
done(e)
}
})

expect(spans[0]).to.have.property('resource', 'GET /user')
expect(traces.length).to.equal(1)
})
.then(done)
.catch(done)
appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

axios.get(`http://localhost:${port}/user`)
.catch(done)
axios.get(`http://localhost:${port}/user`)
.catch(done)
})
})
})

it('should handle error status codes', done => {
const app = express()
it('should not do automatic instrumentation on middleware', done => {
const app = express()

app.use((req, res, next) => {
next()
})
app.use((req, res, next) => {
next()
})

app.get('/user', (req, res) => {
res.status(500).send()
})
app.get('/user', (req, res, next) => {
res.status(200).send()
})

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port
appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

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

expect(spans[0]).to.have.property('error', 1)
expect(spans[0]).to.have.property('resource', 'GET /user')
expect(spans[0].meta).to.have.property('http.status_code', '500')
expect(spans[0].meta).to.have.property('component', 'express')
expect(spans[0]).to.have.property('resource', 'GET /user')
expect(traces.length).to.equal(1)
})
.then(done)

Check failure on line 1572 in packages/datadog-plugin-express/test/index.spec.js

View workflow job for this annotation

GitHub Actions / lint

Missing space before function parentheses
.catch(done)

done()
axios.get(`http://localhost:${port}/user`)
.catch(done)
})

axios
.get(`http://localhost:${port}/user`, {
validateStatus: status => status === 500
})
.catch(done)
})
})

it('should only handle errors for configured status codes', done => {
const app = express()
it('should handle error status codes', done => {
const app = express()

app.use((req, res, next) => {
next()
})
app.use((req, res, next) => {
next()

Check failure on line 1584 in packages/datadog-plugin-express/test/index.spec.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
})

app.get('/user', (req, res) => {
res.statusCode = 400
throw new Error('boom')
})
app.get('/user', (req, res) => {
res.status(500).send()
})

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port
appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

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

expect(spans[0]).to.have.property('error', 0)
expect(spans[0]).to.have.property('error', 1)
expect(spans[0]).to.have.property('resource', 'GET /user')
expect(spans[0].meta).to.have.property('http.status_code', '400')
expect(spans[0].meta).to.have.property('http.status_code', '500')
expect(spans[0].meta).to.have.property('component', 'express')
})
.then(done)
.catch(done)

axios
.get(`http://localhost:${port}/user`, {
validateStatus: status => status === 400
done()
})
.catch(done)

axios
.get(`http://localhost:${port}/user`, {
validateStatus: status => status === 500
})
.catch(done)
})
})
})

it('should handle middleware errors', done => {
const app = express()
const error = new Error('boom')
it('should only handle errors for configured status codes', done => {
const app = express()

app.use((req, res) => { throw error })
// eslint-disable-next-line n/handle-callback-err
app.use((error, req, res, next) => res.status(500).send())
app.use((req, res, next) => {
next()
})

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port
app.get('/user', (req, res) => {
res.statusCode = 400
throw new Error('boom')
})

agent
.use(traces => {
const spans = sort(traces[0])
appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

expect(spans[0]).to.have.property('error', 1)
expect(spans[0].meta).to.have.property(ERROR_TYPE, error.name)
expect(spans[0].meta).to.have.property(ERROR_MESSAGE, error.message)
expect(spans[0].meta).to.have.property(ERROR_STACK, error.stack)
expect(spans[0].meta).to.have.property('component', 'express')
})
.then(done)
.catch(done)
agent
.use(traces => {
const spans = sort(traces[0])

axios
.get(`http://localhost:${port}/user`, {
validateStatus: status => status === 500
})
.catch(done)
expect(spans[0]).to.have.property('error', 0)
expect(spans[0]).to.have.property('resource', 'GET /user')
expect(spans[0].meta).to.have.property('http.status_code', '400')
expect(spans[0].meta).to.have.property('component', 'express')
})
.then(done)
.catch(done)

axios
.get(`http://localhost:${port}/user`, {
validateStatus: status => status === 400
})
.catch(done)
})
})
})

it('should handle request errors', done => {
const app = express()
const error = new Error('boom')
it('should handle middleware errors', done => {
const app = express()
const error = new Error('boom')

app.use(() => { throw error })
app.use((req, res) => { throw error })
// eslint-disable-next-line n/handle-callback-err
app.use((error, req, res, next) => res.status(500).send())

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port
appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

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

expect(spans[0]).to.have.property('error', 1)
expect(spans[0].meta).to.have.property(ERROR_TYPE, error.name)
expect(spans[0].meta).to.have.property(ERROR_MESSAGE, error.message)
expect(spans[0].meta).to.have.property(ERROR_STACK, error.stack)
expect(spans[0].meta).to.have.property('http.status_code', '500')
expect(spans[0].meta).to.have.property('component', 'express')
})
.then(done)
.catch(done)
expect(spans[0]).to.have.property('error', 1)
expect(spans[0].meta).to.have.property(ERROR_TYPE, error.name)
expect(spans[0].meta).to.have.property(ERROR_MESSAGE, error.message)
expect(spans[0].meta).to.have.property(ERROR_STACK, error.stack)
expect(spans[0].meta).to.have.property('component', 'express')
})
.then(done)
.catch(done)

axios
.get(`http://localhost:${port}/user`, {
validateStatus: status => status === 500
})
.catch(done)
axios
.get(`http://localhost:${port}/user`, {
validateStatus: status => status === 500
})
.catch(done)
})
})

it('should handle request errors', done => {
const app = express()
const error = new Error('boom')

app.use(() => { throw error })

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

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

expect(spans[0]).to.have.property('error', 1)
expect(spans[0].meta).to.have.property(ERROR_TYPE, error.name)
expect(spans[0].meta).to.have.property(ERROR_MESSAGE, error.message)
expect(spans[0].meta).to.have.property(ERROR_STACK, error.stack)
expect(spans[0].meta).to.have.property('http.status_code', '500')
expect(spans[0].meta).to.have.property('component', 'express')
})
.then(done)
.catch(done)

axios
.get(`http://localhost:${port}/user`, {
validateStatus: status => status === 500
})
.catch(done)
})
})
})
})
}

})
})
})
2 changes: 1 addition & 1 deletion packages/datadog-plugin-web/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class WebPlugin extends Plugin {
}

configure (config) {
return super.configure(web.normalizeConfig(config))
return super.configure(web.normalizeConfig(config, this._tracerConfig))
}

setFramework (req, name, config) {
Expand Down
Loading
Loading