Integrate on Google Cloud Functions Node.js

The @dynatrace/opentelemetry-gcf module provides APIs for tracing Node.js on Google Cloud Functions (GCF).

Prerequisites

Make sure you have followed the instructions on how to integrate OpenTelemetry on Google Cloud Functions.

  • So far, only HTTP triggers are supported.
  • Cloud Function product version: 1st gen, 2nd gen

Installation

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.

npm install --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

  1. 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.
  2. Start the root Google Cloud Function server span, using one of the two general patterns in OpenTelemetry below.

Compatibility

OneAgent version
OpenTelemetry API
OpenTelemetry SDK
1.243 - 1.255
1.x.y
1.0.x
1.257
1.x.y
1.0.x - 1.7.x
1.259+
1.x.y
1.0.x - 1.8.x
1.261+
1.x.y
1.0.x - 1.9.x
1.265+
1.x.y
1.0.x - 1.10.x
1.273+
1.x.y
1.0.x - 1.15.x
1.279+
1.x.y
1.0.x - 1.17.x
1.283+
1.x.y
1.0.x - 1.18.x
1.285+
1.x.y
1.0.x - 1.20.x
1.289+
1.x.y
1.0.x - 1.22.x
1.293+
1.x.y
1.0.x - 1.24.x
1.297+
1.x.y
1.0.x - 1.25.x

Cold start

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.

    For details, see Function execution timeline.

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 span export.
  • Any network problem between the exporter and Dynatrace backend might also lead to unexpectedly high overhead.