2021-04-19 13:42:47 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
|
|
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
|
|
// or implied. See the License for the specific language governing permissions and limitations under the License.
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
package trace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-02 13:48:25 +08:00
|
|
|
"errors"
|
2021-03-23 01:49:50 +08:00
|
|
|
"io"
|
2021-02-23 09:58:06 +08:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2021-09-09 10:06:29 +08:00
|
|
|
"sync"
|
2021-02-23 09:58:06 +08:00
|
|
|
|
2021-10-06 07:04:02 +08:00
|
|
|
slog "github.com/milvus-io/milvus/internal/log"
|
2021-02-23 09:58:06 +08:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/opentracing/opentracing-go/log"
|
|
|
|
"github.com/uber/jaeger-client-go"
|
2021-03-23 01:49:50 +08:00
|
|
|
"github.com/uber/jaeger-client-go/config"
|
2021-10-06 07:04:02 +08:00
|
|
|
"go.uber.org/zap"
|
2021-02-23 09:58:06 +08:00
|
|
|
)
|
|
|
|
|
2021-09-09 10:06:29 +08:00
|
|
|
var tracingCloserMtx sync.Mutex
|
|
|
|
var tracingCloser io.Closer
|
|
|
|
|
2021-09-17 11:17:57 +08:00
|
|
|
// InitTracing init global trace from env. If not specified, use default config.
|
2021-03-26 15:13:33 +08:00
|
|
|
func InitTracing(serviceName string) io.Closer {
|
2021-09-09 10:06:29 +08:00
|
|
|
tracingCloserMtx.Lock()
|
|
|
|
defer tracingCloserMtx.Unlock()
|
|
|
|
|
|
|
|
if tracingCloser != nil {
|
|
|
|
return tracingCloser
|
|
|
|
}
|
|
|
|
|
2021-09-03 15:41:25 +08:00
|
|
|
cfg := &config.Configuration{
|
|
|
|
ServiceName: serviceName,
|
|
|
|
Sampler: &config.SamplerConfig{
|
|
|
|
Type: "const",
|
|
|
|
Param: 0,
|
|
|
|
},
|
2021-03-26 15:13:33 +08:00
|
|
|
}
|
2021-03-23 01:49:50 +08:00
|
|
|
if true {
|
2021-09-17 16:29:51 +08:00
|
|
|
cfg = initFromEnv(serviceName)
|
2021-03-23 01:49:50 +08:00
|
|
|
}
|
2021-03-26 15:13:33 +08:00
|
|
|
tracer, closer, err := cfg.NewTracer()
|
2021-09-09 10:06:29 +08:00
|
|
|
tracingCloser = closer
|
2021-03-26 15:13:33 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2021-09-09 10:06:29 +08:00
|
|
|
tracingCloser = nil
|
2021-03-23 01:49:50 +08:00
|
|
|
}
|
2021-03-26 15:13:33 +08:00
|
|
|
opentracing.SetGlobalTracer(tracer)
|
2021-09-09 10:06:29 +08:00
|
|
|
|
|
|
|
return tracingCloser
|
2021-03-23 01:49:50 +08:00
|
|
|
}
|
|
|
|
|
2021-09-17 16:29:51 +08:00
|
|
|
func initFromEnv(serviceName string) *config.Configuration {
|
2021-09-03 15:41:25 +08:00
|
|
|
cfg, err := config.FromEnv()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cfg.ServiceName = serviceName
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2021-12-28 13:24:51 +08:00
|
|
|
// StartSpanFromContext starts an opentracing span. The default operation name is
|
2021-09-17 11:17:57 +08:00
|
|
|
// upper two call stacks of the function
|
2021-02-23 09:58:06 +08:00
|
|
|
func StartSpanFromContext(ctx context.Context, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
|
2021-09-29 14:22:00 +08:00
|
|
|
return StartSpanFromContextWithSkip(ctx, 3, opts...)
|
2021-09-03 15:41:25 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 13:22:17 +08:00
|
|
|
// StartSpanFromContextWithSkip starts an opentracing span with call skip. The operation
|
2021-09-17 11:17:57 +08:00
|
|
|
// name is upper @skip call stacks of the function
|
2021-09-03 15:41:25 +08:00
|
|
|
func StartSpanFromContextWithSkip(ctx context.Context, skip int, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
|
2021-02-23 09:58:06 +08:00
|
|
|
if ctx == nil {
|
2021-10-21 13:46:35 +08:00
|
|
|
return NoopSpan(), nil
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var pcs [1]uintptr
|
2021-09-03 15:41:25 +08:00
|
|
|
n := runtime.Callers(skip, pcs[:])
|
2021-02-23 09:58:06 +08:00
|
|
|
if n < 1 {
|
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "unknown", opts...)
|
|
|
|
span.LogFields(log.Error(errors.New("runtime.Callers failed")))
|
|
|
|
return span, ctx
|
|
|
|
}
|
2021-09-29 22:02:00 +08:00
|
|
|
frames := runtime.CallersFrames(pcs[:])
|
|
|
|
frame, _ := frames.Next()
|
|
|
|
name := frame.Function
|
2021-02-23 09:58:06 +08:00
|
|
|
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...)
|
|
|
|
|
2021-09-29 22:02:00 +08:00
|
|
|
file, line := frame.File, frame.Line
|
2021-02-23 09:58:06 +08:00
|
|
|
span.LogFields(log.String("filename", file), log.Int("line", line))
|
|
|
|
|
|
|
|
return span, opentracing.ContextWithSpan(ctx, span)
|
|
|
|
}
|
|
|
|
|
2022-01-07 14:01:50 +08:00
|
|
|
// StartSpanFromContextWithOperationName starts an opentracing span with specific operation name.
|
2021-09-17 11:17:57 +08:00
|
|
|
// And will log print the current call line number and file name.
|
2021-02-23 09:58:06 +08:00
|
|
|
func StartSpanFromContextWithOperationName(ctx context.Context, operationName string, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
|
2021-09-28 22:14:03 +08:00
|
|
|
return StartSpanFromContextWithOperationNameWithSkip(ctx, operationName, 3, opts...)
|
2021-09-03 15:41:25 +08:00
|
|
|
}
|
|
|
|
|
2022-01-07 13:25:42 +08:00
|
|
|
// StartSpanFromContextWithOperationNameWithSkip starts an opentracing span with specific operation name.
|
2021-09-17 11:17:57 +08:00
|
|
|
// And will log print the current call line number and file name.
|
2021-09-03 15:41:25 +08:00
|
|
|
func StartSpanFromContextWithOperationNameWithSkip(ctx context.Context, operationName string, skip int, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
|
2021-02-23 09:58:06 +08:00
|
|
|
if ctx == nil {
|
2021-10-21 13:46:35 +08:00
|
|
|
return NoopSpan(), nil
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var pcs [1]uintptr
|
2021-09-03 15:41:25 +08:00
|
|
|
n := runtime.Callers(skip, pcs[:])
|
2021-02-23 09:58:06 +08:00
|
|
|
if n < 1 {
|
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, operationName, opts...)
|
|
|
|
span.LogFields(log.Error(errors.New("runtime.Callers failed")))
|
|
|
|
return span, ctx
|
|
|
|
}
|
2021-09-29 22:02:00 +08:00
|
|
|
frames := runtime.CallersFrames(pcs[:])
|
|
|
|
frame, _ := frames.Next()
|
|
|
|
file, line := frame.File, frame.Line
|
2021-02-23 09:58:06 +08:00
|
|
|
|
|
|
|
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-09-17 11:17:57 +08:00
|
|
|
// LogError is a method to log error with span.
|
2021-10-06 07:04:02 +08:00
|
|
|
func LogError(span opentracing.Span, err error) {
|
2021-02-23 09:58:06 +08:00
|
|
|
if err == nil {
|
2021-10-06 07:04:02 +08:00
|
|
|
return
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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")))
|
2021-10-06 07:04:02 +08:00
|
|
|
slog.Warn("trace log error failed", zap.Error(err))
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-29 22:02:00 +08:00
|
|
|
frames := runtime.CallersFrames(pcs[:])
|
|
|
|
frame, _ := frames.Next()
|
|
|
|
file, line := frame.File, frame.Line
|
2021-02-23 09:58:06 +08:00
|
|
|
span.LogFields(log.String("filename", file), log.Int("line", line), log.Error(err))
|
|
|
|
}
|
|
|
|
|
2021-09-17 11:17:57 +08:00
|
|
|
// InfoFromSpan is a method return span details.
|
2021-10-29 11:06:37 +08:00
|
|
|
func InfoFromSpan(span opentracing.Span) (traceID string, sampled, found bool) {
|
2021-09-03 15:41:25 +08:00
|
|
|
if span != nil {
|
|
|
|
if spanContext, ok := span.Context().(jaeger.SpanContext); ok {
|
|
|
|
traceID = spanContext.TraceID().String()
|
|
|
|
sampled = spanContext.IsSampled()
|
|
|
|
return traceID, sampled, true
|
|
|
|
}
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
return "", false, false
|
|
|
|
}
|
|
|
|
|
2021-09-17 11:17:57 +08:00
|
|
|
// InfoFromContext is a method return details of span associated with context.
|
2021-10-28 15:58:29 +08:00
|
|
|
func InfoFromContext(ctx context.Context) (traceID string, sampled, found bool) {
|
2021-09-03 15:41:25 +08:00
|
|
|
if ctx != nil {
|
|
|
|
if span := opentracing.SpanFromContext(ctx); span != nil {
|
|
|
|
return InfoFromSpan(span)
|
|
|
|
}
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
return "", false, false
|
|
|
|
}
|
|
|
|
|
2021-09-17 11:17:57 +08:00
|
|
|
// InjectContextToPulsarMsgProperties is a method inject span to pulsr message.
|
2021-02-23 09:58:06 +08:00
|
|
|
func InjectContextToPulsarMsgProperties(sc opentracing.SpanContext, properties map[string]string) {
|
|
|
|
tracer := opentracing.GlobalTracer()
|
2021-04-02 13:48:25 +08:00
|
|
|
tracer.Inject(sc, opentracing.TextMap, PropertiesReaderWriter{properties})
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-26 21:15:56 +08:00
|
|
|
// PropertiesReaderWriter is for saving trce in pulsar msg properties.
|
|
|
|
// Implement Set and ForeachKey methods.
|
2021-04-02 13:48:25 +08:00
|
|
|
type PropertiesReaderWriter struct {
|
|
|
|
PpMap map[string]string
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-26 21:15:56 +08:00
|
|
|
// Set sets key, value to PpMap.
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ppRW PropertiesReaderWriter) Set(key, val string) {
|
2021-02-23 09:58:06 +08:00
|
|
|
key = strings.ToLower(key)
|
2021-04-02 13:48:25 +08:00
|
|
|
ppRW.PpMap[key] = val
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
|
2021-10-06 23:28:06 +08:00
|
|
|
// ForeachKey iterates each key value of PpMap.
|
2021-04-02 13:48:25 +08:00
|
|
|
func (ppRW PropertiesReaderWriter) ForeachKey(handler func(key, val string) error) error {
|
|
|
|
for k, val := range ppRW.PpMap {
|
2021-02-23 09:58:06 +08:00
|
|
|
if err := handler(k, val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-26 21:15:56 +08:00
|
|
|
// NoopSpan is a minimal span to reduce overhead.
|
2021-04-02 13:48:25 +08:00
|
|
|
func NoopSpan() opentracing.Span {
|
2021-02-23 09:58:06 +08:00
|
|
|
return opentracing.NoopTracer{}.StartSpan("Default-span")
|
|
|
|
}
|