From a4d8286e361397aea6780e9d5ba7c5efcf617ec8 Mon Sep 17 00:00:00 2001 From: SunJun Date: Mon, 6 Sep 2021 14:31:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=90=AFotel=E5=90=8E=EF=BC=8Ctracelo?= =?UTF-8?q?g=E8=87=AA=E5=8A=A8=E8=8E=B7=E5=8F=96otle=E7=9A=84traceId?= =?UTF-8?q?=E5=92=8CspanId=20(#946)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 开启otel后,tracelog自动获取otle的traceId和spanId * 去除opentelemetry判断 * 通过 IsRecording 判断span是否活跃 --- core/logx/tracelogger.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core/logx/tracelogger.go b/core/logx/tracelogger.go index 1aba7158..a1b8b2e1 100644 --- a/core/logx/tracelogger.go +++ b/core/logx/tracelogger.go @@ -6,6 +6,8 @@ import ( "io" "time" + "go.opentelemetry.io/otel/trace" + "github.com/tal-tech/go-zero/core/timex" "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 { + span := trace.SpanFromContext(ctx) + if span.IsRecording() { + return span.SpanContext().SpanID().String() + } + t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace) if !ok { return "" @@ -102,6 +109,11 @@ func spanIdFromContext(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) if !ok { return ""