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.
1go 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.
Install dependencies
Set up OpenTelemetry
Instrument the function entry point
Instrument outgoing requests
Install dependencies
Use the following commands to add the required OpenTelemetry and Google Cloud Platform 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.
1go get go.opentelemetry.io/otel2go get go.opentelemetry.io/otel/sdk3go get github.com/GoogleCloudPlatform/functions-framework-go4go get cloud.google.com/go/compute
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.
1package otelsetup23import (4 "go.opentelemetry.io/otel"5 "go.opentelemetry.io/otel/sdk/resource"6 sdk "go.opentelemetry.io/otel/sdk/trace"7 semconv "go.opentelemetry.io/otel/semconv/v1.10.0"89 dtTrace "github.com/dynatrace-oss/opentelemetry-exporter-go/core/trace"10)1112func InitializeTracing() (*dtTrace.DtTracerProvider, error) {13 r, err := resource.Merge(14 resource.Default(),15 resource.NewWithAttributes(16 semconv.SchemaURL,17 semconv.ServiceNameKey.String(serviceName),18 ),19 )2021 tracerProvider, err := dtTrace.NewTracerProvider(22 sdk.WithResource(r),23 )24 if err != nil {25 // handle error26 return nil, err27 }28 otel.SetTracerProvider(tracerProvider)2930 propagator, err := dtTrace.NewTextMapPropagator()31 if err != nil {32 // handle error33 return nil, err34 }35 otel.SetTextMapPropagator(propagator)3637 return tracerProvider, nil38}
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.
When your Cloud Function has an HTTP trigger, you can instrument it with the help of the otelhttp
package. First, install the package:
1go get go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp
For ease of use, you can create a wrapper function for your entry point. The following code samples contain several utility functions which will help you set the required attributes on your spans.
1package instrumentation23import (4 "net/http"56 "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"7 semconv "go.opentelemetry.io/otel/semconv/v1.7.0"8 "go.opentelemetry.io/otel/trace"910 "cloud.google.com/go/compute/metadata"1112 dtTrace "github.com/dynatrace-oss/opentelemetry-exporter-go/core/trace"13)1415type HttpHandler = func(w http.ResponseWriter, r *http.Request)1617func InstrumentHandler(18 functionName string,19 function HttpHandler,20 tracerProvider *dtTrace.DtTracerProvider) HttpHandler {2122 // get project id and region from Google Compute Engine metadata23 projectId, err := getProjectId()24 if err != nil {25 // handle error26 }27 region, err := getRegion()28 if err != nil {29 // handle error30 }3132 // set at least these required attributes33 opts := []trace.SpanStartOption{34 trace.WithAttributes(35 semconv.FaaSTriggerHTTP,36 semconv.CloudProviderGCP,37 semconv.CloudPlatformGCPCloudFunctions,38 semconv.FaaSIDKey.String(createFaasId(projectId, region, functionName)),39 semconv.FaaSNameKey.String(functionName),40 semconv.CloudRegionKey.String(region),41 ),42 }4344 // create instrumented handler45 handler := otelhttp.NewHandler(46 http.HandlerFunc(function), functionName, otelhttp.WithSpanOptions(opts...),47 )4849 return func(w http.ResponseWriter, r *http.Request) {50 // call your function handler51 handler.ServeHTTP(w, r)5253 // flush spans54 tracerProvider.ForceFlush(r.Context())55 }56}5758func getProjectId() (string, error) {59 return metadata.ProjectID()60}6162func getRegion() (string, error) {63 // Returned string has the format "projects/<numeric-project-id>/regions/<region>"64 fullRegion, err := metadata.Get("instance/region")65 if err != nil {66 return "", err67 }68 fullRegionSplit := strings.Split(fullRegion, "/")69 region := fullRegionSplit[len(fullRegionSplit)-1]70 return region, nil71}7273func createFaasId(projectId, region, functionName string) string {74 return fmt.Sprintf("//cloudfunctions.googleapis.com/projects/%s/locations/%s/functions/%s",75 projectId, region, functionName)76}
To put everything together, instrument your Google Cloud Function entry point as follows:
1package myfunction23import (4 "net/http"56 "instrumentation"7 "otelsetup"89 "github.com/GoogleCloudPlatform/functions-framework-go/functions"10)1112func init() {13 // see https://cloud.google.com/functions/docs/configuring/env-var#runtime_environment_variables_set_automatically14 functionName, ok := os.LookupEnv("K_SERVICE")15 if !ok {16 // function name not found; assign a default or treat as an error17 }18 if tracerProvider, err := otelsetup.InitializeTracing(functionName); err == nil {19 instrumentedHandler := instrumentation.InstrumentHandler(functionName, handler, tracerProvider)20 functions.HTTP(functionName, instrumentedHandler)21 } else {22 // handle error, or register function anyway without instrumentation23 }24}2526func handler(w http.ResponseWriter, r *http.Request) {27 // your code goes here28}
When using any version before version 0.37.0 of the otelhttp
package, it is required to explicitly call w.WriteHeader(...)
or w.Write(...)
in your HTTP function handler. Other methods which write content to the ResponseWriter
, such as fmt.Fprint(w, "OK")
, are also valid. Your function handler should then look something like this:
1func handler(w http.ResponseWriter, r *http.Request) {2 // your code goes here34 // content or a status code must be written to the ResponseWriter5 w.WriteHeader(http.StatusOK)6}
For instructions on how to deploy your Cloud Function, see Create and deploy a Cloud Function by using the Google Cloud CLI.
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.