Monitor

原文: https://docs.gofiber.io/api/middleware/monitor

Monitor 监视器

Monitor middleware for Fiber that reports server metrics, inspired by express-status-monitor

​ 受 express-status-monitor 启发的 Fiber 服务器指标报告监视中间件

CAUTION 注意

Monitor is still in beta, API might change in the future!

​ Monitor 仍处于测试阶段,API 可能会在未来发生变化!

img

Signatures 签名

1
func New() fiber.Handler

Examples 示例

Import the middleware package that is part of the Fiber web framework

​ 导入 Fiber Web 框架的一部分中间件包

1
2
3
4
import (
  "github.com/gofiber/fiber/v2"
  "github.com/gofiber/fiber/v2/middleware/monitor"
)

After you initiate your Fiber app, you can use the following possibilities:

​ 在启动 Fiber 应用后,您可以使用以下可能性:

1
2
3
4
5
6
7
// Initialize default config (Assign the middleware to /metrics)
app.Get("/metrics", monitor.New())

// Or extend your config for customization
// Assign the middleware to /metrics
// and change the Title to `MyService Metrics Page`
app.Get("/metrics", monitor.New(monitor.Config{Title: "MyService Metrics Page"}))

You can also access the API endpoint with curl -X GET -H "Accept: application/json" http://localhost:3000/metrics which returns:

​ 您还可以使用 curl -X GET -H "Accept: application/json" http://localhost:3000/metrics 访问 API 端点,它返回:

1
2
3
{"pid":{ "cpu":0.4568381746582226, "ram":20516864,   "conns":3 },
 "os": { "cpu":8.759124087593099,  "ram":3997155328, "conns":44,
    "total_ram":8245489664, "load_avg":0.51 }}

Config 配置

Property 属性Type 输入Description 说明Default 默认
Title 标题stringMetrics page title 指标页面标题“Fiber Monitor” “Fiber Monitor”
Refresh 刷新time.DurationRefresh period 刷新周期3 seconds 3 秒
APIOnlyboolWhether the service should expose only the monitoring API 服务是否应该仅公开监控 APIfalse
Next 下一步func(*fiber.Ctx) boolNext defines a function to skip this middleware when returned true. 接下来定义一个函数,在返回 true 时跳过此中间件。nil
CustomHead 自定义头部stringCustom HTML Code to Head Section(Before End) 自定义 HTML 代码到头部部分(结束前)empty 空
FontURL 字体 URLstringFontURL for specify font resource path or URL 字体 URL 用于指定字体资源路径或 URLhttps://fonts.googleapis.com/css2?family=Roboto:wght@400;900&display=swap" “https://fonts.googleapis.com/css2?family=Roboto:wght@400;900&display=swap”
ChartJsURLstringChartJsURL for specify ChartJS library path or URL ChartJsURL 用于指定 ChartJS 库路径或 URLhttps://cdn.jsdelivr.net/npm/chart.js@2.9/dist/Chart.bundle.min.js"

Default Config 默认配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
var ConfigDefault = Config{
    Title:      defaultTitle,
    Refresh:    defaultRefresh,
    FontURL:    defaultFontURL,
    ChartJsURL: defaultChartJSURL,
    CustomHead: defaultCustomHead,
    APIOnly:    false,
    Next:       nil,
    index: newIndex(viewBag{
        defaultTitle,
        defaultRefresh,
        defaultFontURL,
        defaultChartJSURL,
        defaultCustomHead,
    }),
}
最后修改 February 5, 2024: 更新 (f57b279)