Updated on 2026-01-30 GMT+08:00

Reporting Go Application Data Using OpenTelemetry

Huawei Cloud APM is compatible with OpenTelemetry and can directly receive the trace data reported using the OpenTelemetry SDK or Agent. This section describes how to use OpenTelemetry to connect a Go application to APM and report trace data.

Constraint

Go 1.23 or later must be used.

Environment Preparation

  1. Install Go:
    wget  https://mirrors.aliyun.com/golang/go1.23.1.linux-amd64.tar.gz
    tar -C /usr/local -xzf go1.23.1.linux-amd64.tar.gz
    echo 'export PATH=$PATH:/usr/local/go/bin' >> /etc/profile
    source /etc/profile
  1. Verify the environment:
    go version

Demo

Implement a simple dice roller application using the Gin framework.

  1. Initialize the project:
    go mod init demo
  2. Compile the service code:
    package main
    
    import (
        "log"
        "math/rand"
        "github.com/gin-gonic/gin"
    )
    
    func main() {
        // Routes
        r := gin.Default()
        r.GET("/rolldice", rolldice)
    
        // Run the server
        log.Println("Server starting on :8080...")
        r.Run(":8080")
    }
    
    func rolldice(c *gin.Context) {
        c.JSON(200, gin.H{
            "roll":    rand.Intn(6) + 1,
            "message": "Dice rolled!",
        })
    }
  3. Download and update the dependencies:
    go mod tidy
  4. Run the startup command:
    go run . &
    curl http://localhost:8080/rolldice

Using OTelSDK to Report Data over the gRPC Protocol

  1. Use the OTelSDK for auto tracking.
  2. Add dependencies and initialize the auto tracking SDK:

    package main
    
    import (
        "log"
        "math/rand"
        "github.com/gin-gonic/gin"
        "context"
        "go.opentelemetry.io/otel"
        "go.opentelemetry.io/otel/attribute"
        "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
        "go.opentelemetry.io/otel/sdk/resource"
        sdktrace "go.opentelemetry.io/otel/sdk/trace"
        "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
    )
    
    var (
        SERVICE_NAME = "Application. Component. Environment"
        APM_ENDPOINT = "Reporting address"
        APM_TOKEN = "Authentication information"
    )
    
    func main() {
        cleanup := initTracer()
        defer cleanup(context.Background())
    
        // Routes
        r := gin.Default()
    
        //automatic instrumentation
        r.Use(otelgin.Middleware(SERVICE_NAME))
    
        r.GET("/rolldice", rolldice)
    
        // Run the server
        log.Println("Server starting on :8080...")
        r.Run(":8080")
    }
    
    func rolldice(c *gin.Context) {
        c.JSON(200, gin.H{
            "roll":    rand.Intn(6) + 1,
            "message": "Dice rolled!",
        })
    }
    
    func initTracer() func(context.Context) error {
        headers := map[string]string{"Authentication": APM_TOKEN}
        exporter, err := otlptracegrpc.New(
            context.Background(),
            otlptracegrpc.WithInsecure(),
            otlptracegrpc.WithEndpoint(APM_ENDPOINT),
            otlptracegrpc.WithHeaders(headers),
        )
    
        if err != nil {
            log.Fatalf("Failed to create exporter: %v", err)
        }
    
        resources, err := resource.New(
            context.Background(),
            resource.WithAttributes(
                attribute.String("service.name", SERVICE_NAME),
                attribute.String("library.language", "go"),
            ),
        )
    
        if err != nil {
            log.Fatalf("Could not set resources: %v", err)
        }
    
        otel.SetTracerProvider(
            sdktrace.NewTracerProvider(
                sdktrace.WithSampler(sdktrace.AlwaysSample()),
                sdktrace.WithBatcher(exporter),
                sdktrace.WithResource(resources),
            ),
        )
        return exporter.Shutdown
    }

  3. Download and update the dependencies:

    go mod tidy

  4. Run the demo application:

    go run . &
    curl http://localhost:8080/rolldice

  5. Log in to the APM console.
  6. Click on the left and choose Management & Governance > Application Performance Management.
  7. In the navigation pane, choose Link Trace > Metrics.
  8. In the tree on the left, locate an environment and then go to the URL tab page to view the URL calling data.
  9. In the navigation pane, choose Link Trace > Tracing.