Zero‑Code Observability for Go Apps with Alibaba's Golang Agent
This guide explains how Alibaba's open‑source Golang Agent enables full observability for Go applications without modifying source code, covering download, compilation, endpoint configuration, and examples of trace, metric, and log correlation using Docker and OpenTelemetry.
Background
Traditional monitoring of Golang applications often requires code changes, such as adding OpenTelemetry SDKs, which creates friction between operations and development teams. Alibaba Cloud’s open‑source Golang Agent provides observability without any code modifications, improving efficiency and team satisfaction.
Quick Start
Step 1: Download/Build Golang Agent Binary
Visit the GitHub repository https://github.com/alibaba/opentelemetry-go-auto-instrumentation and download the latest binary, or build it from source:
# Clone the project
git clone https://github.com/alibaba/opentelemetry-go-auto-instrumentation.git
cd opentelemetry-go-auto-instrumentation
make clean && make build
chmod u+x ./otelStep 2: Compile Go Application with Golang Agent
Replace the standard go build command with the agent binary. For example, to build the demo:
cd ./example/demo
../../otel go build .This produces a Go binary that includes built‑in observability.
Step 3: Configure Exporter and Run Application
Trace Export
Start required services (MySQL, Redis, Jaeger) using Docker, then set OpenTelemetry environment variables and launch the binary:
docker run -d -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql:8.0.36
docker run -d -p 6379:6379 redis:latest
docker run --rm --name jaeger -p 16686:16686 jaegertracing/all-in-one:1.53.0
export OTEL_EXPORTER_OTLP_ENDPOINT="http://127.0.0.1:4318"
export OTEL_EXPORTER_OTLP_INSECURE=true
export OTEL_SERVICE_NAME=demo
./demoGenerate traffic with curl localhost:9000/http-service and view traces at http://localhost:16686/jaeger/ui.
Metric Export
Start the metrics example and its dependencies (Otel Collector and Prometheus) via Docker Compose, then configure the exporter:
cd example/metrics
docker compose up --detach
export OTEL_EXPORTER_OTLP_ENDPOINT="http://127.0.0.1:4318"
export OTEL_EXPORTER_OTLP_INSECURE=true
export OTEL_SERVICE_NAME=metrics
./metricsQuery metrics at http://localhost:9090 after invoking curl localhost:9000/gc-metrics and curl localhost:9000/mem-metrics.
Log Correlation
The agent supports automatic log correlation for supported logging frameworks, injecting trace_id and span_id into logs. Example automatic logs:
{"level":"info","msg":"this is info message","trace_id":"d62a8fea...","span_id":"7cb6d692..."}For manual correlation, use the OpenTelemetry SDK to retrieve IDs and add them to logs:
package main
import (
"go.opentelemetry.io/otel/sdk/trace"
"go.uber.org/zap"
"net/http"
)
func main() {
http.HandleFunc("/logwithtrace", func(w http.ResponseWriter, r *http.Request) {
logger := zap.NewExample()
traceId, spanId := trace.GetTraceAndSpanId()
logger.Info("this is info message with fields", zap.String("traceId", traceId), zap.String("spanId", spanId))
})
http.ListenAndServe(":9999", nil)
}Build and run the binary, then request curl localhost:9999/logwithtrace to see logs containing the trace identifiers.
Conclusion and Outlook
The Golang Agent eliminates manual instrumentation for Go applications, has been commercialized on Alibaba Cloud, and is now open‑sourced to the OpenTelemetry community, enabling broader use cases such as service governance, code auditing, security, and debugging.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Observability
Driving continuous progress in observability technology!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
