The dynatrace-oss/opentelemetry-exporter-go
package provides an API for tracing Go code on Google Cloud Functions. This package provides a way to instrument your code with Dynatrace-enhanced OpenTelemetry traces.
We recommend using this package. As an alternative, you can instrument your Google Cloud Functions with plain OpenTelemetry, see Trace Google Cloud Functions in Go with OpenTelemetry.
Ensure that you have followed the initial configuration steps described in Set up OpenTelemetry monitoring for Google Cloud Functions before using the packages below.
Run the following command in the root directory of your Google Cloud Function project to install the latest version of the dynatrace-oss/opentelemetry-exporter-go
package from GitHub.
go get github.com/dynatrace-oss/opentelemetry-exporter-go/core
Note that this package by itself is not enough to get Dynatrace-enhanced traces. Further initialization code and dependencies need to be added, as described below.
Follow the steps below to instrument your Google Cloud Functions.
Install dependencies
Set up OpenTelemetry
Instrument the function entry point
Instrument outgoing requests
Use the following commands to add the required OpenTelemetry and Google Cloud dependencies to your function. If your project already contains any of these dependencies, you might want to skip the corresponding go get
call, otherwise, it will upgrade you to the latest version of the dependency.
go get go.opentelemetry.io/otelgo get go.opentelemetry.io/otel/sdkgo get github.com/GoogleCloudPlatform/functions-framework-gogo get cloud.google.com/go/compute
Some initialization code is required to set up OpenTelemetry before you can start instrumenting your Cloud Function. The following code snippet will initialize the required DtTracerProvider
and DtTextMapPropagator
instances, and register them via the OpenTelemetry API.
package otelsetupimport ("go.opentelemetry.io/otel""go.opentelemetry.io/otel/attribute""go.opentelemetry.io/otel/sdk/resource"sdk "go.opentelemetry.io/otel/sdk/trace"semconv "go.opentelemetry.io/otel/semconv/v1.10.0"dtTrace "github.com/dynatrace-oss/opentelemetry-exporter-go/core/trace")func InitializeTracing() (*dtTrace.DtTracerProvider, error) {r, err := resource.Merge(resource.Default(),resource.NewWithAttributes(semconv.SchemaURL,attribute.String("my.resource.attribute", "My Resource"),),)tracerProvider, err := dtTrace.NewTracerProvider(sdk.WithResource(r),)if err != nil {// handle errorreturn nil, err}otel.SetTracerProvider(tracerProvider)propagator, err := dtTrace.NewTextMapPropagator()if err != nil {// handle errorreturn nil, err}otel.SetTextMapPropagator(propagator)return tracerProvider, nil}
There are several trigger types for Google Cloud Functions. See below how to instrument Cloud Functions with HTTP triggers and Pub/Sub triggers.
For instructions on how to deploy your Cloud Function, see Create and deploy a Cloud Function by using the Google Cloud CLI.
If you have any outgoing requests in your Cloud Function, you can instrument them as well to achieve full end-to-end tracing. Your requests can be instrumented by using instrumentation libraries provided by OpenTelemetry.
Instrumenting outgoing requests works equivalently with the Dynatrace-enhanced tracing package and plain OpenTelemetry. For instructions on how to instrument outgoing requests, see Trace Google Cloud Functions in Go with OpenTelemetry.
In the code snippets above, ForceFlush
is explicitly called after every function invocation to ensure that spans are exported properly.
Because flushing spans becomes part of the function's execution logic, it results in longer execution times. To avoid this, you can omit the call to ForceFlush
. 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.