监控
用于 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 秒 |
APIOnly | bool | 服务是否只暴露监控 API | false |
下一个 | func(*fiber.Ctx) bool | Next 定义一个函数,当返回 true 时跳过此中间件。 | nil |
CustomHead | string | 添加到 Head 部分的自定义 HTML 代码(结束之前) | 空 |
FontURL | string | 用于指定字体资源路径或 URL 的 FontURL | "https://fonts.googleapis.com/css2?family=Roboto:wght@400;900&display=swap" |
ChartJsURL | string | 用于指定 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,
}),
}