W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
Metrics 中間件用于實現(xiàn)服務(wù)的性能指標監(jiān)控,統(tǒng)計了請求耗時和請求計數(shù)。
Metrics 中間件中提供了兩個配置方法 ?WithSeconds()
? 和 ?WithRequests()
?。
WithSeconds()
?func WithSeconds(c metrics.Observer) Option {
return func(o *options) {
o.seconds = c
}
}
用于設(shè)置 metrics 中間件統(tǒng)計請求耗時的 ?Observer
?直方圖。
WithRequests()
?func WithRequests(c metrics.Counter) Option {
return func(o *options) {
o.requests = c
}
}
用于設(shè)置 metrics 中間件統(tǒng)計請求計數(shù)的 ?Counter
?計數(shù)器。
// https://github.com/go-kratos/examples/tree/main/metrics
_metricSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "server",
Subsystem: "requests",
Name: "duration_ms",
Help: "server requests duration(ms).",
Buckets: []float64{5, 10, 25, 50, 100, 250, 500, 1000},
}, []string{"kind", "operation"})
_metricRequests = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "client",
Subsystem: "requests",
Name: "code_total",
Help: "The total number of processed requests",
}, []string{"kind", "operation", "code", "reason"})
prometheus.MustRegister(_metricSeconds, _metricRequests)
httpSrv.Handle("/metrics", promhttp.Handler())
// grpc sever
grpcSrv := grpc.NewServer(
grpc.Address(":9000"),
grpc.Middleware(
metrics.Server(
metrics.WithSeconds(prom.NewHistogram(_metricSeconds)),
metrics.WithRequests(prom.NewCounter(_metricRequests)),
),
),
)
// http server
httpSrv := http.NewServer(
http.Address(":8000"),
http.Middleware(
metrics.Server(
metrics.WithSeconds(prom.NewHistogram(_metricSeconds)),
metrics.WithRequests(prom.NewCounter(_metricRequests)),
),
),
)
// grpc client
conn, err := grpc.DialInsecure(
context.Background(),
grpc.WithEndpoint("127.0.0.1:9000"),
grpc.WithMiddleware(
metrics.Client(
metrics.WithSeconds(prom.NewHistogram(_metricSeconds)),
metrics.WithRequests(prom.NewCounter(_metricRequests)),
),
),
)
// http client
conn, err := http.NewClient(
context.Background(),
http.WithEndpoint("127.0.0.1:8000"),
http.WithMiddleware(
metrics.Client(
metrics.WithSeconds(prom.NewHistogram(_metricSeconds)),
metrics.WithRequests(prom.NewCounter(_metricRequests)),
),
),
)
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: