Azure Functions Go 的追踪

Instana Go Collector 支持对使用 Go 编写的 Azure 函数进行追踪。

处理程序需要通过 包 github.com/instana/go-sensor/instrumentation/instaazurefunction 进行instrumentation,以便收集和发送跟踪数据。

若要使用 Instana用 Go 编写的 Azure 函数进行跟踪,请参阅以下章节:

支持的运行时

  • Go 版本 1.13 或更高版本

设置对 Go Azure 函数的跟踪

从版本 开始, Instana v1.49.0Go 的运行中传感器可以检测到服务正在 Azure 上运行,并切换至无服务器模式。 进程内传感器不会将收集到的跟踪数据发送给主机代理,而是直接将其提交至 Instana 的无服务器接收端点(该端点 INSTANA_ENDPOINT_URL 在环境变量中指定),并使用在环境变量中定义的代理 INSTANA_AGENT_KEY密钥进行提交。

要确保您正在使用最新版本的 github.com/instana/go-sensor,请检查项目中的 go.mod 文件或运行以下命令:

go get github.com/instana/go-sensor@latest
 

然后,您可以下载最新版本的 Go 进程内传感器,并在文件 go.mod 中更新所需版本。

配置

要发送收集的跟踪,您需要提供具有三个环境变量的 Azure 函数:

  • INSTANA_ENDPOINT_URL: 设置为您的 Instana 后端端点的 URL。
  • INSTANA_AGENT_KEY: 设置为 代理程序密钥
  • INSTANA_TIMEOUT: 根据配置的区域设置为相应的超时值。

要在“ Azure ”函数中向函数应用提供这些值,请在“ Instana ”用户界面中转至 “ Azure ”函数的“设置” > “配置 ”页面,然后执行以下操作:

  1. 单击 新建应用程序设置
  2. 在 " 添加/编辑应用程序设置 " 对话框中,提供 名称 字段的值,然后单击 确定

用法

截至今日,微软尚未提供用于在 Golang 中开发 Azure 函数的官方SDK。 相反, Azure 为不同类型的触发器提供了自定义处理程序,这些处理程序可用于开发 Azure 函数。 如需更多信息,请参阅 Microsoft 文档。 若要追踪使用自定义处理程序的 Azure 函数,您需要对该处理程序进行性能监控。 github.com/instana/go-sensor/instrumentation/instaazurefunction 这是一个用于对处理程序代码进行性能监控的库,它提供了用于实现监控的中间件封装。

要将 github.com/instana/go-sensor/instrumentation/instaazurefunction 添加到项目,请从包含 go.mod 文件的目录运行以下命令:

go get github.com/instana/go-sensor/instrumentation/instaazurefunction
 

然后,将检测模块添加到项目依赖关系列表和主 github.com/instana/go-sensor 进程内传感器。

为处理函数添加监控代码

一个典型的、使用 ` HTTP ` 触发器且用 ` Go ` 编写的 ` Azure ` 函数如下所示:

package main

import (
        "net/http"
)

func main() {
        http.HandleFunc("/api/azf-test", handlerFn)
}

func handlerFn(w http.ResponseWriter, r *http.Request) {
        // ...
}
 

处理函数与 Go 中的基本 HTTP 处理函数完全相同。 要使用 来对处理函数进行 instaazurefunctioninstrumentation,请使用 包裹该 instaazurefunction.WrapFunctionHandler() 处理函数,因此之前的代码将变为如下所示:

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) {
        // ...
}
 

基础架构指标

要收集 Azure 环境的运行时度量值和其他元数据,您需要使用 Azure 预订详细信息来配置代理程序。 如需更多信息,请参阅《 监控 Azure Functions 服务 》。 配置完成后,代理将收集指标,相关信息将与跟踪数据一同显示在 Instana 仪表盘上。