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

Send Server-Timing responses for SPARQL query responses #405

Merged
merged 2 commits into from
Jun 12, 2024
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
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
Loading