2021-02-23 01:58:06 +00:00
|
|
|
package trace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-22 17:49:50 +00:00
|
|
|
"io"
|
2021-02-23 01:58:06 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
2021-03-05 02:15:27 +00:00
|
|
|
"errors"
|
|
|
|
|
2021-02-23 01:58:06 +00:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/opentracing/opentracing-go/ext"
|
|
|
|
"github.com/opentracing/opentracing-go/log"
|
|
|
|
"github.com/uber/jaeger-client-go"
|
2021-03-22 17:49:50 +00:00
|
|
|
"github.com/uber/jaeger-client-go/config"
|
2021-02-23 01:58:06 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
)
|
|
|
|
|
2021-03-26 07:13:33 +00:00
|
|
|
func InitTracing(serviceName string) io.Closer {
|
|
|
|
if opentracing.IsGlobalTracerRegistered() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var cfg *config.Configuration
|
|
|
|
var err error
|
2021-03-22 17:49:50 +00:00
|
|
|
if true {
|
2021-03-26 07:13:33 +00:00
|
|
|
cfg, err = config.FromEnv()
|
2021-03-22 17:49:50 +00:00
|
|
|
if err != nil {
|
2021-03-26 07:13:33 +00:00
|
|
|
log.Error(err)
|
|
|
|
return nil
|
2021-03-22 17:49:50 +00:00
|
|
|
}
|
|
|
|
cfg.ServiceName = serviceName
|
2021-03-26 07:13:33 +00:00
|
|
|
} else {
|
|
|
|
cfg = &config.Configuration{
|
|
|
|
ServiceName: serviceName,
|
|
|
|
Sampler: &config.SamplerConfig{
|
|
|
|
Type: "const",
|
|
|
|
Param: 1,
|
|
|
|
},
|
|
|
|
}
|
2021-03-22 17:49:50 +00:00
|
|
|
}
|
2021-03-26 07:13:33 +00:00
|
|
|
tracer, closer, err := cfg.NewTracer()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return nil
|
2021-03-22 17:49:50 +00:00
|
|
|
}
|
2021-03-26 07:13:33 +00:00
|
|
|
opentracing.SetGlobalTracer(tracer)
|
|
|
|
return closer
|
2021-03-22 17:49:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 01:58:06 +00:00
|
|
|
func StartSpanFromContext(ctx context.Context, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
|
|
|
|
if ctx == nil {
|
2021-02-25 09:35:36 +00:00
|
|
|
return noopSpan(), ctx
|
2021-02-23 01:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var pcs [1]uintptr
|
|
|
|
n := runtime.Callers(2, pcs[:])
|
|
|
|
if n < 1 {
|
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "unknown", opts...)
|
|
|
|
span.LogFields(log.Error(errors.New("runtime.Callers failed")))
|
|
|
|
return span, ctx
|
|
|
|
}
|
|
|
|
fn := runtime.FuncForPC(pcs[0])
|
|
|
|
name := fn.Name()
|
|
|
|
if lastSlash := strings.LastIndexByte(name, '/'); lastSlash > 0 {
|
|
|
|
name = name[lastSlash+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if parent := opentracing.SpanFromContext(ctx); parent != nil {
|
|
|
|
opts = append(opts, opentracing.ChildOf(parent.Context()))
|
|
|
|
}
|
|
|
|
span := opentracing.StartSpan(name, opts...)
|
|
|
|
|
|
|
|
file, line := fn.FileLine(pcs[0])
|
|
|
|
span.LogFields(log.String("filename", file), log.Int("line", line))
|
|
|
|
|
|
|
|
return span, opentracing.ContextWithSpan(ctx, span)
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartSpanFromContextWithOperationName(ctx context.Context, operationName string, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
|
|
|
|
if ctx == nil {
|
2021-02-25 09:35:36 +00:00
|
|
|
return noopSpan(), ctx
|
2021-02-23 01:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var pcs [1]uintptr
|
|
|
|
n := runtime.Callers(2, pcs[:])
|
|
|
|
if n < 1 {
|
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, operationName, opts...)
|
|
|
|
span.LogFields(log.Error(errors.New("runtime.Callers failed")))
|
|
|
|
return span, ctx
|
|
|
|
}
|
|
|
|
file, line := runtime.FuncForPC(pcs[0]).FileLine(pcs[0])
|
|
|
|
|
|
|
|
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
|
|
|
|
opts = append(opts, opentracing.ChildOf(parentSpan.Context()))
|
|
|
|
}
|
|
|
|
span := opentracing.StartSpan(operationName, opts...)
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
|
|
|
|
span.LogFields(log.String("filename", file), log.Int("line", line))
|
|
|
|
|
|
|
|
return span, ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func LogError(span opentracing.Span, err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get caller frame.
|
|
|
|
var pcs [1]uintptr
|
|
|
|
n := runtime.Callers(2, pcs[:])
|
|
|
|
if n < 1 {
|
|
|
|
span.LogFields(log.Error(err))
|
|
|
|
span.LogFields(log.Error(errors.New("runtime.Callers failed")))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
file, line := runtime.FuncForPC(pcs[0]).FileLine(pcs[0])
|
|
|
|
span.LogFields(log.String("filename", file), log.Int("line", line), log.Error(err))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func InfoFromSpan(span opentracing.Span) (traceID string, sampled bool, found bool) {
|
|
|
|
if spanContext, ok := span.Context().(jaeger.SpanContext); ok {
|
|
|
|
traceID = spanContext.TraceID().String()
|
|
|
|
sampled = spanContext.IsSampled()
|
|
|
|
return traceID, sampled, true
|
|
|
|
}
|
|
|
|
return "", false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func InfoFromContext(ctx context.Context) (traceID string, sampled bool, found bool) {
|
|
|
|
if span := opentracing.SpanFromContext(ctx); span != nil {
|
|
|
|
return InfoFromSpan(span)
|
|
|
|
}
|
|
|
|
return "", false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func InjectContextToPulsarMsgProperties(sc opentracing.SpanContext, properties map[string]string) {
|
|
|
|
tracer := opentracing.GlobalTracer()
|
|
|
|
tracer.Inject(sc, opentracing.TextMap, propertiesReaderWriter{properties})
|
|
|
|
}
|
|
|
|
|
2021-02-25 09:35:36 +00:00
|
|
|
func ExtractFromPulsarMsgProperties(msg msgstream.TsMsg, properties map[string]string) (opentracing.Span, bool) {
|
2021-02-23 01:58:06 +00:00
|
|
|
if !allowTrace(msg) {
|
2021-02-25 09:35:36 +00:00
|
|
|
return noopSpan(), false
|
2021-02-23 01:58:06 +00:00
|
|
|
}
|
|
|
|
tracer := opentracing.GlobalTracer()
|
|
|
|
sc, _ := tracer.Extract(opentracing.TextMap, propertiesReaderWriter{properties})
|
|
|
|
name := "receive pulsar msg"
|
|
|
|
opts := []opentracing.StartSpanOption{
|
|
|
|
ext.RPCServerOption(sc),
|
|
|
|
opentracing.Tags{
|
|
|
|
"ID": msg.ID(),
|
|
|
|
"Type": msg.Type(),
|
|
|
|
"HashKeys": msg.HashKeys(),
|
|
|
|
"Position": msg.Position(),
|
|
|
|
}}
|
2021-02-25 09:35:36 +00:00
|
|
|
return opentracing.StartSpan(name, opts...), true
|
2021-02-23 01:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MsgSpanFromCtx(ctx context.Context, msg msgstream.TsMsg, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
|
2021-02-25 09:35:36 +00:00
|
|
|
if ctx == nil {
|
|
|
|
return noopSpan(), ctx
|
|
|
|
}
|
2021-02-23 01:58:06 +00:00
|
|
|
if !allowTrace(msg) {
|
|
|
|
return noopSpan(), ctx
|
|
|
|
}
|
2021-02-25 09:35:36 +00:00
|
|
|
operationName := "send pulsar msg"
|
2021-02-23 01:58:06 +00:00
|
|
|
opts = append(opts, opentracing.Tags{
|
|
|
|
"ID": msg.ID(),
|
|
|
|
"Type": msg.Type(),
|
|
|
|
"HashKeys": msg.HashKeys(),
|
|
|
|
"Position": msg.Position(),
|
|
|
|
})
|
2021-02-25 09:35:36 +00:00
|
|
|
|
|
|
|
var pcs [1]uintptr
|
|
|
|
n := runtime.Callers(2, pcs[:])
|
|
|
|
if n < 1 {
|
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, operationName, opts...)
|
|
|
|
span.LogFields(log.Error(errors.New("runtime.Callers failed")))
|
|
|
|
return span, ctx
|
|
|
|
}
|
|
|
|
file, line := runtime.FuncForPC(pcs[0]).FileLine(pcs[0])
|
|
|
|
|
|
|
|
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
|
|
|
|
opts = append(opts, opentracing.ChildOf(parentSpan.Context()))
|
|
|
|
}
|
|
|
|
span := opentracing.StartSpan(operationName, opts...)
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
|
|
|
|
span.LogFields(log.String("filename", file), log.Int("line", line))
|
|
|
|
|
|
|
|
return span, ctx
|
2021-02-23 01:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type propertiesReaderWriter struct {
|
|
|
|
ppMap map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ppRW propertiesReaderWriter) Set(key, val string) {
|
|
|
|
key = strings.ToLower(key)
|
|
|
|
ppRW.ppMap[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ppRW propertiesReaderWriter) ForeachKey(handler func(key, val string) error) error {
|
|
|
|
for k, val := range ppRW.ppMap {
|
|
|
|
if err := handler(k, val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func allowTrace(in interface{}) bool {
|
|
|
|
if in == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch res := in.(type) {
|
|
|
|
case msgstream.TsMsg:
|
2021-03-10 06:45:35 +00:00
|
|
|
return !(res.Type() == commonpb.MsgType_TimeTick ||
|
|
|
|
res.Type() == commonpb.MsgType_QueryNodeStats ||
|
|
|
|
res.Type() == commonpb.MsgType_LoadIndex)
|
2021-02-23 01:58:06 +00:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func noopSpan() opentracing.Span {
|
|
|
|
return opentracing.NoopTracer{}.StartSpan("Default-span")
|
|
|
|
}
|