You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`monitor` is built on `net/http`. Fiber is based on `fasthttp`, so the safest integration today is to create one monitor instance during startup and expose only the monitor endpoint through Fiber's official adaptor.
78
+
`monitor` is built on `net/http`. Fiber is based on `fasthttp`, so create one monitor instance during startup, expose only the monitor endpoint through Fiber's official adaptor, and record business requests with Fiber-native middleware.
79
79
80
80
> Important: do not call `monitor.New` or `monitor.NewMonitor` inside `adaptor.HTTPMiddleware`.
81
81
> Fiber executes that middleware factory for every request, while each monitor instance starts one background collector goroutine. Creating a monitor instance per request will leak collector goroutines.
@@ -85,9 +85,10 @@ package main
85
85
86
86
import (
87
87
"net/http"
88
+
"time"
88
89
89
-
"github.com/gofiber/fiber/v2"
90
-
"github.com/gofiber/fiber/v2/middleware/adaptor"
90
+
"github.com/gofiber/fiber/v3"
91
+
"github.com/gofiber/fiber/v3/middleware/adaptor"
91
92
"github.com/gofurry/monitor"
92
93
)
93
94
@@ -99,9 +100,29 @@ func main() {
99
100
})
100
101
defer m.Stop()
101
102
103
+
app.Use(func(c fiber.Ctx) error {
104
+
if c.Path() == "/monitor" {
105
+
return c.Next()
106
+
}
107
+
108
+
started:= time.Now()
109
+
m.RequestStarted()
110
+
err:= c.Next()
111
+
112
+
status:= c.Response().StatusCode()
113
+
if err != nil {
114
+
status = fiber.StatusInternalServerError
115
+
iffiberErr, ok:= err.(*fiber.Error); ok {
116
+
status = fiberErr.Code
117
+
}
118
+
}
119
+
m.RequestFinished(status, time.Since(started))
120
+
return err
121
+
})
122
+
102
123
app.All("/monitor", adaptor.HTTPHandler(m))
103
124
104
-
app.Get("/", func(c *fiber.Ctx) error {
125
+
app.Get("/", func(c fiber.Ctx) error {
105
126
return c.SendString("hello")
106
127
})
107
128
@@ -111,7 +132,114 @@ func main() {
111
132
112
133
Open `http://localhost:8080/monitor`.
113
134
114
-
This Fiber example safely serves the monitor page and JSON snapshot, but it does not wrap all Fiber routes. Therefore `http.total_requests` only reflects requests handled by this monitor handler. If you need full Fiber business request accounting, use a native Fiber adapter instead of wrapping `monitor.New` with `adaptor.HTTPMiddleware`.
135
+
This Fiber example safely serves the monitor page and JSON snapshot, while `http.total_requests`, in-flight requests, status code classes, and latency are recorded from the native Fiber middleware.
136
+
137
+
## Gin
138
+
139
+
Gin runs on `net/http`, but you can still use the framework-neutral request lifecycle methods when you want monitor to stay outside Gin's handler chain.
0 commit comments