共计 3621 个字符,预计需要花费 10 分钟才能阅读完成。
本文介绍 go micro 中退出 metric, 并接入 Prometheus、Grafana
1.go micro 中间件加载 prometheus plugins
go micro 中提供了 prometheus plugins
github.com/micro/go-plugins/wrapper/monitoring/prometheus/
package main
import (
"net/http"
"github.com/micro/go-plugins/wrapper/monitoring/prometheus/v2"
"github.com/prometheus/client_golang/prometheus/promhttp"
//...
)
func main() {
// New Service
service := micro.NewService(micro.Name("go.micro.api.myauth"),
micro.Version("latest"),
micro.WrapHandler(prometheus.NewHandlerWrapper()),
)
// Initialise service
service.Init(
// create wrap for the Myauth service client
micro.WrapHandler(client.MyauthWrapper(service)),
)
go PrometheusBoot()
// Register Handler
myauth.RegisterMyauthHandler(service.Server(), new(handler.Myauth))
// Run service
if err := service.Run(); err != nil {log.Fatal(err)
}
}
func PrometheusBoot() {http.Handle("/metrics", promhttp.Handler())
// 启动 web 服务,监听 8085 端口
go func() {err := http.ListenAndServe("localhost:8085", nil)
if err != nil {log.Fatal("ListenAndServe:", err)
}
}()}
代码很简略,在 micro.NewService 中传入micro.WrapHandler(prometheus.NewHandlerWrapper()),
再启动 web 服务,监听 8085 端口,裸露 metric 数据
拜访 http://localhost:8085/metrics
即可看到数据
2. 启动 prometheus
本文演示 prometheus、grafana 在 docker 中运行
启动 prometheus
- 创立 prometheus.yml,粘贴以下内容
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['host.docker.internal:8085']
这里示例 micro 微服务间接运行在宿主机上,Prometheus 抓取数据须要拜访主机端口,所以 targets 的 ip 写的是 host.docker.internal
- 启动 prometheus
docker run -d -p 9090:9090 -v /Users/wulin/gowork/myauth/prometheus.yml:/etc/prometheus/prometheus.yml --name prometheus prom/prometheus
拜访地址 http://localhost:9090/
能够看到抓取状态是失常的
2. 启动 grafana
docker run -d -p 3000:3000 grafana/grafana
拜访地址 http://localhost:3000/
首先设置数据源,抉择 prometheus,url 填 http:// 宿主 ip:9090/,点击保留,提醒胜利即可
dashboard 中点击 `add panel`, 抉择数据源,填写 metrics(点击有提醒),实现后点 `apply` 即可
测试用的 node-exporter,能够用于测试
docker run -d -p 9100:9100 \
-v "/proc:/host/proc:ro" \
-v "/sys:/host/sys:ro" \
-v "/:/rootfs:ro" \
prom/node-exporter
以上内容探讨的是 go micro
的 metrics,
以下内容探讨的是 micro 网关
中的 metrics,官网 plugins 请见 https://github.com/micro/go-p…
自定义 micro 网关
代码如下,仅列出 metrics 相干代码
import (
"github.com/micro/go-plugins/micro/metrics/v2"
"github.com/micro/micro/v2/cmd"
"github.com/micro/micro/v2/plugin"
)
func main() {plugin.Register(metrics.NewPlugin())
cmd.Init()}
插件中代码如下
// NewPlugin returns a new metrics plugin
func NewPlugin() plugin.Plugin {metrics := new(Metrics)
return plugin.NewPlugin(plugin.WithName("metrics"),
plugin.WithFlag(
&cli.StringFlag{
Name: "metrics",
Usage: "Specify the type of metrics provider e.g prometheus",
},
),
plugin.WithHandler(metrics.Handler),
plugin.WithInit(func(ctx *cli.Context) error {provider := ctx.String("metrics")
switch provider {
case "prometheus":
metrics.Provider = prometheus.New()
log.Info("Loaded prometheus metrics at /metrics")
}
return nil
}),
)
}
看起来不是很欠缺,目前只反对 prometheus,且参数不能在代码指定 metrics,启动如下
go run ./gateway/main.go -metrics prometheus api --handler=api
plugins 参数指定须要放到 api 之前哦
启动后能够看见控制台
file=v2@v2.9.1/metrics.go:49 level=info Loaded prometheus metrics at /metrics
拜访 http://localhost:8080/metrics,即可看到 metrics 信息
数据收集展示和上文一样,这里就不反复介绍了。
go micro 剖析系列文章
go micro server 启动剖析
go micro client
go micro broker
go micro cmd
go micro config
go micro store
go micro registry
go micro router
go micro runtime
go micro transport
go micro web
go micro registry 插件 consul
go micro plugin
go micro jwt 网关鉴权
go micro 链路追踪
go micro 熔断与限流
go micro wrapper 中间件
go micro metrics 接入 Prometheus、Grafana