From 58874779e777a025b92d3288a4c6d076fa9ccda0 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sun, 5 Sep 2021 22:18:35 +0800 Subject: [PATCH] move opentelemetry into trace package, and refactoring (#996) * move opentelemetry into trace package, and refactoring * rename rewritten package names --- core/service/serviceconf.go | 2 +- core/{ => trace}/opentelemetry/agent.go | 5 ++- core/{ => trace}/opentelemetry/attributes.go | 0 .../{ => trace}/opentelemetry/clientstream.go | 35 +++++++++---------- core/{ => trace}/opentelemetry/config.go | 0 core/{ => trace}/opentelemetry/message.go | 6 ++-- .../{ => trace}/opentelemetry/serverstream.go | 2 -- core/{ => trace}/opentelemetry/tracer.go | 8 +++-- core/{ => trace}/opentelemetry/utils.go | 6 ++-- rest/handler/otelhandler.go | 2 +- rest/handler/otelhandler_test.go | 2 +- .../opentracinginterceptor.go | 2 +- .../opentracinginterceptor_test.go | 2 +- .../opentracinginterceptor.go | 2 +- .../opentracinginterceptor_test.go | 2 +- 15 files changed, 38 insertions(+), 38 deletions(-) rename core/{ => trace}/opentelemetry/agent.go (94%) rename core/{ => trace}/opentelemetry/attributes.go (100%) rename core/{ => trace}/opentelemetry/clientstream.go (85%) rename core/{ => trace}/opentelemetry/config.go (100%) rename core/{ => trace}/opentelemetry/message.go (86%) rename core/{ => trace}/opentelemetry/serverstream.go (99%) rename core/{ => trace}/opentelemetry/tracer.go (95%) rename core/{ => trace}/opentelemetry/utils.go (95%) diff --git a/core/service/serviceconf.go b/core/service/serviceconf.go index 32a6bd94..c87d89eb 100644 --- a/core/service/serviceconf.go +++ b/core/service/serviceconf.go @@ -5,9 +5,9 @@ import ( "github.com/tal-tech/go-zero/core/load" "github.com/tal-tech/go-zero/core/logx" - "github.com/tal-tech/go-zero/core/opentelemetry" "github.com/tal-tech/go-zero/core/prometheus" "github.com/tal-tech/go-zero/core/stat" + "github.com/tal-tech/go-zero/core/trace/opentelemetry" ) const ( diff --git a/core/opentelemetry/agent.go b/core/trace/opentelemetry/agent.go similarity index 94% rename from core/opentelemetry/agent.go rename to core/trace/opentelemetry/agent.go index 0e35151a..265f0608 100644 --- a/core/opentelemetry/agent.go +++ b/core/trace/opentelemetry/agent.go @@ -29,6 +29,7 @@ func StartAgent(c Config) { if len(c.Endpoint) == 0 { return } + // Just support jaeger now if c.Batcher != "jaeger" { return @@ -46,9 +47,7 @@ func StartAgent(c Config) { // Always be sure to batch in production. tracesdk.WithBatcher(exp), // Record information about this application in an Resource. - tracesdk.WithResource(resource.NewSchemaless( - semconv.ServiceNameKey.String(c.Name), - )), + tracesdk.WithResource(resource.NewSchemaless(semconv.ServiceNameKey.String(c.Name))), ) otel.SetTracerProvider(tp) diff --git a/core/opentelemetry/attributes.go b/core/trace/opentelemetry/attributes.go similarity index 100% rename from core/opentelemetry/attributes.go rename to core/trace/opentelemetry/attributes.go diff --git a/core/opentelemetry/clientstream.go b/core/trace/opentelemetry/clientstream.go similarity index 85% rename from core/opentelemetry/clientstream.go rename to core/trace/opentelemetry/clientstream.go index 408527a6..c6c5456d 100644 --- a/core/opentelemetry/clientstream.go +++ b/core/trace/opentelemetry/clientstream.go @@ -6,7 +6,6 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/metadata" - "google.golang.org/protobuf/proto" ) const ( @@ -14,26 +13,24 @@ const ( errorEvent ) -var _ = proto.Marshal +type ( + streamEventType int -type streamEventType int - -type streamEvent struct { - Type streamEventType - Err error -} - -type clientStream struct { - grpc.ClientStream - - desc *grpc.StreamDesc - events chan streamEvent - eventsDone chan struct{} - Finished chan error + streamEvent struct { + Type streamEventType + Err error + } - receivedMessageID int - sentMessageID int -} + clientStream struct { + grpc.ClientStream + Finished chan error + desc *grpc.StreamDesc + events chan streamEvent + eventsDone chan struct{} + receivedMessageID int + sentMessageID int + } +) func (w *clientStream) RecvMsg(m interface{}) error { err := w.ClientStream.RecvMsg(m) diff --git a/core/opentelemetry/config.go b/core/trace/opentelemetry/config.go similarity index 100% rename from core/opentelemetry/config.go rename to core/trace/opentelemetry/config.go diff --git a/core/opentelemetry/message.go b/core/trace/opentelemetry/message.go similarity index 86% rename from core/opentelemetry/message.go rename to core/trace/opentelemetry/message.go index 1199db50..f637dbfa 100644 --- a/core/opentelemetry/message.go +++ b/core/trace/opentelemetry/message.go @@ -8,6 +8,8 @@ import ( "google.golang.org/protobuf/proto" ) +const messageEvent = "message" + var ( // MessageSent is the type of sent messages. MessageSent = messageType(RPCMessageTypeSent) @@ -22,13 +24,13 @@ type messageType attribute.KeyValue func (m messageType) Event(ctx context.Context, id int, message interface{}) { span := trace.SpanFromContext(ctx) if p, ok := message.(proto.Message); ok { - span.AddEvent("message", trace.WithAttributes( + span.AddEvent(messageEvent, trace.WithAttributes( attribute.KeyValue(m), RPCMessageIDKey.Int(id), RPCMessageUncompressedSizeKey.Int(proto.Size(p)), )) } else { - span.AddEvent("message", trace.WithAttributes( + span.AddEvent(messageEvent, trace.WithAttributes( attribute.KeyValue(m), RPCMessageIDKey.Int(id), )) diff --git a/core/opentelemetry/serverstream.go b/core/trace/opentelemetry/serverstream.go similarity index 99% rename from core/opentelemetry/serverstream.go rename to core/trace/opentelemetry/serverstream.go index 8f740a5a..43a24b49 100644 --- a/core/opentelemetry/serverstream.go +++ b/core/trace/opentelemetry/serverstream.go @@ -22,7 +22,6 @@ func (w *serverStream) Context() context.Context { func (w *serverStream) RecvMsg(m interface{}) error { err := w.ServerStream.RecvMsg(m) - if err == nil { w.receivedMessageID++ MessageReceived.Event(w.Context(), w.receivedMessageID, m) @@ -33,7 +32,6 @@ func (w *serverStream) RecvMsg(m interface{}) error { func (w *serverStream) SendMsg(m interface{}) error { err := w.ServerStream.SendMsg(m) - w.sentMessageID++ MessageSent.Event(w.Context(), w.sentMessageID, m) diff --git a/core/opentelemetry/tracer.go b/core/trace/opentelemetry/tracer.go similarity index 95% rename from core/opentelemetry/tracer.go rename to core/trace/opentelemetry/tracer.go index 2328f2e9..e775c8bf 100644 --- a/core/opentelemetry/tracer.go +++ b/core/trace/opentelemetry/tracer.go @@ -9,18 +9,19 @@ import ( "google.golang.org/grpc/metadata" ) +// assert that metadataSupplier implements the TextMapCarrier interface +var _ propagation.TextMapCarrier = new(metadataSupplier) + type metadataSupplier struct { metadata *metadata.MD } -// assert that metadataSupplier implements the TextMapCarrier interface -var _ propagation.TextMapCarrier = &metadataSupplier{} - func (s *metadataSupplier) Get(key string) string { values := s.metadata.Get(key) if len(values) == 0 { return "" } + return values[0] } @@ -33,6 +34,7 @@ func (s *metadataSupplier) Keys() []string { for key := range *s.metadata { out = append(out, key) } + return out } diff --git a/core/opentelemetry/utils.go b/core/trace/opentelemetry/utils.go similarity index 95% rename from core/opentelemetry/utils.go rename to core/trace/opentelemetry/utils.go index 79f56782..e1074c13 100644 --- a/core/opentelemetry/utils.go +++ b/core/trace/opentelemetry/utils.go @@ -10,6 +10,8 @@ import ( "google.golang.org/grpc/peer" ) +const localhost = "127.0.0.1" + // PeerFromCtx returns the peer from ctx. func PeerFromCtx(ctx context.Context) string { p, ok := peer.FromContext(ctx) @@ -56,8 +58,8 @@ func PeerAttr(addr string) []attribute.KeyValue { return []attribute.KeyValue(nil) } - if host == "" { - host = "127.0.0.1" + if len(host) == 0 { + host = localhost } return []attribute.KeyValue{ diff --git a/rest/handler/otelhandler.go b/rest/handler/otelhandler.go index 3dc00341..72bbfafa 100644 --- a/rest/handler/otelhandler.go +++ b/rest/handler/otelhandler.go @@ -3,7 +3,7 @@ package handler import ( "net/http" - "github.com/tal-tech/go-zero/core/opentelemetry" + "github.com/tal-tech/go-zero/core/trace/opentelemetry" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" semconv "go.opentelemetry.io/otel/semconv/v1.4.0" diff --git a/rest/handler/otelhandler_test.go b/rest/handler/otelhandler_test.go index 30ca9b35..8812046d 100644 --- a/rest/handler/otelhandler_test.go +++ b/rest/handler/otelhandler_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/tal-tech/go-zero/core/opentelemetry" + "github.com/tal-tech/go-zero/core/trace/opentelemetry" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/trace" diff --git a/zrpc/internal/clientinterceptors/opentracinginterceptor.go b/zrpc/internal/clientinterceptors/opentracinginterceptor.go index 657b286a..0905e1e2 100644 --- a/zrpc/internal/clientinterceptors/opentracinginterceptor.go +++ b/zrpc/internal/clientinterceptors/opentracinginterceptor.go @@ -3,7 +3,7 @@ package clientinterceptors import ( "context" - "github.com/tal-tech/go-zero/core/opentelemetry" + "github.com/tal-tech/go-zero/core/trace/opentelemetry" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" diff --git a/zrpc/internal/clientinterceptors/opentracinginterceptor_test.go b/zrpc/internal/clientinterceptors/opentracinginterceptor_test.go index ed01d2f9..c883e6a7 100644 --- a/zrpc/internal/clientinterceptors/opentracinginterceptor_test.go +++ b/zrpc/internal/clientinterceptors/opentracinginterceptor_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/tal-tech/go-zero/core/opentelemetry" + "github.com/tal-tech/go-zero/core/trace/opentelemetry" "google.golang.org/grpc" ) diff --git a/zrpc/internal/serverinterceptors/opentracinginterceptor.go b/zrpc/internal/serverinterceptors/opentracinginterceptor.go index 904dd634..42342a10 100644 --- a/zrpc/internal/serverinterceptors/opentracinginterceptor.go +++ b/zrpc/internal/serverinterceptors/opentracinginterceptor.go @@ -3,7 +3,7 @@ package serverinterceptors import ( "context" - "github.com/tal-tech/go-zero/core/opentelemetry" + "github.com/tal-tech/go-zero/core/trace/opentelemetry" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/codes" diff --git a/zrpc/internal/serverinterceptors/opentracinginterceptor_test.go b/zrpc/internal/serverinterceptors/opentracinginterceptor_test.go index 3feb2a79..8e95a59e 100644 --- a/zrpc/internal/serverinterceptors/opentracinginterceptor_test.go +++ b/zrpc/internal/serverinterceptors/opentracinginterceptor_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/tal-tech/go-zero/core/opentelemetry" + "github.com/tal-tech/go-zero/core/trace/opentelemetry" "google.golang.org/grpc" )