Go 用 Application Real-Time Monitoring (ARMS) エージェントは、カスタム拡張機能を提供します。コードを変更せずにカスタム機能を追加できるため、リクエストパラメータとリクエスト本文に基づいてエラーの場所を特定しやすくなります。このトピックでは、Go 用 ARMS エージェントのカスタム拡張機能を使用して OpenTelemetry スパンを作成する方法について説明します。
前提条件
アプリケーションは Go V1.18 以降で実行されています。
Go アプリケーションが ARMS によって監視されている。
重要参照ドキュメントの要件に従って、コンパイルが
./instgo go build xxx
に変更されていることを確認してください。
手順
現在のプロジェクトのディレクトリの外側に、
hook
という名前のフォルダを作成し、go mod init hook
コマンドを実行して初期化します。hook
フォルダに、カスタム拡張機能を提供する次のコードを含むhook.go
という名前のファイルを作成します。package hook import ( "encoding/json" "fmt" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/api" "net/http" ) // hook 関数の最初の引数は api.CallContext で、その後にターゲット関数の引数が続きます。 func httpClientEnterHook(call api.CallContext, t *http.Transport, req *http.Request) { header, _ := json.Marshal(req.Header) fmt.Println("request header is ", string(header)) tracer := otel.GetTracerProvider().Tracer("") _, span := tracer.Start(context.Background(), "Client/User defined span") span.SetAttributes(attribute.String("client", "client-with-ot")) span.SetAttributes(attribute.Bool("user.defined", true)) span.End() } // hook 関数の最初の引数は api.CallContext で、その後にターゲット関数の戻り値の引数が続きます。 func httpClientExitHook(call api.CallContext, res *http.Response, err error) { header, _ := json.Marshal(res.Header) fmt.Println("response header is ", string(header)) }
スパンの作成を担当するコードセグメントは次のとおりです。
tracer := otel.GetTracerProvider().Tracer("") _, span := tracer.Start(context.Background(), "Client/User defined span") span.SetAttributes(attribute.String("client", "client-with-ot")) span.SetAttributes(attribute.Bool("user.defined", true)) span.End()
テストデモを開発します。
hook
フォルダとは別のディレクトリに、デモアプリケーションフォルダを作成し、go mod init demo
コマンドを実行して初期化します。demo
フォルダに、次のコードを含むmain.go
という名前のファイルを作成します。
プロジェクトがすでに OpenTelemetry に依存している場合は、_ "go.opentelemetry.io/otel"
を main.go
ファイルに追加する必要はありません。
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
_ "go.opentelemetry.io/otel"
)
func main() {
// リクエスト URL 。
req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://www.aliyun.com", nil)
req.Header.Set("otelbuild", "true")
client := &http.Client{}
resp, _ := client.Do(req)
// 関数が戻る前にレスポンス本文を閉じます。
defer resp.Body.Close()
}
次のコードを使用して、
conf.json
設定ファイルをdemo
フォルダに作成します。[{ "ImportPath":"net/http", "Function":"RoundTrip", "OnEnter":"httpClientEnterHook", "ReceiverType": "*Transport", "OnExit": "httpClientExitHook", "Path": "/path/to/hook" # Path を hook コードのローカルパスに変更します }]
このコードは、Go 用 ARMS エージェントに対し、カスタムコード
hook
をdatabase/sql::(*DB).Query()
関数に挿入するように指示します。demo
ディレクトリに移動し、instgo を使用してプログラムをコンパイルおよび実行し、SQL インジェクションに対する保護を確認します。$ ./instgo set --rule=./conf.json $ ./instgo go build
環境変数を指定して、デモプログラムをローカルで起動します。
環境変数を指定します。
export ARMS_ENABLE=true export ARMS_APP_NAME=xxx # アプリケーション名。 export ARMS_REGION_ID=xxx # Alibaba Cloud アカウントのリージョン ID 。 export ARMS_LICENSE_KEY=xxx # 手順 1 で取得したライセンスキー。
デモプログラムを起動します。
./demo
説明または、コンテナ化環境にプログラムをデプロイします。
ARMS コンソール にログインします。 左側のナビゲーションウィンドウで、 を選択し、アプリケーションのモニタリングデータを表示します。