Use distributed tracing

All inbound requests to a Cloud Function automatically generate a basic set of trace data to help you measure timing and identify causes of latency in your function.

For more detailed tracing, you can add additional instrumentation to your Cloud Function. This lets you measure the time it takes for the request to complete different sub-operations within your function, such as database queries, API requests, or complex business logic. Each of these layer-specific time measurements is a called a span.

You can view an analysis of your traces with Cloud Trace.

Trace sampling rate

Cloud Functions performs tracing at a maximum rate of one request per ten second interval for each Cloud Function instance. You can also force a particular request to be traced.

Billing charges

Automatically generated traces, whether sampled or forced, don't cause billing charges. However, if you use Cloud Trace libraries and add your own spans by correlating them to Cloud Functions provided spans, you are charged by Cloud Trace.

When to add instrumentation

If you need more trace information than the automatically-generated trace data, you must add instrumentation code to your service to enable additional tracing. For example, you need to add instrumentation to:

  • Create custom trace spans, such as monitoring how long it takes your service to get results back from API requests.
  • Propagate trace context so Cloud Trace shows the request flow across multiple services as a single request.

To add instrumentation, refer to Instrumenting tracing for applications.

View your traces

To view an analysis of your trace data, see viewing traces.