jaeger
package main
import (
"fmt"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
)
func main() {
cfg := jaegercfg.Configuration{
Sampler: &jaegercfg.SamplerConfig{ //采样类型
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &jaegercfg.ReporterConfig{ //上报配置
LogSpans: true, //打印日志
LocalAgentHostPort: "192.168.0.102:6831",
},
//Disabled:
ServiceName: "chengpeng", //服务名
}
fmt.Println("chengpeng", cfg)
tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))
if err != nil {
panic(err)
}
defer closer.Close()
span := tracer.StartSpan("go-grpc-web")
defer span.Finish()
}
相关功能讲解
package main
import (
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
"time"
jaegercfg "github.com/uber/jaeger-client-go/config"
)
func main() {
//var a []opentracing.Tag
//a = append(a, opentracing.Tag{Key: "chengpeng", Value: 123})
cfg := jaegercfg.Configuration{
Sampler: &jaegercfg.SamplerConfig{ //采样类型
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &jaegercfg.ReporterConfig{ //上报配置
LogSpans: true, //打印日志
LocalAgentHostPort: "192.168.0.102:6831", //当前ip
},
//Disabled:
ServiceName: "chengpeng", //服务名
}
tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))
if err != nil {
panic(err)
}
defer closer.Close()
parentSpan := tracer.StartSpan("main")
span := tracer.StartSpan("funcA", opentracing.ChildOf(parentSpan.Context()))
time.Sleep(time.Millisecond * 500)
span.Finish()
time.Sleep(time.Millisecond * 50)
span2 := tracer.StartSpan("funcB", opentracing.ChildOf(parentSpan.Context()))
time.Sleep(time.Millisecond * 1000)
span2.Finish()
parentSpan.Finish()
}
go-grpc-opentracing
package main
import (
"context"
"fmt"
"OldPackageTest/jaeger_test/otgrpc"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
"google.golang.org/grpc"
"OldPackageTest/grpc_test/proto"
)
func main() {
cfg := jaegercfg.Configuration{
Sampler: &jaegercfg.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &jaegercfg.ReporterConfig{
LogSpans: true,
LocalAgentHostPort: "192.168.0.104:6831",
},
ServiceName: "mxshop",
}
tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))
if err != nil {
panic(err)
}
opentracing.SetGlobalTracer(tracer)
defer closer.Close()
conn, err := grpc.Dial("127.0.0.1:50051", grpc.WithInsecure(), grpc.WithUnaryInterceptor(otgrpc.OpenTracingClientInterceptor(opentracing.GlobalTracer())))
if err != nil {
panic(err)
}
defer conn.Close()
c := proto.NewGreeterClient(conn)
r, err := c.SayHello(context.Background(), &proto.HelloRequest{Name: "bobby"})
if err != nil {
panic(err)
}
fmt.Println(r.Message)
}
修改资源绑定的代码
func OpenTracingClientInterceptor(tracer opentracing.Tracer, optFuncs ...Option) grpc.UnaryClientInterceptor {
otgrpcOpts := newOptions()
otgrpcOpts.apply(optFuncs...)
return func(
ctx context.Context,
method string,
req, resp interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
var err error
var parentCtx opentracing.SpanContext
if parent := opentracing.SpanFromContext(ctx); parent != nil {
parentCtx = parent.Context()
}
//fmt.Println("chengpengtext", parentCtx)
ginContext := ctx.Value("ginContext")
switch ginContext.(type) {
case *gin.Context:
if itracer, ok := ginContext.(*gin.Context).Get("tracer"); ok {
tracer = itracer.(opentracing.Tracer)
}
if parentSpan, ok := ginContext.(*gin.Context).Get("parentSpan"); ok {
parentCtx = parentSpan.(*jaegerClient.Span).Context()
}
}
//fmt.Println("chengpengtext1", ginContext)
//fmt.Println("chengpengtext2", tracer)
//fmt.Println("chengpengtext3", parentCtx)
if otgrpcOpts.inclusionFunc != nil &&
!otgrpcOpts.inclusionFunc(parentCtx, method, req, resp) {
return invoker(ctx, method, req, resp, cc, opts...)
}
clientSpan := tracer.StartSpan(
method,
opentracing.ChildOf(parentCtx),
ext.SpanKindRPCClient,
gRPCComponentTag,
)
defer clientSpan.Finish()
ctx = injectSpanContext(ctx, tracer, clientSpan)
if otgrpcOpts.logPayloads {
clientSpan.LogFields(log.Object("gRPC request", req))
}
err = invoker(ctx, method, req, resp, cc, opts...)
if err == nil {
if otgrpcOpts.logPayloads {
clientSpan.LogFields(log.Object("gRPC response", resp))
}
} else {
SetSpanTags(clientSpan, err, true)
clientSpan.LogFields(log.String("event", "error"), log.String("message", err.Error()))
}
if otgrpcOpts.decorator != nil {
otgrpcOpts.decorator(clientSpan, method, req, resp, err)
}
return err
}
}
package middlewares
import (
"chengpeng_api/goods-web/global"
"fmt"
"github.com/gin-gonic/gin"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
//"google.golang.org/grpc"
)
// Trace 链路跟踪
func Trace() gin.HandlerFunc {
return func(ctx *gin.Context) {
cfg := jaegercfg.Configuration{
Sampler: &jaegercfg.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &jaegercfg.ReporterConfig{
LogSpans: true,
LocalAgentHostPort: fmt.Sprintf("%s:%d", global.ServerConfig.JaegerInfo.Host, global.ServerConfig.JaegerInfo.Port),
},
ServiceName: global.ServerConfig.JaegerInfo.Name,
}
tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))
if err != nil {
panic(err)
}
//opentracing.SetGlobalTracer(tracer) //设置为全局 这样使用就会出问题 不能放到同一个tracer
defer closer.Close()
fmt.Println("chengpengTrace", ctx.Request.URL.Path) //每一个api都是trace
startSpan := tracer.StartSpan(ctx.Request.URL.Path) //起始的Span
defer startSpan.Finish()
//放到ctx中 需要直接传递过去
ctx.Set("tracer", tracer)
ctx.Set("parentSpan", startSpan)
ctx.Next()
}
}
这样就会设置gin.conext中去,然后通过图片上的就可以放到context中
grpc的server端如何获取客户端