DEV Community

Sumeet Patidar
Sumeet Patidar

Posted on

Debug Fiber APIs Like a Pro with GoRequestTracer: JSON Logging, DB Queries, and Panic Tracking

If you’re building APIs or microservices in Go using Fiber, you know how hard it is to track HTTP requests, database queries, and unexpected panics.

Meet GoRequestTracer, a lightweight Fiber middleware that:

  • Logs HTTP requests in JSON
  • Tracks DB queries
  • Monitors HTTP client calls
  • Captures panic stack traces
  • Generates a unique request ID for every request All this without changing your existing code too much.

Why You Need This

Imagine a production API receiving hundreds of requests per second.

  • Which request failed?
  • What DB query caused the delay?
  • Did an unexpected panic happen?

GoRequestTracer solves all this by centralizing request and DB tracking in a structured JSON log, making debugging faster and simpler.

Installation

# Clone the repo
git clone https://github.com/Patidar-Ops/GoRequestTracer.git

# Enter directory
cd GoRequestTracer

# Download dependencies
go mod tidy
Enter fullscreen mode Exit fullscreen mode

If using SQLite, ensure CGO is enabled.

Usage Example

package main

import (
    "database/sql"
    "log"

    "github.com/gofiber/fiber/v2"
    _ "github.com/mattn/go-sqlite3"

    "github.com/Patidar-Ops/GoRequestTracer/tracer"
)

func main() {
    app := fiber.New()

    db, err := sql.Open("sqlite3", ":memory:")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    app.Use(tracer.New(db)) // Fiber middleware

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("Hello, GoRequestTracer!")
    })

    app.Listen(":3000")
}
Enter fullscreen mode Exit fullscreen mode

Example Log Output

HTTP request log:

{
  "request_id": "1165699a-a262-40e9-a903-997c5bb783c1",
  "method": "GET",
  "path": "/",
  "status": 200,
  "total_ms": 5
}
Enter fullscreen mode Exit fullscreen mode

Panic log:

{
  "request_id": "b6099617-802c-4b3f-a128-3040a22862e0",
  "method": "GET",
  "path": "/panic",
  "status": 500,
  "total_ms": 0,
  "panic_stack": "runtime error: invalid memory address or nil pointer dereference\n..."
}

Enter fullscreen mode Exit fullscreen mode

Key Features

  • ✅ **Unique Request ID **per request
  • JSON structured logging
  • DB query tracking (DBTracer)
  • HTTP client call logging
  • Panic recovery with stack trace
  • ✅ Works with any database/sql driver

Contributing

GoRequestTracer is open to contributions! Fork, create a branch, and submit a Pull Request.

Check the repo here: GoRequestTracer on GitHub

Why Star This Repo?

If you’re building Go APIs with Fiber or just want to monitor DB queries and panics in production efficiently, this repo will save you hours of debugging.

🌟 Star the repo to support it and stay updated!

Top comments (0)