8

I am currently collecting and logging performance metrics of my function and the only metric I am missing is the startup duration (my function is pretty large, about 35MB compressed). I really need to know how long the startup takes and, even more important, how often it occurs (I have the feeling it is occurring way more often since my concurrent executions limit has been raised).

Is there any way to retrieve this information in Lambda? Or can someone think of a possibility to pass the current timestamp of an API Gateway call via e.g. the mapping template, so I can compute the startup time by myself?

birnbaum
  • 4,718
  • 28
  • 37

4 Answers4

13

AWS has recently introduced a Init Duration in cloudwatch logs alongside Billed Duration log for measuring a cold start lambda's before the actual invocation begins.

From the "Report" section inside Lambda Log Stream

Here's a sample log from one of my lambda -

Duration: 1200.50 ms 
Billed Duration: 1300 ms 
Memory Size: 3008 MB 
Max Memory Used: 308 MB 
Init Duration: 4317.73 ms
Sunny Sharma
  • 4,688
  • 5
  • 35
  • 73
priyesh.patel
  • 146
  • 1
  • 6
  • Is that verbatim? How is your Duration shorter than your Init Duration? – bmm6o Dec 31 '19 at 16:51
  • 1
    @bmm6o https://stackoverflow.com/questions/55412624/does-aws-lambda-charge-for-the-time-spent-initializing-code. The Init Duration is time taken by AWS to make allocation of containers, memory, pulling your image and calling handler function. So, you won't be charged for Init Duration. Init Duration is for the purpose where you need to understand that it is an added overhead on your performance per cold start request. Note - the DB connection initialisation and other global initialisation comes under billed duration. – priyesh.patel Jan 02 '20 at 06:26
  • I am able to filter this info using cloud watch log filter and the issue I am facing is that I am not able to assign the function name here as the function name is not being logged, and idea how can i assign the function name to the dimensions – Programming-Lover Dec 05 '22 at 13:55
7

Try this in Log insights Cloudwatch for a Lambda function:

fields @timestamp, @initDuration

    | filter @type = "REPORT"
    | sort @timestamp desc
chintan thakar
  • 1,440
  • 1
  • 15
  • 25
Mehul Karia
  • 71
  • 1
  • 1
1

There's no deterministic way to measure function startup time right now. At this time, your best bet would be to enable CloudWatch Logs for your API, and process the timestamps of the log events. It should be pretty obvious which calls trigger a function initialization as the time delta between the events before and after calling Lambda would be significantly higher than the average. Based on this you could generate some statistics on the ratio of initializations, and the average initialization time.

Thanks, Ryan

RyanG
  • 3,973
  • 25
  • 19
  • Ok, thanks! I am already doing that and it works fine. I was just curious if it was possible to somehow get this information into Lambda, because then I could very easily visualize it as a metric. It would be very cool to have the current timestamp in the set of variables in API Gateway! – birnbaum Mar 10 '16 at 09:06
  • We will add this to our backlog - thanks! If you don't mind including the client-side latencies, you could always generate a timestamp on the client-side and proxy it through to Lambda – RyanG Mar 11 '16 at 17:27
1

The answer is already listed, I am just showing it in a better format.

filter @type="REPORT" and ispresent(@initDuration)
| stats count() as coldStarts, avg(@initDuration), min(@initDuration), max(@initDuration) by bin(5m)
Jha Nitesh
  • 188
  • 1
  • 11