Skip to content

Commit

Permalink
handler-fetch: configure server-timing
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicm67 committed Jun 12, 2024
1 parent f0452b1 commit 6a4dcfc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-ducks-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trifid-handler-fetch": minor
---

Returns `Server-Timing` as response header containing the duration of the request to perform.
5 changes: 5 additions & 0 deletions packages/handler-fetch/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check

import { Worker } from 'node:worker_threads'
import { performance } from 'node:perf_hooks'
import { v4 as uuidv4 } from 'uuid'
import { waitForVariableToBeTrue } from './lib/utils.js'

Expand Down Expand Up @@ -160,8 +161,12 @@ export const factory = async (trifid) => {
logger.debug(`Received query: ${query}`)

try {
const start = performance.now()
const { response, contentType } = await handleQuery(query)
const end = performance.now()
const duration = end - start
reply.type(contentType)
reply.header('Server-Timing', `handler-fetch;dur=${duration};desc="Query execution time"`)
logger.debug(`Sending the following ${contentType} response:\n${response}`)
reply.status(200).send(response)
} catch (error) {
Expand Down

0 comments on commit 6a4dcfc

Please sign in to comment.