Integrate OpenTelemetry Node.js on Google Cloud Functions
The @dynatrace/opentelemetry-gcf
module provides APIs for tracing Node.js on Google Cloud Functions (GCF).
So far, only HTTP triggers are supported.
Installation
To set up OpenTelemetry Node.js integration on Google Cloud Functions, run the command below.
npm install --save @dynatrace/opentelemetry-gcf
Usage
To export traces to Dynatrace
- Select one of the two ways below to initialize tracing.
NodeTracerProvider
used to initialize tracing is more lightweight thanNodeSDK
NodeSDK
typically is used if you're interested in additional OpenTelemetry signals such as metrics
- Start the root Google Cloud Function server span, using one of the two general patterns in OpenTelemetry below:
Cold start
Starting a Google Cloud Function span during cold starts produces additional HTTP requests to fetch metadata from your Google Cloud Platform environment and set the attributes required for Dynatrace to process the spans.
Span flush
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.
Note: Flushing spans in the function's code results in longer execution time, as this operation becomes part of the function's execution logic. Experiments have shown that, usually, not flushing spans explicitly results in correctly exported spans as well (enabled via continuous background export). However, since a Google Cloud Function could be suspended immediately after sending its response, it might result in span loss.
Caveats
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 the span export.
- Any network problem between the exporter and Dynatrace backend might also lead to unexpected high overhead.