platform icon indicating copy to clipboard operation
platform copied to clipboard

Add into all headers x-process-time

Open haykeminyan opened this issue 1 year ago • 2 comments

Just noticed that when sending requests in headers doesn't exist useful middleware x-process-time which can be added into headers and into logs too. For future enhancement of the speed of requests and catching slow connections/requests. Feel free to ignore it :=)

haykeminyan avatar Feb 14 '24 21:02 haykeminyan

Right now we use server statistics on server, probable we need some stuff on client as well. If you have some usage examples of x-process-time it would be good to see. Thanks.

haiodo avatar Feb 15 '24 07:02 haiodo

Right now we use server statistics on server, probable we need some stuff on client as well. If you have some usage examples of x-process-time it would be good to see. Thanks.

unfortunately, I have only working example in FastApi which is not relevant here :=) BTW I have tried here and get some result. But it now make console.log with timing of request. Main problem that current headers can't be editable as they are immutable. If it is acceptable for you I will add for example for signUp and logIn functions. Example:

  try {
    // https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
    const start = performance.now()
    const response = await fetch(accountsUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(request)
    })

    // check time after sended request
    const end = performance.now()

    // calculate time
    const processingTime = end - start

    const result = await response.json()

    // create new headers with time which spent on sending request
    const newHeaders = new Headers(response.headers)
    // Append X-Process-Time header to the newHeaders object
    newHeaders.append('X-Process-Time', processingTime.toFixed(3) + 'ms')

    console.log('X-Process-Time', processingTime.toFixed(3) + 'ms')
    console.log('login result', result)
    return [result.error ?? OK, result.result]
  } catch (err) {
    return [unknownError(err), undefined]
  }
}

haykeminyan avatar Feb 15 '24 22:02 haykeminyan