From bdec5f2349c36d5e1b1d0891665397dd881638c2 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Tue, 7 Sep 2021 12:02:41 +0800 Subject: [PATCH] use sdktrace instead of trace for opentelemetry to avoid conflicts (#1005) * use sdktrace instead of trace for opentelemetry to avoid conflicts * disable opentelemetry for right now --- core/service/serviceconf.go | 20 +++++++++++--------- core/trace/opentelemetry/agent.go | 10 +++++----- core/trace/opentelemetry/tracer.go | 7 ++++--- go.sum | 4 ++++ tools/goctl/api/gogen/genhandlers.go | 8 ++++++-- zrpc/internal/rpcserver.go | 2 -- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/core/service/serviceconf.go b/core/service/serviceconf.go index d2f53e77..2421ccfa 100644 --- a/core/service/serviceconf.go +++ b/core/service/serviceconf.go @@ -7,7 +7,6 @@ import ( "github.com/tal-tech/go-zero/core/logx" "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 ( @@ -27,10 +26,11 @@ const ( type ServiceConf struct { Name string Log logx.LogConf - Mode string `json:",default=pro,options=dev|test|rt|pre|pro"` - MetricsUrl string `json:",optional"` - Prometheus prometheus.Config `json:",optional"` - Telemetry opentelemetry.Config `json:",optional"` + Mode string `json:",default=pro,options=dev|test|rt|pre|pro"` + MetricsUrl string `json:",optional"` + Prometheus prometheus.Config `json:",optional"` + // TODO: enable it in v1.2.1 + // Telemetry opentelemetry.Config `json:",optional"` } // MustSetUp sets up the service, exits on error. @@ -45,16 +45,18 @@ func (sc ServiceConf) SetUp() error { if len(sc.Log.ServiceName) == 0 { sc.Log.ServiceName = sc.Name } - if len(sc.Telemetry.Name) == 0 { - sc.Telemetry.Name = sc.Name - } if err := logx.SetUp(sc.Log); err != nil { return err } sc.initMode() prometheus.StartAgent(sc.Prometheus) - opentelemetry.StartAgent(sc.Telemetry) + + // TODO: enable it in v1.2.1 + // if len(sc.Telemetry.Name) == 0 { + // sc.Telemetry.Name = sc.Name + // } + // opentelemetry.StartAgent(sc.Telemetry) if len(sc.MetricsUrl) > 0 { stat.SetReportWriter(stat.NewRemoteWriter(sc.MetricsUrl)) diff --git a/core/trace/opentelemetry/agent.go b/core/trace/opentelemetry/agent.go index 2e379f49..fdb70b77 100644 --- a/core/trace/opentelemetry/agent.go +++ b/core/trace/opentelemetry/agent.go @@ -9,7 +9,7 @@ import ( "go.opentelemetry.io/otel/exporters/jaeger" "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/sdk/resource" - tracesdk "go.opentelemetry.io/otel/sdk/trace" + sdktrace "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.4.0" ) @@ -41,13 +41,13 @@ func StartAgent(c Config) { return } - tp := tracesdk.NewTracerProvider( + tp := sdktrace.NewTracerProvider( // Set the sampling rate based on the parent span to 100% - tracesdk.WithSampler(tracesdk.ParentBased(tracesdk.TraceIDRatioBased(c.Sampler))), + sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(c.Sampler))), // Always be sure to batch in production. - tracesdk.WithBatcher(exp), + sdktrace.WithBatcher(exp), // Record information about this application in an Resource. - tracesdk.WithResource(resource.NewSchemaless(semconv.ServiceNameKey.String(c.Name))), + sdktrace.WithResource(resource.NewSchemaless(semconv.ServiceNameKey.String(c.Name))), ) otel.SetTracerProvider(tp) diff --git a/core/trace/opentelemetry/tracer.go b/core/trace/opentelemetry/tracer.go index e775c8bf..f70021e0 100644 --- a/core/trace/opentelemetry/tracer.go +++ b/core/trace/opentelemetry/tracer.go @@ -5,7 +5,7 @@ import ( "go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/trace" + sdktrace "go.opentelemetry.io/otel/trace" "google.golang.org/grpc/metadata" ) @@ -46,10 +46,11 @@ func Inject(ctx context.Context, p propagation.TextMapPropagator, metadata *meta } // Extract extracts the metadata from ctx. -func Extract(ctx context.Context, p propagation.TextMapPropagator, metadata *metadata.MD) (baggage.Baggage, trace.SpanContext) { +func Extract(ctx context.Context, p propagation.TextMapPropagator, metadata *metadata.MD) ( + baggage.Baggage, sdktrace.SpanContext) { ctx = p.Extract(ctx, &metadataSupplier{ metadata: metadata, }) - return baggage.FromContext(ctx), trace.SpanContextFromContext(ctx) + return baggage.FromContext(ctx), sdktrace.SpanContextFromContext(ctx) } diff --git a/go.sum b/go.sum index 0e02d9e4..2d04b236 100644 --- a/go.sum +++ b/go.sum @@ -358,12 +358,16 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opentelemetry.io/otel v1.0.0-RC2 h1:SHhxSjB+omnGZPgGlKe+QMp3MyazcOHdQ8qwo89oKbg= go.opentelemetry.io/otel v1.0.0-RC2/go.mod h1:w1thVQ7qbAy8MHb0IFj8a5Q2QU0l2ksf8u/CN8m3NOM= +go.opentelemetry.io/otel v1.0.0-RC3 h1:kvwiyEkiUT/JaadXzVLI/R1wDO934A7r3Bs2wEe6wqA= +go.opentelemetry.io/otel v1.0.0-RC3/go.mod h1:Ka5j3ua8tZs4Rkq4Ex3hwgBgOchyPVq5S6P2lz//nKQ= go.opentelemetry.io/otel/exporters/jaeger v1.0.0-RC2 h1:RF0nWsIDpDBe+s06lkLxUw9CWQUAhO6hBSxxB7dz45s= go.opentelemetry.io/otel/exporters/jaeger v1.0.0-RC2/go.mod h1:sZZqN3Vb0iT+NE6mZ1S7sNyH3t4PFk6ElK5TLGFBZ7E= go.opentelemetry.io/otel/sdk v1.0.0-RC2 h1:ROuteeSCBaZNjiT9JcFzZepmInDvLktR28Y6qKo8bCs= go.opentelemetry.io/otel/sdk v1.0.0-RC2/go.mod h1:fgwHyiDn4e5k40TD9VX243rOxXR+jzsWBZYA2P5jpEw= go.opentelemetry.io/otel/trace v1.0.0-RC2 h1:dunAP0qDULMIT82atj34m5RgvsIK6LcsXf1c/MsYg1w= go.opentelemetry.io/otel/trace v1.0.0-RC2/go.mod h1:JPQ+z6nNw9mqEGT8o3eoPTdnNI+Aj5JcxEsVGREIAy4= +go.opentelemetry.io/otel/trace v1.0.0-RC3 h1:9F0ayEvlxv8BmNmPbU005WK7hC+7KbOazCPZjNa1yME= +go.opentelemetry.io/otel/trace v1.0.0-RC3/go.mod h1:VUt2TUYd8S2/ZRX09ZDFZQwn2RqfMB5MzO17jBojGxo= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index e0f715f3..9248a3b4 100644 --- a/tools/goctl/api/gogen/genhandlers.go +++ b/tools/goctl/api/gogen/genhandlers.go @@ -13,7 +13,9 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/vars" ) -const handlerTemplate = `package {{.PkgName}} +const ( + defaultLogicPackage = "logic" + handlerTemplate = `package {{.PkgName}} import ( "net/http" @@ -40,6 +42,7 @@ func {{.HandlerName}}(ctx *svc.ServiceContext) http.HandlerFunc { } } ` +) type handlerInfo struct { PkgName string @@ -58,7 +61,7 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route handler := getHandlerName(route) handlerPath := getHandlerFolderPath(group, route) pkgName := handlerPath[strings.LastIndex(handlerPath, "/")+1:] - logicName := "logic" + logicName := defaultLogicPackage if handlerPath != handlerDir { handler = strings.Title(handler) logicName = pkgName @@ -150,6 +153,7 @@ func getHandlerFolderPath(group spec.Group, route spec.Route) string { return handlerDir } } + folder = strings.TrimPrefix(folder, "/") folder = strings.TrimSuffix(folder, "/") return path.Join(handlerDir, folder) diff --git a/zrpc/internal/rpcserver.go b/zrpc/internal/rpcserver.go index 42d4defd..3b5c9a3e 100644 --- a/zrpc/internal/rpcserver.go +++ b/zrpc/internal/rpcserver.go @@ -59,13 +59,11 @@ func (s *rpcServer) Start(register RegisterFn) error { serverinterceptors.UnaryStatInterceptor(s.metrics), serverinterceptors.UnaryPrometheusInterceptor(), serverinterceptors.UnaryBreakerInterceptor(), - serverinterceptors.UnaryOpenTracingInterceptor(), } unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...) streamInterceptors := []grpc.StreamServerInterceptor{ serverinterceptors.StreamCrashInterceptor, serverinterceptors.StreamBreakerInterceptor, - serverinterceptors.StreamOpenTracingInterceptor(), } streamInterceptors = append(streamInterceptors, s.streamInterceptors...) options := append(s.options, WithUnaryServerInterceptors(unaryInterceptors...),