README
¶
metrics
The grpc's server-side and client-side metrics can continue to be captured using prometheus.
Example of use
grpc server
import "github.com/zhufuyi/sponge/pkg/grpc/metrics"
func UnaryServerLabels(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
// set up prometheus custom labels
tag := grpc_ctxtags.NewTags().
Set(serverNameLabelKey, serverNameLabelValue).
Set(envLabelKey, envLabelValue)
newCtx := grpc_ctxtags.SetInContext(ctx, tag)
return handler(newCtx, req)
}
func getServerOptions() []grpc.ServerOption {
var options []grpc.ServerOption
// metrics interceptor
option := grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
//UnaryServerLabels, // tag
metrics.UnaryServerMetrics(
// metrics.WithCounterMetrics(customizedCounterMetric) // adding custom metrics
),
))
options = append(options, option)
option = grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
metrics.StreamServerMetrics(), // metrics interceptor for streaming rpc
))
options = append(options, option)
return options
}
func main() {
rand.Seed(time.Now().UnixNano())
addr := ":8282"
fmt.Println("start rpc server", addr)
list, err := net.Listen("tcp", addr)
if err != nil {
panic(err)
}
server := grpc.NewServer(getServerOptions()...)
serverNameV1.RegisterGreeterServer(server, &GreeterServer{})
// start metrics server, collect grpc metrics by default, turn on, go metrics
metrics.ServerHTTPService(":8283", server)
fmt.Println("start metrics server", ":8283")
err = server.Serve(list)
if err != nil {
panic(err)
}
}
grpc client
import "github.com/zhufuyi/sponge/pkg/grpc/metrics"
func getDialOptions() []grpc.DialOption {
var options []grpc.DialOption
// use insecure transfer
options = append(options, grpc.WithTransportCredentials(insecure.NewCredentials()))
// Metrics
options = append(options, grpc.WithUnaryInterceptor(metrics.UnaryClientMetrics()))
options = append(options, grpc.WithStreamInterceptor(metrics.StreamClientMetrics()))
return options
}
func main() {
conn, err := grpc.Dial("127.0.0.1:8282", getDialOptions()...)
metrics.ClientHTTPService(":8284")
fmt.Println("start metrics server", ":8284")
client := serverNameV1.NewGreeterClient(conn)
i := 0
for {
i++
time.Sleep(time.Millisecond * 500) // qps is 2
err = sayHello(client, i)
if err != nil {
fmt.Println(err)
}
}
}
Documentation
¶
Overview ¶
Package metrics is grpc's server-side and client-side metrics can continue to be captured using prometheus.
Index ¶
- func ClientHTTPService(addr string) *http.Server
- func ClientRegister(mux *http.ServeMux)
- func Register(mux *http.ServeMux, grpcServer *grpc.Server)
- func ServerHTTPService(addr string, grpcServer *grpc.Server) *http.Server
- func SetClientPattern(pattern string)
- func SetServerPattern(pattern string)
- func StreamClientMetrics() grpc.StreamClientInterceptor
- func StreamServerMetrics(opts ...Option) grpc.StreamServerInterceptor
- func UnaryClientMetrics() grpc.UnaryClientInterceptor
- func UnaryServerMetrics(opts ...Option) grpc.UnaryServerInterceptor
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClientHTTPService ¶
ClientHTTPService initialize the client's prometheus exporter service and use http://ip:port/metrics to fetch data
func ClientRegister ¶ added in v1.2.0
ClientRegister for http routing and grpc methods
func ServerHTTPService ¶ added in v1.2.0
ServerHTTPService initialize the prometheus exporter service on the server side and fetch data using http://ip:port/metrics
func SetClientPattern ¶ added in v1.2.0
func SetClientPattern(pattern string)
SetClientPattern set the client pattern
func SetServerPattern ¶ added in v1.2.0
func SetServerPattern(pattern string)
SetServerPattern set the server pattern
func StreamClientMetrics ¶
func StreamClientMetrics() grpc.StreamClientInterceptor
StreamClientMetrics metrics stream interceptor
func StreamServerMetrics ¶
func StreamServerMetrics(opts ...Option) grpc.StreamServerInterceptor
StreamServerMetrics metrics stream interceptor
func UnaryClientMetrics ¶
func UnaryClientMetrics() grpc.UnaryClientInterceptor
UnaryClientMetrics metrics unary interceptor
func UnaryServerMetrics ¶
func UnaryServerMetrics(opts ...Option) grpc.UnaryServerInterceptor
UnaryServerMetrics metrics unary interceptor
Types ¶
type Option ¶ added in v1.2.0
type Option func(*options)
Option set metrics
func WithCounterMetrics ¶
func WithCounterMetrics(metrics ...*prometheus.CounterVec) Option
WithCounterMetrics add Counter type indicator
func WithGaugeMetrics ¶
func WithGaugeMetrics(metrics ...*prometheus.GaugeVec) Option
WithGaugeMetrics add Gauge type indicator
func WithHistogramMetrics ¶
func WithHistogramMetrics(metrics ...*prometheus.HistogramVec) Option
WithHistogramMetrics adding Histogram type indicators
func WithSummaryMetrics ¶
func WithSummaryMetrics(metrics ...*prometheus.SummaryVec) Option
WithSummaryMetrics add Summary type indicator