开启otel后,tracelog自动获取otle的traceId和spanId (#946)

* 开启otel后,tracelog自动获取otle的traceId和spanId

* 去除opentelemetry判断

* 通过 IsRecording 判断span是否活跃
master
SunJun 3 years ago committed by GitHub
parent 84d2b64e7c
commit a4d8286e36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,6 +6,8 @@ import (
"io" "io"
"time" "time"
"go.opentelemetry.io/otel/trace"
"github.com/tal-tech/go-zero/core/timex" "github.com/tal-tech/go-zero/core/timex"
"github.com/tal-tech/go-zero/core/trace/tracespec" "github.com/tal-tech/go-zero/core/trace/tracespec"
) )
@ -93,6 +95,11 @@ func WithContext(ctx context.Context) Logger {
} }
func spanIdFromContext(ctx context.Context) string { func spanIdFromContext(ctx context.Context) string {
span := trace.SpanFromContext(ctx)
if span.IsRecording() {
return span.SpanContext().SpanID().String()
}
t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace) t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace)
if !ok { if !ok {
return "" return ""
@ -102,6 +109,11 @@ func spanIdFromContext(ctx context.Context) string {
} }
func traceIdFromContext(ctx context.Context) string { func traceIdFromContext(ctx context.Context) string {
span := trace.SpanFromContext(ctx)
if span.IsRecording() {
return span.SpanContext().SpanID().String()
}
t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace) t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace)
if !ok { if !ok {
return "" return ""

Loading…
Cancel
Save