Azure Functions Tracing for Go
Instana Go Collector supports the tracing of Azure functions that are written in Go. Handlers need to be instrumented by using the github.com/instana/go-sensor/instrumentation/instaazurefunction
package to collect and send trace data.
To use Instana for tracing of Azure functions that are written in Go, see the following sections:
- Supported Runtimes
- Set up the tracing of Go Azure functions
- Configuration
- Usage
- Infrastructure Metrics
Supported Runtimes
- Go versions
1.13
or later
Set up the tracing of Go Azure functions
From version v1.49.0
, Instana Go in-process sensor can detect that a service is running on Azure and switch to serverless mode. Instead of sending collected
traces to the host agent, the in-process sensor submits the collected traces directly to Instana serverless acceptor endpoint that is specified in the INSTANA_ENDPOINT_URL
environment variable by using an agent key, which is defined
in the environment variable INSTANA_AGENT_KEY
.
To make sure you're using the latest version of github.com/instana/go-sensor
, check the go.mod
file in your project or run the following command:
go get github.com/instana/go-sensor@latest
Then, you can download the latest version of Go in-process sensor, and update the required version in the go.mod
file.
Configuration
To send collected traces, you need to provide an Azure function with three environment variables:
INSTANA_ENDPOINT_URL
: Sets to the URL of your Instana backend endpoint.INSTANA_AGENT_KEY
: Sets to your Agent key.INSTANA_TIMEOUT
: Sets to an appropriate timeout value depending on the zone that you have configured.
To provide these values to the function app in Azure Function, go to the Azure Function Settings >Configuration page in the Instana UI, and then complete the following actions:
- Click New application setting.
- In the Add/Edit application setting dialog, provide values for the name and value fields, and click Ok.
Usage
For developing Azure functions in Golang, no official SDK from Microsoft exists till today. Instead, Azure provides custom handlers for different types of triggers, which can be used to develop Azure functions. For more information, see Microsoft documentation.
To trace an Azure function that uses a custom handler, you need to instrument the handler. github.com/instana/go-sensor/instrumentation/instaazurefunction
is an instrumentation library that provides middleware wrappers for instrumenting the handler code.
To add github.com/instana/go-sensor/instrumentation/instaazurefunction
to your project, run the following command from the directory that contains the go.mod
file:
go get github.com/instana/go-sensor/instrumentation/instaazurefunction
Then, the instrumentation module is added to your project dependency list and the main github.com/instana/go-sensor
in-process sensor.
Instrumenting a handler function
A typical Azure function with HTTP trigger that is written in Go looks as follows:
package main
import (
"net/http"
)
func main() {
http.HandleFunc("/api/azf-test", handlerFn)
}
func handlerFn(w http.ResponseWriter, r *http.Request) {
// ...
}
A handler function is exactly like a basic HTTP handler in Go. To instrument a handler function by using instaazurefunction
, wrap the handler by using the instaazurefunction.WrapFunctionHandler()
,
so the earlier code changes to as follows:
package main
import (
"net/http"
instana "github.com/instana/go-sensor"
"github.com/instana/go-sensor/instrumentation/instaazurefunction"
)
// This example demonstrates how to instrument a custom handler for Azure Functions
func main() {
// Initialize a new sensor.
sensor := instana.NewSensor("my-azf-sensor")
// Instrument your handler before passing it to the http router.
http.HandleFunc("/api/azf-test", instaazurefunction.WrapFunctionHandler(sensor, handlerFn))
}
func handlerFn(w http.ResponseWriter, r *http.Request) {
// ...
}
Infrastructure Metrics
To collect the runtime metrics of the Azure environment and additional metadata, you need to configure the agent with the Azure subscription details. For more information, see Monitoring Azure Functions Service. Once configured, the agent collects the metrics, and the information will be available on the Instana dashboard along with the trace data.