Middleware Tracing Example
This example demonstrates the comprehensive middleware tracking capabilities of GoVisual.
Features Demonstrated
- Middleware Chain Tracking: See how requests flow through multiple middleware layers
- SQL Query Tracking: Monitor database queries with timing and results
- HTTP Call Tracking: Track external API calls
- Custom Trace Points: Add custom trace entries for specific operations
- Performance Metrics: View detailed performance data for each request
Running the Example
go run main.go
The server will start on http://localhost:8090
Dashboard
Open http://localhost:8090/__viz to view the dashboard.
Test Endpoints
-
Basic Request: http://localhost:8090/
- Simple JSON response
- Shows basic middleware execution
-
Database Query: http://localhost:8090/api/users
- Executes SQL queries
- Shows database interaction in traces
-
Slow Operation: http://localhost:8090/api/slow
- Multi-step operation with timing
- Shows nested trace entries
-
External API: http://localhost:8090/api/external
- Simulates external HTTP calls
- Shows HTTP tracking in traces
Viewing Traces
- Make some requests to the test endpoints
- Open the dashboard at http://localhost:8090/__viz
- Go to the "Trace" tab
- Select a request to see its detailed execution trace
- Middleware Stack: See each middleware that processed the request
- Execution Timeline: Visual timeline of all operations
- SQL Queries: Full query text, duration, and row counts
- HTTP Calls: External API calls with status and timing
- Custom Events: Application-specific trace points
- Performance Metrics: CPU usage, memory allocation, and bottlenecks
Customizing Traces
You can add custom trace points in your handlers:
tracer := middleware.GetTracer(r.Context())
if tracer != nil {
tracer.StartTrace("Operation Name", "custom", map[string]interface{}{
"custom_field": "value",
})
// ... your operation ...
tracer.EndTrace(nil)
}
SQL Query Tracking
SQL queries are automatically tracked when using the profiling-enabled database drivers. The traces show:
- Query text
- Execution duration
- Number of rows affected/returned
- Any errors encountered