To set up OpenTelemetry Node.js integration on Google Cloud Functions, run the command below in the root directory of your Google Cloud Function project.
npminstall --save @dynatrace/opentelemetry-gcf
This will install the latest version of the @dynatrace/opentelemetry-gcf module from NPM. Note that this library by itself is not enough to start tracing your Google Cloud Functions.
See the Usage section below for the remaining required steps.
Usage
To export traces to Dynatrace
Select one of the two ways below to initialize tracing.
NodeTracerProvider used to initialize tracing is more lightweight than NodeSDK.
NodeSDK is typically used if you're interested in additional OpenTelemetry signals such as metrics.
Install the required OpenTelemetry packages with the command below.
Starting a Google Cloud Function span during cold starts produces additional HTTP requests to fetch metadata from your Google Cloud environment and set the attributes required for Dynatrace to process the spans.
Span flush
To ensure that spans are exported properly, you need to flush the spans before a function's response is sent to the client. For details on this limitation, see Signalling function termination.
You can use endHttpSpan() and flushSpans() separately instead of endHttpSpanAndFlush() when needed.
Flushing spans in the function's code results in longer execution times, as this operation becomes part of the function's execution logic. To avoid this, you can omit the flush operation. Spans will still be periodically exported in the background.
Because code running outside the function execution can be terminated at any time, it's discouraged by Google Cloud Functions.
Google Cloud Functions 1st gen
Background task execution after function invocation is not guaranteed without flushing spans and might result in span loss. In practice, samples have shown that not explicitly flushing spans usually still results in correctly exported spans.
Google Cloud Functions 2nd gen
Google Cloud Functions 2nd gen can handle multiple concurrent requests in a single function instance. The flush operation of one invocation can prolong the execution time of another function invocation.
Because function instances usually need to be kept idle for some time to handle multiple concurrent requests, you can disable the flushing of spans to improve performance. For details, see Instance lifecycle.
Note that idle function instances are not guaranteed to be allocated CPU unless their CPU allocation mode is set to CPU always allocated.
You need to pay special attention to cases like unhandled exceptions or function timeouts. If not handled properly, they could lead to a non-ended, and therefore non-exported, span.
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.