Skip to content

Commit

Permalink
Merge pull request #405 from zazuko/server-timing
Browse files Browse the repository at this point in the history
Send Server-Timing responses for SPARQL query responses
  • Loading branch information
ludovicm67 committed Jun 12, 2024
2 parents 1a04ad8 + 6a4dcfc commit dd8b6bf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-papayas-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zazuko/trifid-plugin-sparql-proxy": minor
---

Returns `Server-Timing` as response header containing the duration of the request to the configured endpoint.
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
6 changes: 6 additions & 0 deletions packages/sparql-proxy/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check

import { Readable } from 'node:stream'
import { performance } from 'node:perf_hooks'
import { sparqlGetRewriteConfiguration } from 'trifid-core'
import replaceStream from 'string-replace-stream'

Expand Down Expand Up @@ -155,11 +156,15 @@ const factory = async (trifid) => {
if (authorizationHeader) {
headers.Authorization = authorizationHeader
}

const start = performance.now()
const response = await fetch(options.endpointUrl, {
method: 'POST',
headers,
body: new URLSearchParams({ query }),
})
const end = performance.now()
const duration = end - start

const contentType = response.headers.get('content-type')

Expand All @@ -175,6 +180,7 @@ const factory = async (trifid) => {

return reply
.status(response.status)
.header('Server-Timing', `sparql-proxy;dur=${duration};desc="Querying the endpoint"`)
.header('content-type', contentType)
.send(responseStream)
} catch (error) {
Expand Down

0 comments on commit dd8b6bf

Please sign in to comment.