すべてのプロダクト
Search
ドキュメントセンター

Application Real-Time Monitoring Service:カスタム Go 拡張機能を使用して OpenTelemetry スパンを作成する

最終更新日:Jul 03, 2025

Go 用 Application Real-Time Monitoring (ARMS) エージェントは、カスタム拡張機能を提供します。コードを変更せずにカスタム機能を追加できるため、リクエストパラメータとリクエスト本文に基づいてエラーの場所を特定しやすくなります。このトピックでは、Go 用 ARMS エージェントのカスタム拡張機能を使用して OpenTelemetry スパンを作成する方法について説明します。

前提条件

手順

  1. 現在のプロジェクトのディレクトリの外側に、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()
  2. テストデモを開発します。

    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()
}
  1. 次のコードを使用して、conf.json 設定ファイルを demo フォルダに作成します。

    [{
      "ImportPath":"net/http",
      "Function":"RoundTrip",
      "OnEnter":"httpClientEnterHook",
      "ReceiverType": "*Transport",
      "OnExit": "httpClientExitHook",
      "Path": "/path/to/hook" # Path を hook コードのローカルパスに変更します
    }]

    このコードは、Go 用 ARMS エージェントに対し、カスタムコード hookdatabase/sql::(*DB).Query() 関数に挿入するように指示します。

  2. demo ディレクトリに移動し、instgo を使用してプログラムをコンパイルおよび実行し、SQL インジェクションに対する保護を確認します。

    $ ./instgo set --rule=./conf.json
    $ ./instgo go build 
  3. 環境変数を指定して、デモプログラムをローカルで起動します。

    1. 環境変数を指定します。

      export ARMS_ENABLE=true
      export ARMS_APP_NAME=xxx   # アプリケーション名。
      export ARMS_REGION_ID=xxx   # Alibaba Cloud アカウントのリージョン ID 。
      export ARMS_LICENSE_KEY=xxx   # 手順 1 で取得したライセンスキー。
    2. デモプログラムを起動します。

      ./demo
  4. ARMS コンソール にログインします。 左側のナビゲーションウィンドウで、[アプリケーションモニタリング] > [アプリケーションリスト] を選択し、アプリケーションのモニタリングデータを表示します。

参考資料

Go 用 ARMS エージェントのカスタム拡張機能を使用する