Skip to content

Commit

Permalink
Merge pull request #60 from omrilotan/2020-11-09-custom-name-and-desc…
Browse files Browse the repository at this point in the history
…ription

Allow for custom name and description
  • Loading branch information
yosuke-furukawa committed Nov 10, 2020
2 parents 8087095 + fb43461 commit b2305d9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ app.use(serverTiming({

## constructor(options)

- options.name: string, default `total`, name for the timing item
- options.description: string, default `Total Response Time`, explanation for the timing item
- options.total: boolean, default `true`, add total response time
- options.enabled: boolean | function, default `true`, enable server timing header. If a function is passed, it will be called with two arguments, `request` and `response`, and should return a boolean.
- options.autoEnd: boolean, default `true` automatically endTime is called if timer is not finished.
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const Timer = require('./timer')

module.exports = function serverTiming (options) {
const opts = Object.assign({
name: 'total',
description: 'Total Response Time',
total: true,
enabled: true,
autoEnd: true,
Expand Down Expand Up @@ -34,7 +36,7 @@ module.exports = function serverTiming (options) {
if (opts.total) {
const diff = process.hrtime(startAt)
const timeSec = (diff[0] * 1E3) + (diff[1] * 1e-6)
res.setMetric('total', timeSec, 'Total Response Time')
res.setMetric(opts.name, timeSec, opts.description)
}
timer.clear()

Expand Down
20 changes: 20 additions & 0 deletions test/express-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ test('express total response', () => {
})
})

test('custom timing name and description', () => {
const app = express()
app.use(serverTiming({
name: 'app',
description: 'Service Layer Response Time'
}))
app.use((req, res, next) => {
res.send('hello')
})
const server = app.listen(0, () => {
http.get(`http://localhost:${server.address().port}/`, mustCall((res) => {
const assertStream = new AssertStream()
assertStream.expect('hello')
res.pipe(assertStream)
assert(/app; dur=.*; desc="Service Layer Response Time"/.test(res.headers['server-timing']))
server.close()
}))
})
})

test('express add some custom server timing header', () => {
const app = express()
app.use(serverTiming())
Expand Down

0 comments on commit b2305d9

Please sign in to comment.