|
| 1 | +--- |
| 2 | +id: monitor |
| 3 | +--- |
| 4 | + |
| 5 | +# Monitor |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +Monitor middleware for [Fiber](https://github.com/gofiber/fiber) that reports server metrics, inspired by [express-status-monitor](https://github.com/RafalWilinski/express-status-monitor) |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## Install |
| 18 | + |
| 19 | +This middleware supports Fiber v3. |
| 20 | + |
| 21 | +``` |
| 22 | +go get -u github.com/gofiber/fiber/v3 |
| 23 | +go get -u github.com/gofiber/contrib/monitor |
| 24 | +``` |
| 25 | + |
| 26 | +### Signature |
| 27 | + |
| 28 | +```go |
| 29 | +monitor.New(config ...monitor.Config) fiber.Handler |
| 30 | +``` |
| 31 | + |
| 32 | +### Config |
| 33 | + |
| 34 | +| Property | Type | Description | Default | |
| 35 | +| :--------- | :------------------------ | :----------------------------------------------------------------------------------- | :-------------------------------------------------------------------------- | |
| 36 | +| Title | `string` | Metrics page title. | `Fiber Monitor` | |
| 37 | +| Refresh | `time.Duration` | Refresh period. | `3 seconds` | |
| 38 | +| APIOnly | `bool` | Whether the service should expose only the montioring API. | `false` | |
| 39 | +| Next | `func(c *fiber.Ctx) bool` | Define a function to add custom fields. | `nil` | |
| 40 | +| CustomHead | `string` | Custom HTML code to Head Section(Before End). | `empty` | |
| 41 | +| FontURL | `string` | FontURL for specilt font resource path or URL. also you can use relative path. | `https://fonts.googleapis.com/css2?family=Roboto:wght@400;900&display=swap` | |
| 42 | +| ChartJsURL | `string` | ChartJsURL for specilt chartjs library, path or URL, also you can use relative path. | `https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.bundle.min.js` | |
| 43 | + |
| 44 | +### Example |
| 45 | + |
| 46 | +```go |
| 47 | +package main |
| 48 | + |
| 49 | +import ( |
| 50 | + "log" |
| 51 | + |
| 52 | + "github.com/gofiber/fiber/v3" |
| 53 | + "github.com/gofiber/contrib/monitor" |
| 54 | +) |
| 55 | + |
| 56 | +func main() { |
| 57 | + app := fiber.New() |
| 58 | + |
| 59 | + // Initialize default config (Assign the middleware to /metrics) |
| 60 | + app.Get("/metrics", monitor.New()) |
| 61 | + |
| 62 | + // Or extend your config for customization |
| 63 | + // Assign the middleware to /metrics |
| 64 | + // and change the Title to `MyService Metrics Page` |
| 65 | + app.Get("/metrics", monitor.New(monitor.Config{Title: "MyService Metrics Page"})) |
| 66 | + |
| 67 | + log.Fatal(app.Listen(":3000")) |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | + |
| 72 | +## Default Config |
| 73 | + |
| 74 | +```go |
| 75 | +var ConfigDefault = Config{ |
| 76 | + Title: defaultTitle, |
| 77 | + Refresh: defaultRefresh, |
| 78 | + FontURL: defaultFontURL, |
| 79 | + ChartJsURL: defaultChartJSURL, |
| 80 | + CustomHead: defaultCustomHead, |
| 81 | + APIOnly: false, |
| 82 | + Next: nil, |
| 83 | +} |
| 84 | +``` |
0 commit comments