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
- 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
- Verify the environment:
go version
Demo
Implement a simple dice roller application using the Gin framework.
- Initialize the project:
go mod init demo
- 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!", }) } - Download and update the dependencies:
go mod tidy
- Run the startup command:
go run . & curl http://localhost:8080/rolldice
Using OTelSDK to Report Data over the gRPC Protocol
- Use the OTelSDK for auto tracking.
- 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 } - Download and update the dependencies:
go mod tidy
- Run the demo application:
go run . & curl http://localhost:8080/rolldice

- Log in to the APM console.
- Click
on the left and choose Management & Governance > Application Performance Management. - In the navigation pane, choose Link Trace > Metrics.
- In the tree on the left, locate an environment and then go to the URL tab page to view the URL calling data.
- In the navigation pane, choose Link Trace > Tracing.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot