Integrate on Google Cloud Functions Python
The dynatrace-opentelemetry-gcf
package provides APIs for tracing Python Google Cloud Functions (GCF).
Prerequisites
Ensure that you have followed the initial configuration steps described in Set up OpenTelemetry monitoring for Google Cloud Functions before using the packages below.
- dynatrace-opentelemetry-gcf version 1.247+
Installation
To set up OpenTelemetry Python integration on Google Cloud Functions, add the following line to the requirements.txt
file of your function:
dynatrace-opentelemetry-gcf
This adds the latest version of the dynatrace-opentelemetry-gcf
package as a dependency to your function. For more information about managing dependencies, consult the GCF documentation for Python.
Trace export
To export traces to Dynatrace, you need to initialize tracing and then instrument your handler function.
Initialize tracing
Select one of the following ways to initialize tracing:
-
configure_dynatrace
function—This is the recommended option unless you need to manually set up tracing components. -
Manual tracing setup—This allows for a more fine-grained setup of tracing components.
The tracing setup code should be implemented to set up tracing only once before any other third-party module is imported. If you use isort
to sort your imports, we suggest that you deactivate it while importing the tracing setup module, as shown in the following example:
# isort: off
import setup_tracing # import the module containing your setup code
# isort: on
# import other modules
Instrument a handler function
Use the wrap_handler
decorator to instrument your handler function as shown in the following example:
import flask
from dynatrace.opentelemetry.gcf import wrap_handler
@wrap_handler
def handler(request: flask.Request) -> flask.Response:
# From here the created span is available in the OpenTelemetry context as the current span.
# do something ...
return flask.Response("Hello World", 200)
Cold start
When the wrapped handler is invoked for the first time after cold start, the decorator will make additional HTTP requests to fetch metadata from your Google Cloud Platform environment. This metadata is used to set the required attributes for Dynatrace to process the span.
Span flush
By default, the wrap_handler
decorator automatically performs a flush operation when the decorated function exits to ensure that spans are exported properly. However, flushing spans results in longer execution time, because this operation becomes part of the function's execution logic.
By providing an additional parameter to the decorator, @wrap_handler(flush_on_exit=False)
, you can disable the flushing after every invocation.
Experiments have shown that not flushing spans explicitly usually results in correctly exported spans as well (via periodic background exports).
However, since a Google Cloud Function could be suspended immediately after sending its response, it might result in span loss.
Dynatrace overhead
- Because span export and metadata fetch take some time during cold starts, they increase the duration of the function and subsequently increase costs.
- Pay attention to infrequently invoked functions (usually with cold starts), which might require more time for the TCP handshake during span export.
- Any network problem between the exporter and Dynatrace backend might also lead to unexpectedly high overhead.
Limitations
DtSpanProcessor
only works together withDtSampler
. Make sure to setDtSampler
as a sampler when manually setting up tracing; spans might not be exported otherwise.