All Products
Search
Document Center

Application Real-Time Monitoring Service:Use custom Go extensions to create an OpenTelemetry span

Last Updated:Jul 24, 2025

The Application Real-Time Monitoring (ARMS) agent for Go provides custom extensions. You can add custom features without changing the code, facilitating error location based on request parameters and the request body. This topic explains how to use custom extensions of the ARMS agent for Go to create an OpenTelemetry span.

Prerequisites

Procedure

  1. Outside the directory of the current project, create a folder named hook and run the go mod init hook command to initialize it. In the hook folder, create a file named hook.go with the following code, which provides custom extensions:

    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"
    )
    
    // The first argument of the hook function must be api.CallContext followed by parameters matching the target function.
    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()
    }
    // The first argument of the hook function must be api.CallContext followed by parameters matching the target function's return values.
    func httpClientExitHook(call api.CallContext, res *http.Response, err error) {
    	header, _ := json.Marshal(res.Header)
    	fmt.Println("response header is ", string(header))
    }
    

    The code segment responsible for creating the span is:

    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. Develop a test demo.

    In a separate directory from the hook folder, create a demo application folder and run the go mod init demo command to initialize it. In the demo folder, create a file named main.go with the following code:

Note

If the project already relies on OpenTelemetry, you do not need to add _ "go.opentelemetry.io/otel" in the main.go file.

package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
    _ "go.opentelemetry.io/otel"
)

func main() {
	// The request URL.
	req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://www.aliyun.com", nil)
	req.Header.Set("otelbuild", "true")
	client := &http.Client{}
	resp, _ := client.Do(req)

	// Close the response body before the function returns.
	defer resp.Body.Close()
}
  1. Use the following code to create a conf.json configuration file in the demo folder:

    [{
      "ImportPath":"net/http",
      "Function":"RoundTrip",
      "OnEnter":"httpClientEnterHook",
      "ReceiverType": "*Transport",
      "OnExit": "httpClientExitHook",
      "Path": "/path/to/hook" # Modify Path to the local path of the hook code
    }]

    The code instructs the ARMS agent for Go to inject the custom code hook into the database/sql::(*DB).Query() function.

  2. Go to the demo directory and use instgo to compile and run the program to verify the protection against SQL injection.

    $ ./instgo set --rule=./conf.json
    $ ./instgo go build 
  3. Start the demo program locally by specifying environment variables:

    1. Specify environment variables:

      export ARMS_ENABLE=true
      export ARMS_APP_NAME=xxx   # The application name.
      export ARMS_REGION_ID=xxx   # The region ID of your Alibaba Cloud account.
      export ARMS_LICENSE_KEY=xxx   # The license key you retrieved in step 1.
    2. Start the demo program:

      ./demo
    Note

    Alternatively, deploy the program in a containerized environment.

  4. Log on to ARMS console. In the left-side navigation pane, choose Application Monitoring > Application List and view the monitoring data of the application.

Reference

Use the custom extensions of the ARMS agent for Go