0

I use Google Firebase Cloud Function.

Just to be clear, I'm ok with cold start and I don't want to reduce the start time.

I want to get a timestamp corresponding to when the request is received before the function start.

The final goal is to estimate duration.

Our server save a record in the database with a timestamp then we call an external service.

This external API will call a webhook on our server to update states.

We want to measure the duration between the saved record and the webhook call.

It works most of the times, but when cloud function hit cold start, it would affect the stats.

I looked for variable with this information in documentation but not found anything.

I checked headers from the external service for a timestamp but there is nothing there too.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
rm4
  • 711
  • 4
  • 15
  • 1
    Which cloud function are we talking about? Callable function or HTTP triggered or any background function? Also do you need the timestamp when the function was triggered? – Dharmaraj Jul 18 '21 at 14:58
  • @Dharmaraj I use HTTP triggered and yes that's it. The timestamp when the function was initially triggered would be enough accurate. – rm4 Jul 18 '21 at 15:14

1 Answers1

0

That doesn't seem to be possible. There is no standard header for date and time in a HTTP request. Easiest way would be to pass your own header in your API request. You can pass an UNIX timestamp in the request and just to make sure someone has not passed wrong timestamp i.e. future or past time you can have a check in your cloud function.

if (Date.now() - req.headers.req_time > 3000) {
  // greater than 3 seconds - something is wrong
}

That may not be the best solution but at the same time there is no standard solution at least for the time being.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • It is an external service and I have no control over http headers. – rm4 Jul 18 '21 at 18:41
  • @rm4 so you are saying that service triggers the function. Can you tell name of it so I can look around if there's a way? If not then there's no option I believe – Dharmaraj Jul 18 '21 at 19:08
  • It's twilio the notification system. They call webhooks when sms are delivered. – rm4 Jul 18 '21 at 20:40