From c277a3ecc0efa214021cd67bdf793735688a9f28 Mon Sep 17 00:00:00 2001 From: Asim Date: Mon, 7 Nov 2016 20:11:58 +0000 Subject: [PATCH] Add example of client call func wrap --- examples/client/wrapper/wrapper.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/examples/client/wrapper/wrapper.go b/examples/client/wrapper/wrapper.go index b87c62af..ba90d67e 100644 --- a/examples/client/wrapper/wrapper.go +++ b/examples/client/wrapper/wrapper.go @@ -46,6 +46,15 @@ func traceWrap(c client.Client) client.Client { return &traceWrapper{c} } +func metricsWrap(cf client.CallFunc) client.CallFunc { + return func(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error { + t := time.Now() + err := cf(ctx, addr, req, rsp, opts) + fmt.Printf("[Metrics Wrapper] called: %s %s.%s duration: %v\n", addr, req.Service(), req.Method(), time.Since(t)) + return err + } +} + func call(i int) { // Create new request to service go.micro.srv.example, method Example.Call req := client.NewRequest("go.micro.srv.example", "Example.Call", &example.Request{ @@ -88,4 +97,13 @@ func main() { ) call(1) + + fmt.Println("\n--- Metrics Wrapper example ---\n") + + // Wrap using client.Wrap option + client.DefaultClient = client.NewClient( + client.WrapCall(metricsWrap), + ) + + call(2) }