Golang

本文档主要从 dd-trace-go 官方的 github 页面摘取了部分信息便于大家直接上手,如果碰到一些过不去的问题,可能是本文档更新滞后,建议参考原始文档。

安装依赖¶

安装 ddtrace golang SDK:

# 安装 tracing 库
go get gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer

# 安装 profiling 库
go get gopkg.in/DataDog/dd-trace-go.v1/profiler

# 其它跟组件有关的库,视情况而定,比如:
go get gopkg.in/DataDog/dd-trace-go.v1/contrib/gorilla/mux
go get gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http
go get gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql

我们可以从 contrib list 找到更多可用的 tracing SDK。

设置 DataKit¶

需先安装、启动 datakit,并开启 ddtrace 采集器

代码示例¶

以下代码演示了一个文件打开操作的 trace 数据收集。

main()
package main

import (
    "io/ioutil"
    "os"
    "time"

    "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
    "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func main() {
    tracer.Start(
        tracer.WithEnv("prod"),
        tracer.WithService("test-file-read"),
        tracer.WithServiceVersion("1.2.3"),
        tracer.WithGlobalTag("project", "add-ddtrace-in-golang-project"),
    )

    // end of app exit, make sure tracer stopped
    defer tracer.Stop()

    tick := time.NewTicker(time.Second)
    defer tick.Stop()

    // your-app-main-entry...
    for {
        runApp()
        runAppWithError()

        select {
        case <-tick.C:
        }
    }
}

func runApp() {
    var err error
    // Start a root span.
    span := tracer.StartSpan("get.data")
    defer span.Finish(tracer.WithError(err))

    // Create a child of it, computing the time needed to read a file.
    child := tracer.StartSpan("read.file", tracer.ChildOf(span.Context()))
    child.SetTag(ext.ResourceName, os.Args[0])

    // Perform an operation.
    var bts []byte
    bts, err = ioutil.ReadFile(os.Args[0])
    span.SetTag("file_len", len(bts))
    child.Finish(tracer.WithError(err))
}

func runAppWithError() {
    var err error
    // Start a root span.
    span := tracer.StartSpan("get.data")

    // Create a child of it, computing the time needed to read a file.
    child := tracer.StartSpan("read.file", tracer.ChildOf(span.Context()))
    child.SetTag(ext.ResourceName, "somefile-not-found.go")

    defer func() {
        child.Finish(tracer.WithError(err))
        span.Finish(tracer.WithError(err))
    }()

    // Perform an error operation.
    if _, err = ioutil.ReadFile("somefile-not-found.go"); err != nil {
        // error handle
    }
}

编译运行¶

go build main.go -o my-app
DD_AGENT_HOST=localhost DD_TRACE_AGENT_PORT=9529 ./my-app
go build main.go -o my-app.exe
$env:DD_AGENT_HOST="localhost"; $env:DD_TRACE_AGENT_PORT="9529"; .\my-app.exe

程序运行一段时间后,即可在观测云看到类似如下 trace 数据:

Golang 程序 trace 数据展示

支持的环境变量¶

以下环境变量支持在启动程序的时候指定 ddtrace 的一些配置参数,其基本形式为:

DD_XXX=<env-value> DD_YYY=<env-value> ./my-app
WithXXX()
DD_VERSIONDD_SERVICEDD_ENVDD_AGENT_HOSTlocalhostDD_TRACE_AGENT_PORTDD_DOGSTATSD_PORTDD_TRACE_SAMPLING_RULESsample_rate[0.0, 1.0]DD_TRACE_SAMPLE_RATE='[{"sample_rate": 0.2}]' ./my-appapp1.*abcDD_TRACE_SAMPLE_RATE='[{"service": "app1.*", "name": "b", "sample_rate": 0.1}, {"sample_rate": 0.2}]' ./my-appDD_TRACE_SAMPLE_RATEDD_TRACE_RATE_LIMITDD_TRACE_SAMPLE_RATEDD_TAGSlayer:api,team:intakelayer:api team:intakeDD_TRACE_STARTUP_LOGStrueDD_TRACE_DEBUGfalseDD_TRACE_ENABLEDtrueDD_SERVICE_MAPPINGmysql:mysql-service-name,postgres:postgres-service-namemysql:mysql-service-name postgres:postgres-service-name