diff --git a/middleware/monitor/README.md b/middleware/monitor/README.md index f209cf191f..671e3c7352 100644 --- a/middleware/monitor/README.md +++ b/middleware/monitor/README.md @@ -24,23 +24,35 @@ import ( func main() { app := fiber.New() - - app.Get("/dashboard", monitor.New()) - + + app.Get("/metrics", monitor.New(monitor.Config{Title: "MyService Metrics Page"})) + log.Fatal(app.Listen(":3000")) } ``` +You can also access the API endpoint with +`curl -X GET -H "Accept: application/json" http://localhost:3000/metrics` which returns: +```json +{"pid":{ "cpu":0.4568381746582226, "ram":20516864, "conns":3 }, + "os": { "cpu":8.759124087593099, "ram":3997155328, "conns":44, + "total_ram":8245489664, "load_avg":0.51 }} +``` ## Config ```go // Config defines the config for middleware. type Config struct { + // Metrics page title + // + // Optional. Default: "Fiber Monitor" + Title string + // To disable serving HTML, you can make true this option. // // Optional. Default: false APIOnly bool - + // Next defines a function to skip this middleware when returned true. // // Optional. Default: nil @@ -52,7 +64,8 @@ type Config struct { ```go var ConfigDefault = Config{ + Title: "Fiber Monitor", APIOnly: false, - Next: nil, + Next: nil, } ``` diff --git a/middleware/monitor/config.go b/middleware/monitor/config.go index 955323ef8a..b98f73d4e7 100644 --- a/middleware/monitor/config.go +++ b/middleware/monitor/config.go @@ -1,9 +1,18 @@ package monitor -import "github.com/gofiber/fiber/v2" +import ( + "strings" + + "github.com/gofiber/fiber/v2" +) // Config defines the config for middleware. type Config struct { + // Metrics page title + // + // Optional. Default: "Fiber Monitor" + Title string + // Whether the service should expose only the monitoring API. // // Optional. Default: false @@ -13,14 +22,36 @@ type Config struct { // // Optional. Default: nil Next func(c *fiber.Ctx) bool + + // defaultTitle substituted with Title in indexHtml + index string } +var prevTitle = defaultTitle + var ConfigDefault = Config{ + Title: defaultTitle, APIOnly: false, Next: nil, + index: indexHtml, +} + +func newIndex(title string) string { + if title == defaultTitle { + return indexHtml + } + return strings.ReplaceAll(indexHtml, defaultTitle, title) } func configDefault(config ...Config) Config { + // Users can change ConfigDefault.Title which then + // becomes incompatible with ConfigDefault.index + if prevTitle != ConfigDefault.Title { + prevTitle = ConfigDefault.Title + // update default index with new default title + ConfigDefault.index = newIndex(prevTitle) + } + // Return default config if nothing provided if len(config) < 1 { return ConfigDefault @@ -30,6 +61,14 @@ func configDefault(config ...Config) Config { cfg := config[0] // Set default values + if cfg.Title == "" || cfg.Title == ConfigDefault.Title { + cfg.Title = ConfigDefault.Title + cfg.index = ConfigDefault.index + } else { + // update cfg.index with new title + cfg.index = newIndex(cfg.Title) + } + if cfg.Next == nil { cfg.Next = ConfigDefault.Next } diff --git a/middleware/monitor/index.go b/middleware/monitor/index.go index 92341dddb1..6313685290 100644 --- a/middleware/monitor/index.go +++ b/middleware/monitor/index.go @@ -1,290 +1,255 @@ package monitor -var index = []byte(` - - - - - - - - Fiber Monitor - - - - -
-
-

Fiber Monitor

-
- -
-
-
-
CPU Usage
-

0.00%

-
-
- -
-
- -
-
-
Memory Usage
-

0.00 MB

-
-
- -
-
- -
-
-
Response Time
-

0ms

-
-
- -
-
- -
-
-
Open Connections
-

0

-
-
- -
-
-
-
- - - - -`) + indexHtml = ` + + + + + + + ` + defaultTitle + ` + + + +
+

` + defaultTitle + `

+
+
+
+
CPU Usage
+

0.00%

+
+
+ +
+
+
+
+
Memory Usage
+

0.00 MB

+
+
+ +
+
+
+
+
Response Time
+

0ms

+
+
+ +
+
+
+
+
Open Connections
+

0

+
+
+ +
+
+
+
+ + +` +) diff --git a/middleware/monitor/index.html b/middleware/monitor/index.html deleted file mode 100644 index 91fc79e0d1..0000000000 --- a/middleware/monitor/index.html +++ /dev/null @@ -1,304 +0,0 @@ - - - - - - - - - Fiber Monitor - - - - -
-
-

Fiber Monitor

-
- -
-
-
-
CPU Usage
-

0.00%

-
-
- -
-
- -
-
-
Memory Usage
-

0.00 MB

-
-
- -
-
- -
-
-
One Minute Load Avg
-

0.00

-
-
- -
-
- -
-
-
Response Time
-

0ms

-
-
- -
-
- -
-
-
Open Connections
-

0

-
-
- -
-
-
-
- - - - - \ No newline at end of file diff --git a/middleware/monitor/monitor.go b/middleware/monitor/monitor.go index 6decf3c24e..cd1f3705d1 100644 --- a/middleware/monitor/monitor.go +++ b/middleware/monitor/monitor.go @@ -95,8 +95,8 @@ func New(config ...Config) fiber.Handler { mutex.Unlock() return c.Status(fiber.StatusOK).JSON(data) } - c.Response().Header.SetContentType(fiber.MIMETextHTMLCharsetUTF8) - return c.Status(fiber.StatusOK).Send(index) + c.Set(fiber.HeaderContentType, fiber.MIMETextHTMLCharsetUTF8) + return c.Status(fiber.StatusOK).SendString(cfg.index) } } diff --git a/middleware/monitor/monitor_test.go b/middleware/monitor/monitor_test.go index c59e302355..0573fe879c 100644 --- a/middleware/monitor/monitor_test.go +++ b/middleware/monitor/monitor_test.go @@ -37,7 +37,7 @@ func Test_Monitor_Html(t *testing.T) { b, err := ioutil.ReadAll(resp.Body) utils.AssertEqual(t, nil, err) - utils.AssertEqual(t, true, bytes.Contains(b, []byte("Fiber Monitor"))) + utils.AssertEqual(t, true, bytes.Contains(b, []byte(""+defaultTitle+""))) } // go test -run Test_Monitor_JSON -race