Integrate on Google Cloud Functions GoLang

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.

Prerequisites

Ensure that you have followed the initial configuration steps described in Set up OpenTelemetry monitoring for Google Cloud Functions before using the packages below.

  • Dynatrace version 1.222+
  • Cloud Functions Go Runtime 1.16+
  • Cloud Functions product version:
    • 1st gen
    • dynatrace-oss/opentelemetry-exporter-go version 1.267+ 2nd gen

Installation

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.

Usage

Follow the steps below to instrument your Google Cloud Functions.

Step 1 Install dependencies

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/otel
go get go.opentelemetry.io/otel/sdk
go get github.com/GoogleCloudPlatform/functions-framework-go
go get cloud.google.com/go/compute

Step 2 Set up OpenTelemetry

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 otelsetup
import (
"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 error
return nil, err
}
otel.SetTracerProvider(tracerProvider)
propagator, err := dtTrace.NewTextMapPropagator()
if err != nil {
// handle error
return nil, err
}
otel.SetTextMapPropagator(propagator)
return tracerProvider, nil
}

Step 3 Instrument the function entry point

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.

Step 4 Instrument outgoing requests

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.

Span flush

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.

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.