地方エンジニアの学習日記

興味ある技術の雑なメモだったりを書いてくブログ。たまに日記とガジェット紹介。

opentelemetry-go-instrumentationで自動計装を試す

github.com

対象のアプリケーションを書く

net/httpを簡単なwebサーバをターゲットにします。

package main

import (
    "fmt"
    "net/http"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello")
}

func main() {
    http.HandleFunc("/", helloHandler)
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        panic(err)
    }
}

ビルド

$ git clone https://github.com/open-telemetry/opentelemetry-go-instrumentation.git
$ cd opentelemetry-go-instrumentation
$ apt-get update && apt-get install -y curl clang gcc llvm make libbpf-dev
$ make build

libbpf-devを入れることでbpf関連のパッケージが入ってくる

実行

opentelemetry-go-instrumentation

$ sudo OTEL_GO_AUTO_TARGET_EXE=/home/app/main OTEL_SERVICE_NAME=main OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 ./otel-go-instrumentation
{"level":"info","ts":1698717508.0797493,"caller":"opentelemetry/controller.go:61","msg":"got event","attrs":[{"Key":"http.method","Value":{"Type":"STRING","Value":"GET"}},{"Key":"http.target","Value":{"Type":"STRING","Value":"/"}}]}
l^C{"level":"info","ts":1698717669.6427422,"caller":"server/probe.go:248","msg":"closing net/http instrumentor"}
{"level":"info","ts":1698717669.696353,"caller":"instrumentors/runner.go:109","msg":"Cleaning bpffs"}
{"level":"error","ts":1698717669.6965513,"caller":"cli/main.go:61","msg":"instrumentation crashed","error":"context canceled","stacktrace":"main.main\n\t/otel/opentelemetry-go-instrumentation-0.7.0-alpha/cli/main.go:61\nruntime.main\n\t/usr/lib/go-1.21/src/runtime/proc.go:267"}

otel-collectorのログ

otel-collector_1  | {"resourceSpans":[{"resource":{"attributes":[{"key":"service.name","value":{"stringValue":"main"}},{"key":"telemetry.auto.version","value":{"stringValue":"v0.7.0-alpha"}},{"key":"telemetry.sdk.language","value":{"stringValue":"go"}}]},"scopeSpans":[{"scope":{"name":"net/http"},"spans":[{"traceId":"7df9d5c330b6f532d2b056f51c49a175","spanId":"5c5c30b04384c9c0","parentSpanId":"","name":"GET","kind":2,"startTimeUnixNano":"1698717508078647326","endTimeUnixNano":"1698717508078680189","attributes":[{"key":"http.method","value":{"stringValue":"GET"}},{"key":"http.target","value":{"stringValue":"/"}}],"status":{}}]}]}]}