The @dynatrace/opentelemetry-gcf
module provides APIs for tracing Node.js on Google Cloud Functions (GCF).
Make sure you have followed the instructions on how to integrate OpenTelemetry on Google Cloud Functions.
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.
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.
npm install --save @opentelemetry/sdk-trace-node @opentelemetry/semantic-conventions
After you install the packages, initialize tracing using the following snippet as an example.
const { Resource } = require('@opentelemetry/resources');const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');const { DtSpanExporter, DtSpanProcessor, DtTextMapPropagator, DtSampler } = require('@dynatrace/opentelemetry-core');const processor = new DtSpanProcessor(new DtSpanExporter());const provider = new NodeTracerProvider({resource: new Resource({"my.resource.attribute": "My Resource",}),sampler: new DtSampler(),// for @opentelemetry/sdk-trace-node versions lower than 1.29.0 use `provider.addSpanProcessor(processor)` insteadspanProcessors: [processor]// ...other configurations});provider.register({propagator: new DtTextMapPropagator(),// ...other configurations});
Start the root Google Cloud Function server span, using one of the two general patterns in OpenTelemetry below.
Example that starts and immediately activates a span inside a Google Cloud Function:
const { startActiveHttpSpan, endHttpSpanAndFlush } = require('@dynatrace/opentelemetry-gcf');// ...tracing initialization codeasync function handler(req, res) {await startActiveHttpSpan(req, async (span) => {let error;try {// do something} catch (e) {error = e;}// status should be set before span endsres.status(error != null ? 500 : 200);/*** Span must be ended and flushed before handler sends response.* This limitiation comes from GCF, for details see:* https://cloud.google.com/functions/docs/concepts/nodejs-runtime#signal-termination*/await endHttpSpanAndFlush(span, res, error);res.send("hello world");});}
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.
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.
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.