From a5e5f04bcf8d84efabcef0e8d763d6c49d17a888 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sun, 26 Feb 2023 09:35:59 +0800 Subject: [PATCH] chore: reformat code (#2925) --- core/stores/mon/trace.go | 6 +----- core/stores/redis/hook.go | 1 - core/stores/sqlx/trace.go | 6 +----- core/trace/utils.go | 38 +++++++++++++++++++------------------- 4 files changed, 21 insertions(+), 30 deletions(-) diff --git a/core/stores/mon/trace.go b/core/stores/mon/trace.go index 8fd8212d..1c9d6061 100644 --- a/core/stores/mon/trace.go +++ b/core/stores/mon/trace.go @@ -14,11 +14,7 @@ var mongoCmdAttributeKey = attribute.Key("mongo.cmd") func startSpan(ctx context.Context, cmd string) (context.Context, oteltrace.Span) { tracer := trace.TracerFromContext(ctx) - - ctx, span := tracer.Start(ctx, - spanName, - oteltrace.WithSpanKind(oteltrace.SpanKindClient), - ) + ctx, span := tracer.Start(ctx, spanName, oteltrace.WithSpanKind(oteltrace.SpanKindClient)) span.SetAttributes(mongoCmdAttributeKey.String(cmd)) return ctx, span diff --git a/core/stores/redis/hook.go b/core/stores/redis/hook.go index ba0d79ef..856c5d5d 100644 --- a/core/stores/redis/hook.go +++ b/core/stores/redis/hook.go @@ -14,7 +14,6 @@ import ( "github.com/zeromicro/go-zero/core/mapping" "github.com/zeromicro/go-zero/core/timex" "github.com/zeromicro/go-zero/core/trace" - "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" oteltrace "go.opentelemetry.io/otel/trace" diff --git a/core/stores/sqlx/trace.go b/core/stores/sqlx/trace.go index b256a6c6..6a3a0406 100644 --- a/core/stores/sqlx/trace.go +++ b/core/stores/sqlx/trace.go @@ -14,11 +14,7 @@ var sqlAttributeKey = attribute.Key("sql.method") func startSpan(ctx context.Context, method string) (context.Context, oteltrace.Span) { tracer := trace.TracerFromContext(ctx) - - start, span := tracer.Start(ctx, - spanName, - oteltrace.WithSpanKind(oteltrace.SpanKindClient), - ) + start, span := tracer.Start(ctx, spanName, oteltrace.WithSpanKind(oteltrace.SpanKindClient)) span.SetAttributes(sqlAttributeKey.String(method)) return start, span diff --git a/core/trace/utils.go b/core/trace/utils.go index 3fad0cfc..c665b89c 100644 --- a/core/trace/utils.go +++ b/core/trace/utils.go @@ -22,25 +22,6 @@ var ( TraceIDFromContext = ztrace.TraceIDFromContext ) -// PeerFromCtx returns the peer from ctx. -func PeerFromCtx(ctx context.Context) string { - p, ok := peer.FromContext(ctx) - if !ok || p == nil { - return "" - } - - return p.Addr.String() -} - -// SpanInfo returns the span info. -func SpanInfo(fullMethod, peerAddress string) (string, []attribute.KeyValue) { - attrs := []attribute.KeyValue{RPCSystemGRPC} - name, mAttrs := ParseFullMethod(fullMethod) - attrs = append(attrs, mAttrs...) - attrs = append(attrs, PeerAttr(peerAddress)...) - return name, attrs -} - // ParseFullMethod returns the method name and attributes. func ParseFullMethod(fullMethod string) (string, []attribute.KeyValue) { name := strings.TrimLeft(fullMethod, "/") @@ -78,6 +59,25 @@ func PeerAttr(addr string) []attribute.KeyValue { } } +// PeerFromCtx returns the peer from ctx. +func PeerFromCtx(ctx context.Context) string { + p, ok := peer.FromContext(ctx) + if !ok || p == nil { + return "" + } + + return p.Addr.String() +} + +// SpanInfo returns the span info. +func SpanInfo(fullMethod, peerAddress string) (string, []attribute.KeyValue) { + attrs := []attribute.KeyValue{RPCSystemGRPC} + name, mAttrs := ParseFullMethod(fullMethod) + attrs = append(attrs, mAttrs...) + attrs = append(attrs, PeerAttr(peerAddress)...) + return name, attrs +} + // TracerFromContext returns a tracer in ctx, otherwise returns a global tracer. func TracerFromContext(ctx context.Context) (tracer trace.Tracer) { if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {