Context Propagation Go
This middleware is used for support propagate context between micro services.
For this version, we propagate context by opentracing baggage protocol.
Supported framework for auto inject and extract:
How to use
Operate data from context
Before get or set data from context, you should enable auto inject and extract.
go get -u github.com/AminoApps/context-propagation-go
package main
import cp "github.com/AminoApps/context-propagation-go"
ctx = cp.SetValueToContext(ctx, "my-key", "my-value")
valye := cp.GetValueFromContext(context.Background(), "my-key")
Auto inject and extract
Gin
go get -u github.com/AminoApps/context-propagation-go/module/context-propagation-gin
package main
import cpgin "github.com/AminoApps/context-propagation-go/module/context-propagation-gin"
e := gin.New()
e.Use(cpgin.Middleware())
// For propagation context from gin, please use the context from the request
func TestApi(c *gin.Context) {
GetDataFromDataBase(c.Request.Context())
}
Http Client/Server
go get -u github.com/AminoApps/context-propagation-go/module/context-propagation-http
package main
import cphttp "github.com/AminoApps/context-propagation-go/module/context-propagation-http"
http.ListenAndServe(":8080", cphttp.Wrap(myHandler))
client := cphttp.WrapClient(&http.Client{})
// Please use the ctxhttp to wrap the request.
resp, err := ctxhttp.Get(ctx, client, "http://127.0.0.1:8080/test")
Grpc Client/Server
go get -u github.com/AminoApps/context-propagation-go/module/context-propagation-grpc
package main
import cpgrpc "github.com/AminoApps/context-propagation-go/module/context-propagation-grpc"
server := grpc.NewServer(grpc.UnaryInterceptor(cpgrpc.NewUnaryServerInterceptor()))
client := grpc.Dial(address, grpc.WithUnaryInterceptor(cpgrpc.NewUnaryClientInterceptor()))