Gorilla Mux
Instrument Gorilla Mux applications with the Last9 Go Agent for automatic HTTP tracing and metrics
Use the Last9 Go Agent to instrument your Gorilla Mux application with automatic tracing and metrics. The agent wraps the official OpenTelemetry Gorilla Mux middleware — full OTel compliance with minimal code changes.
Prerequisites
- Go 1.22 or higher
- Gorilla Mux (
github.com/gorilla/mux) - Last9 account with OTLP credentials
Installation
-
Install the Last9 Go Agent
go get github.com/last9/go-agent -
Set Environment Variables
export OTEL_SERVICE_NAME="your-gorilla-service"export OTEL_EXPORTER_OTLP_ENDPOINT="$last9_otlp_endpoint"export OTEL_EXPORTER_OTLP_HEADERS="Authorization=$last9_otlp_auth_header"export OTEL_TRACES_SAMPLER="always_on"export OTEL_RESOURCE_ATTRIBUTES="deployment.environment=production" -
Instrument your application
Replace
mux.NewRouter()withgorillaagent.NewRouter()— that’s the only change required:package mainimport ("log""net/http""github.com/last9/go-agent"gorillaagent "github.com/last9/go-agent/instrumentation/gorilla")func main() {if err := agent.Start(); err != nil {log.Fatalf("failed to start agent: %v", err)}defer agent.Shutdown()// Drop-in replacement for mux.NewRouter()r := gorillaagent.NewRouter()r.HandleFunc("/", indexHandler).Methods("GET")r.HandleFunc("/users/{id}", getUserHandler).Methods("GET")r.HandleFunc("/users", createUserHandler).Methods("POST")log.Fatal(http.ListenAndServe(":8080", r))}Add tracing middleware to your existing router:
package mainimport ("log""net/http""github.com/gorilla/mux""github.com/last9/go-agent"gorillaagent "github.com/last9/go-agent/instrumentation/gorilla")func main() {if err := agent.Start(); err != nil {log.Fatalf("failed to start agent: %v", err)}defer agent.Shutdown()r := mux.NewRouter()r.Use(gorillaagent.Middleware())r.HandleFunc("/users/{id}", getUserHandler).Methods("GET")log.Fatal(http.ListenAndServe(":8080", r))}
Database Instrumentation
Use the agent’s database integration for automatic SQL query tracing. Supported drivers: PostgreSQL, MySQL, SQLite.
import "github.com/last9/go-agent/integrations/database"
db, err := database.Open(database.Config{ DriverName: "postgres", DSN: "postgres://user:pass@localhost/mydb", DatabaseName: "mydb",})if err != nil { log.Fatal(err)}defer db.Close()
// Use db normally — all queries are automatically tracedfunc getUserHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) rows, err := db.QueryContext(r.Context(), "SELECT id, name FROM users WHERE id = $1", vars["id"]) // ...}The agent automatically extracts server.address, server.port, db.user, and db.name from the DSN and attaches them to every span.
Redis Instrumentation
import redisagent "github.com/last9/go-agent/integrations/redis"
// Drop-in replacement for redis.NewClient()rdb := redisagent.NewClient(&redis.Options{ Addr: "localhost:6379",})
func cacheHandler(w http.ResponseWriter, r *http.Request) { val, err := rdb.Get(r.Context(), "key").Result() // ...}HTTP Client Instrumentation
For outgoing requests with automatic traceparent propagation:
import httpagent "github.com/last9/go-agent/integrations/http"
client := httpagent.NewClient(&http.Client{ Timeout: 10 * time.Second,})
func proxyHandler(w http.ResponseWriter, r *http.Request) { req, _ := http.NewRequestWithContext(r.Context(), "GET", "https://upstream.example.com/api", nil) resp, err := client.Do(req) // ...}What Gets Traced Automatically
| Signal | What’s captured |
|---|---|
| Traces | Every HTTP request: method, route pattern, status code, latency |
| Traces | Database queries: SQL statement, db system, server address/port |
| Traces | Redis commands: command name, key |
| Traces | Outbound HTTP: method, URL, status code |
| Metrics | Runtime: memory, GC pause, goroutine count |
| Metrics | HTTP: request duration, request/response sizes, active connections |
| Metrics | Database: connection pool usage, idle, wait time |
View Traces and Metrics
After running your application, navigate to Trace Explorer and Metrics Explorer in Last9 to view your telemetry data.
Troubleshooting
Please get in touch with us on Discord or Email if you have any questions.