Skip to content
Last9
Book demo

fasthttp

Instrument fasthttp Go applications with the Last9 Go Agent for automatic HTTP request tracing and metrics

Use the Last9 Go Agent to instrument your fasthttp application with automatic tracing and metrics. The agent implements the OTel propagation API for fasthttp natively — full OTel compliance with minimal code changes.

Prerequisites

  • Go 1.22 or higher
  • fasthttp (github.com/valyala/fasthttp)
  • Last9 account with OTLP credentials

Installation

  1. Install the Last9 Go Agent

    go get github.com/last9/go-agent
  2. Set Environment Variables

    export OTEL_SERVICE_NAME="your-fasthttp-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"
  3. Instrument your application

    Wrap your fasthttp handler with fasthttpagent.Middleware(). It extracts incoming trace context, creates a server span per request, and stores the context for child spans:

    package main
    import (
    "log"
    agent "github.com/last9/go-agent"
    fasthttpagent "github.com/last9/go-agent/instrumentation/fasthttp"
    "github.com/valyala/fasthttp"
    )
    func main() {
    if err := agent.Start(); err != nil {
    log.Fatalf("failed to start agent: %v", err)
    }
    defer agent.Shutdown()
    handler := func(ctx *fasthttp.RequestCtx) {
    ctx.WriteString("hello")
    }
    fasthttp.ListenAndServe(":8080", fasthttpagent.Middleware(handler))
    }

    Creating child spans inside handlers

    Use ContextFromRequest to get the parent context and create child spans:

    import (
    fasthttpagent "github.com/last9/go-agent/instrumentation/fasthttp"
    "go.opentelemetry.io/otel"
    )
    func myHandler(ctx *fasthttp.RequestCtx) {
    otelCtx := fasthttpagent.ContextFromRequest(ctx)
    _, span := otel.Tracer("my-service").Start(otelCtx, "my-operation")
    defer span.End()
    // your handler logic
    }

Database Instrumentation

Use the agent’s database integration for automatic SQL query tracing. Supported drivers: PostgreSQL, MySQL, SQLite.

import (
"log"
"os"
"github.com/last9/go-agent/integrations/database"
)
var db *sql.DB
func init() {
var err error
db, err = database.Open(database.Config{
DriverName: "postgres",
DSN: os.Getenv("DATABASE_URL"),
DatabaseName: "mydb",
})
if err != nil {
log.Fatal(err)
}
}
func getUser(ctx *fasthttp.RequestCtx) {
otelCtx := fasthttpagent.ContextFromRequest(ctx)
rows, err := db.QueryContext(otelCtx, "SELECT id, name FROM users WHERE id = $1", ctx.UserValue("id"))
// ...
}

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 getFromCache(ctx *fasthttp.RequestCtx) {
otelCtx := fasthttpagent.ContextFromRequest(ctx)
val, err := rdb.Get(otelCtx, "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{})
func callUpstream(ctx *fasthttp.RequestCtx) {
otelCtx := fasthttpagent.ContextFromRequest(ctx)
req, _ := http.NewRequestWithContext(otelCtx, "GET", "https://api.example.com/data", nil)
resp, err := client.Do(req)
// ...
}

What Gets Traced Automatically

SignalWhat’s captured
TracesEvery HTTP request: method, route pattern, status code, latency
TracesDatabase queries: SQL statement, db system, server address/port
TracesRedis commands: command name, key
TracesOutbound HTTP: method, URL, status code
MetricsRuntime: memory, GC pause, goroutine count
MetricsHTTP: request duration, request/response sizes, active connections
MetricsDatabase: 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.