Middleware para o Fiber v2 que captura automaticamente todas as requisições HTTP e envia eventos de auditoria imutáveis para o ImmutableLog.
Usa c.Next() para capturar status e erros, c.Locals() para eventos customizados e goroutine fire-and-forget para zero overhead. Inclui tratamento correto do buffer recycling do fasthttp.
go get github.com/gofiber/fiber/v2internal/middleware/immutablelog.go
| Variável | Obrigatório | Default | Descrição |
|---|---|---|---|
IMTBL_API_KEY |
Sim | — | Chave de API do ImmutableLog |
IMTBL_SERVICE_NAME |
Não | "" |
Nome do serviço exibido nos eventos |
IMTBL_ENV |
Não | "" |
Ambiente de execução |
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/yourorg/yourapp/internal/middleware"
)
func main() {
app := fiber.New()
app.Use(middleware.New(middleware.Config{
APIKey: "iml_live_xxxx",
ServiceName: "my-api",
Env: "production",
SkipPaths: []string{"/health", "/metrics"},
}))
app.Get("/health", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{"ok": true})
})
app.Post("/payments", paymentsHandler)
app.Listen(":8080")
}| Etapa | Descrição |
|---|---|
copy(bodyBytes, c.Body()) |
Copia o body ANTES de c.Next() — Fiber recicla buffers após a request |
c.Next() + StatusCode() |
Executa handlers — status lido de c.Response().StatusCode() depois |
| Captura de erro por retorno | err := c.Next() — erros Go são retornados, não lançados |
go emit() |
Goroutine fire-and-forget — zero latência adicionada |
func paymentsHandler(c *fiber.Ctx) error {
c.Locals("imtbl.eventName", "payment.created")
// ... lógica de negócio ...
return c.Status(201).JSON(fiber.Map{"ok": true})
}| Situação | Resultado |
|---|---|
Path em SkipPaths |
c.Next() chamado diretamente, sem audit |
| Status 2xx | type: success |
| Status 3xx | type: info |
| Status 4xx / 5xx | type: error |
error retornado |
type: error com error_message no payload |
| Falha no POST | Silenciada na goroutine — nunca quebra a aplicação |