跳到主要内容
版本: v2.x

监控

用于 Fiber 的监控中间件,用于报告服务器指标,灵感来自 express-status-monitor

注意

Monitor 仍处于测试阶段,未来的 API 可能会有所变化!

签名

func New() fiber.Handler

示例

导入 Fiber Web 框架中的中间件包

import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/monitor"
)

在初始化 Fiber 应用程序后,可以使用以下方法

// 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"}))

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

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

配置

属性类型描述默认值
标题string指标页面标题"Fiber Monitor"
刷新time.Duration刷新周期3 秒
APIOnlybool服务是否只暴露监控 APIfalse
下一个func(*fiber.Ctx) boolNext 定义一个函数,当返回 true 时跳过此中间件。nil
CustomHeadstring添加到 Head 部分的自定义 HTML 代码(结束之前)
FontURLstring用于指定字体资源路径或 URL 的 FontURL"https://fonts.googleapis.com/css2?family=Roboto:wght@400;900&display=swap"
ChartJsURLstring用于指定 ChartJS 库路径或 URL 的 ChartJsURL"https://cdn.jsdelivr.net.cn/npm/chart.js@2.9/dist/Chart.bundle.min.js"

默认配置

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,
}),
}