Tracing - In-Process Service (HTTP Exporter)
Code Source: https://github.com/gogf/examples/tree/main/observability/trace/inprocess
Description
This example demonstrates how to implement distributed tracing in in-process services using GoFrame with HTTP-based OpenTelemetry exporter. It shows how to:
- Configure tracing in a single process using HTTP exporter
- Trace function calls and operations
- Propagate trace context
- Visualize distributed traces
Requirements
- Go 1.22 or higher
- Git
- GoFrame
- GoFrame OpenTelemetry HTTP Tracing
Structure
.
├── main.go # Main application with HTTP-based tracing
├── go.mod # Go module file
└── go.sum # Go module checksums
Features
The example showcases the following features:
-
Distributed Tracing with HTTP Exporter
- HTTP-based trace data transmission
- Span management
- Trace visualization
-
Function Call Tracing
- Function entry/exit tracing
- Context propagation
- Error handling
-
Data Operations
- User data retrieval
- Data aggregation
- Error handling
Prerequisites
- Running Jaeger instance:
docker run --rm --name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 4317:4317 \
-p 4318:4318 \
-p 14250:14250 \
-p 14268:14268 \
-p 14269:14269 \
-p 9411:9411 \
jaegertracing/all-in-one:1.55
Usage
-
Run the application:
go run main.go
-
View traces: Open http://localhost:16686 in your browser to view traces in Jaeger UI.
Implementation Details
The example demonstrates:
-
User Data Operations
GetUser(ctx, id) // Retrieves complete user data
├── GetInfo(ctx, id) // Gets basic user information
├── GetDetail(ctx, id) // Gets detailed user information
└── GetScores(ctx, id) // Gets user scores -
Trace Context Flow
- Main function creates root span
- Each function creates child span
- Context propagates through function calls
- All spans are properly ended
-
Error Handling
- Non-existent user IDs return nil
- Each function handles errors independently
- Errors are traced and logged
-
HTTP-based Trace Export
- Uses HTTP protocol for trace data transmission
- Configures HTTP trace collector endpoint
- Handles HTTP connection management
Troubleshooting
-
Application Issues:
- Check if application is running correctly
- Verify function call outputs
- Review error logs
-
Tracing Issues:
- Verify Jaeger is running:
docker ps | grep jaeger
- Check Jaeger UI accessibility: http://localhost:16686
- Ensure HTTP endpoint is correct in configuration
- Verify Jaeger is running:
-
HTTP Export Issues:
- Check HTTP connection status
- Verify HTTP endpoint accessibility
- Review HTTP error messages
Example Output
For user ID 100:
{
"id": 100,
"name": "john",
"gender": 1,
"site": "https://goframe.org",
"email": "john@goframe.org",
"math": 100,
"english": 60,
"chinese": 50
}
For non-existent user IDs:
{}