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
master
Kevin Wan 3 years ago committed by GitHub
parent bc92b57bdb
commit bdec5f2349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,7 +7,6 @@ import (
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/prometheus" "github.com/tal-tech/go-zero/core/prometheus"
"github.com/tal-tech/go-zero/core/stat" "github.com/tal-tech/go-zero/core/stat"
"github.com/tal-tech/go-zero/core/trace/opentelemetry"
) )
const ( const (
@ -30,7 +29,8 @@ type ServiceConf struct {
Mode string `json:",default=pro,options=dev|test|rt|pre|pro"` Mode string `json:",default=pro,options=dev|test|rt|pre|pro"`
MetricsUrl string `json:",optional"` MetricsUrl string `json:",optional"`
Prometheus prometheus.Config `json:",optional"` Prometheus prometheus.Config `json:",optional"`
Telemetry opentelemetry.Config `json:",optional"` // TODO: enable it in v1.2.1
// Telemetry opentelemetry.Config `json:",optional"`
} }
// MustSetUp sets up the service, exits on error. // MustSetUp sets up the service, exits on error.
@ -45,16 +45,18 @@ func (sc ServiceConf) SetUp() error {
if len(sc.Log.ServiceName) == 0 { if len(sc.Log.ServiceName) == 0 {
sc.Log.ServiceName = sc.Name sc.Log.ServiceName = sc.Name
} }
if len(sc.Telemetry.Name) == 0 {
sc.Telemetry.Name = sc.Name
}
if err := logx.SetUp(sc.Log); err != nil { if err := logx.SetUp(sc.Log); err != nil {
return err return err
} }
sc.initMode() sc.initMode()
prometheus.StartAgent(sc.Prometheus) 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 { if len(sc.MetricsUrl) > 0 {
stat.SetReportWriter(stat.NewRemoteWriter(sc.MetricsUrl)) stat.SetReportWriter(stat.NewRemoteWriter(sc.MetricsUrl))

@ -9,7 +9,7 @@ import (
"go.opentelemetry.io/otel/exporters/jaeger" "go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource" "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" semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
) )
@ -41,13 +41,13 @@ func StartAgent(c Config) {
return return
} }
tp := tracesdk.NewTracerProvider( tp := sdktrace.NewTracerProvider(
// Set the sampling rate based on the parent span to 100% // 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. // Always be sure to batch in production.
tracesdk.WithBatcher(exp), sdktrace.WithBatcher(exp),
// Record information about this application in an Resource. // 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) otel.SetTracerProvider(tp)

@ -5,7 +5,7 @@ import (
"go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace" sdktrace "go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/metadata" "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. // 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{ ctx = p.Extract(ctx, &metadataSupplier{
metadata: metadata, metadata: metadata,
}) })
return baggage.FromContext(ctx), trace.SpanContextFromContext(ctx) return baggage.FromContext(ctx), sdktrace.SpanContextFromContext(ctx)
} }

@ -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.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 h1:SHhxSjB+omnGZPgGlKe+QMp3MyazcOHdQ8qwo89oKbg=
go.opentelemetry.io/otel v1.0.0-RC2/go.mod h1:w1thVQ7qbAy8MHb0IFj8a5Q2QU0l2ksf8u/CN8m3NOM= 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 h1:RF0nWsIDpDBe+s06lkLxUw9CWQUAhO6hBSxxB7dz45s=
go.opentelemetry.io/otel/exporters/jaeger v1.0.0-RC2/go.mod h1:sZZqN3Vb0iT+NE6mZ1S7sNyH3t4PFk6ElK5TLGFBZ7E= 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 h1:ROuteeSCBaZNjiT9JcFzZepmInDvLktR28Y6qKo8bCs=
go.opentelemetry.io/otel/sdk v1.0.0-RC2/go.mod h1:fgwHyiDn4e5k40TD9VX243rOxXR+jzsWBZYA2P5jpEw= 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 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-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.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 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=

@ -13,7 +13,9 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/vars" "github.com/tal-tech/go-zero/tools/goctl/vars"
) )
const handlerTemplate = `package {{.PkgName}} const (
defaultLogicPackage = "logic"
handlerTemplate = `package {{.PkgName}}
import ( import (
"net/http" "net/http"
@ -40,6 +42,7 @@ func {{.HandlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {
} }
} }
` `
)
type handlerInfo struct { type handlerInfo struct {
PkgName string PkgName string
@ -58,7 +61,7 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route
handler := getHandlerName(route) handler := getHandlerName(route)
handlerPath := getHandlerFolderPath(group, route) handlerPath := getHandlerFolderPath(group, route)
pkgName := handlerPath[strings.LastIndex(handlerPath, "/")+1:] pkgName := handlerPath[strings.LastIndex(handlerPath, "/")+1:]
logicName := "logic" logicName := defaultLogicPackage
if handlerPath != handlerDir { if handlerPath != handlerDir {
handler = strings.Title(handler) handler = strings.Title(handler)
logicName = pkgName logicName = pkgName
@ -150,6 +153,7 @@ func getHandlerFolderPath(group spec.Group, route spec.Route) string {
return handlerDir return handlerDir
} }
} }
folder = strings.TrimPrefix(folder, "/") folder = strings.TrimPrefix(folder, "/")
folder = strings.TrimSuffix(folder, "/") folder = strings.TrimSuffix(folder, "/")
return path.Join(handlerDir, folder) return path.Join(handlerDir, folder)

@ -59,13 +59,11 @@ func (s *rpcServer) Start(register RegisterFn) error {
serverinterceptors.UnaryStatInterceptor(s.metrics), serverinterceptors.UnaryStatInterceptor(s.metrics),
serverinterceptors.UnaryPrometheusInterceptor(), serverinterceptors.UnaryPrometheusInterceptor(),
serverinterceptors.UnaryBreakerInterceptor(), serverinterceptors.UnaryBreakerInterceptor(),
serverinterceptors.UnaryOpenTracingInterceptor(),
} }
unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...) unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...)
streamInterceptors := []grpc.StreamServerInterceptor{ streamInterceptors := []grpc.StreamServerInterceptor{
serverinterceptors.StreamCrashInterceptor, serverinterceptors.StreamCrashInterceptor,
serverinterceptors.StreamBreakerInterceptor, serverinterceptors.StreamBreakerInterceptor,
serverinterceptors.StreamOpenTracingInterceptor(),
} }
streamInterceptors = append(streamInterceptors, s.streamInterceptors...) streamInterceptors = append(streamInterceptors, s.streamInterceptors...)
options := append(s.options, WithUnaryServerInterceptors(unaryInterceptors...), options := append(s.options, WithUnaryServerInterceptors(unaryInterceptors...),

Loading…
Cancel
Save