2015-12-02 11:54:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/client"
|
|
|
|
"github.com/micro/go-micro/cmd"
|
|
|
|
example "github.com/micro/go-micro/examples/server/proto/example"
|
2016-01-28 17:55:28 +00:00
|
|
|
"github.com/micro/go-micro/metadata"
|
2015-12-02 11:54:36 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// wrapper example code
|
|
|
|
|
|
|
|
// log wrapper logs every time a request is made
|
|
|
|
type logWrapper struct {
|
|
|
|
client.Client
|
|
|
|
}
|
|
|
|
|
2015-12-08 19:25:42 +00:00
|
|
|
func (l *logWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
2016-01-28 17:55:28 +00:00
|
|
|
md, _ := metadata.FromContext(ctx)
|
2015-12-02 11:54:36 +00:00
|
|
|
fmt.Printf("[Log Wrapper] ctx: %v service: %s method: %s\n", md, req.Service(), req.Method())
|
|
|
|
return l.Client.Call(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// trace wrapper attaches a unique trace ID - timestamp
|
|
|
|
type traceWrapper struct {
|
|
|
|
client.Client
|
|
|
|
}
|
|
|
|
|
2015-12-08 19:25:42 +00:00
|
|
|
func (t *traceWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
2016-01-28 17:55:28 +00:00
|
|
|
ctx = metadata.NewContext(ctx, map[string]string{
|
2015-12-02 11:54:36 +00:00
|
|
|
"X-Trace-Id": fmt.Sprintf("%d", time.Now().Unix()),
|
|
|
|
})
|
|
|
|
return t.Client.Call(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements client.Wrapper as logWrapper
|
|
|
|
func logWrap(c client.Client) client.Client {
|
|
|
|
return &logWrapper{c}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements client.Wrapper as traceWrapper
|
|
|
|
func traceWrap(c client.Client) client.Client {
|
|
|
|
return &traceWrapper{c}
|
|
|
|
}
|
|
|
|
|
2016-11-07 20:11:58 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 11:54:36 +00:00
|
|
|
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{
|
|
|
|
Name: "John",
|
|
|
|
})
|
|
|
|
|
|
|
|
// create context with metadata
|
2016-01-28 17:55:28 +00:00
|
|
|
ctx := metadata.NewContext(context.Background(), map[string]string{
|
2015-12-02 11:54:36 +00:00
|
|
|
"X-User-Id": "john",
|
|
|
|
"X-From-Id": "script",
|
|
|
|
})
|
|
|
|
|
|
|
|
rsp := &example.Response{}
|
|
|
|
|
|
|
|
// Call service
|
|
|
|
if err := client.Call(ctx, req, rsp); err != nil {
|
|
|
|
fmt.Println("call err: ", err, rsp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Call:", i, "rsp:", rsp.Msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cmd.Init()
|
|
|
|
|
|
|
|
fmt.Println("\n--- Log Wrapper example ---\n")
|
|
|
|
|
|
|
|
// Wrap the default client
|
|
|
|
client.DefaultClient = logWrap(client.DefaultClient)
|
|
|
|
|
|
|
|
call(0)
|
|
|
|
|
|
|
|
fmt.Println("\n--- Log+Trace Wrapper example ---\n")
|
|
|
|
|
|
|
|
// Wrap using client.Wrap option
|
|
|
|
client.DefaultClient = client.NewClient(
|
|
|
|
client.Wrap(traceWrap),
|
|
|
|
client.Wrap(logWrap),
|
|
|
|
)
|
|
|
|
|
|
|
|
call(1)
|
2016-11-07 20:11:58 +00:00
|
|
|
|
|
|
|
fmt.Println("\n--- Metrics Wrapper example ---\n")
|
|
|
|
|
|
|
|
// Wrap using client.Wrap option
|
|
|
|
client.DefaultClient = client.NewClient(
|
|
|
|
client.WrapCall(metricsWrap),
|
|
|
|
)
|
|
|
|
|
|
|
|
call(2)
|
2015-12-02 11:54:36 +00:00
|
|
|
}
|