fix: opentelemetry traceid not correct (#1108)
This commit is contained in:
@@ -96,7 +96,10 @@ func WithContext(ctx context.Context) Logger {
|
|||||||
func spanIdFromContext(ctx context.Context) string {
|
func spanIdFromContext(ctx context.Context) string {
|
||||||
span := trace.SpanFromContext(ctx)
|
span := trace.SpanFromContext(ctx)
|
||||||
if span.IsRecording() {
|
if span.IsRecording() {
|
||||||
return span.SpanContext().SpanID().String()
|
spanCtx := span.SpanContext()
|
||||||
|
if spanCtx.IsValid() {
|
||||||
|
return spanCtx.SpanID().String()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace)
|
t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace)
|
||||||
@@ -110,7 +113,10 @@ func spanIdFromContext(ctx context.Context) string {
|
|||||||
func traceIdFromContext(ctx context.Context) string {
|
func traceIdFromContext(ctx context.Context) string {
|
||||||
span := trace.SpanFromContext(ctx)
|
span := trace.SpanFromContext(ctx)
|
||||||
if span.IsRecording() {
|
if span.IsRecording() {
|
||||||
return span.SpanContext().SpanID().String()
|
spanCtx := span.SpanContext()
|
||||||
|
if spanCtx.IsValid() {
|
||||||
|
return span.SpanContext().TraceID().String()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace)
|
t, ok := ctx.Value(tracespec.TracingKey).(tracespec.Trace)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ 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 (
|
||||||
@@ -26,11 +27,10 @@ const (
|
|||||||
type ServiceConf struct {
|
type ServiceConf struct {
|
||||||
Name string
|
Name string
|
||||||
Log logx.LogConf
|
Log logx.LogConf
|
||||||
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"`
|
||||||
// TODO: enable it in v1.2.2
|
Telemetry opentelemetry.Config `json:",optional"`
|
||||||
// Telemetry opentelemetry.Config `json:",optional"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustSetUp sets up the service, exits on error.
|
// MustSetUp sets up the service, exits on error.
|
||||||
@@ -52,11 +52,10 @@ func (sc ServiceConf) SetUp() error {
|
|||||||
sc.initMode()
|
sc.initMode()
|
||||||
prometheus.StartAgent(sc.Prometheus)
|
prometheus.StartAgent(sc.Prometheus)
|
||||||
|
|
||||||
// TODO: enable it in v1.2.2
|
if len(sc.Telemetry.Name) == 0 {
|
||||||
// if len(sc.Telemetry.Name) == 0 {
|
sc.Telemetry.Name = sc.Name
|
||||||
// sc.Telemetry.Name = sc.Name
|
}
|
||||||
// }
|
opentelemetry.StartAgent(sc.Telemetry)
|
||||||
// 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))
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
// serverStream wraps around the embedded grpc.ServerStream, and intercepts the RecvMsg and
|
// serverStream wraps around the embedded grpc.ServerStream,
|
||||||
// SendMsg method call.
|
// and intercepts the RecvMsg and SendMsg method call.
|
||||||
type serverStream struct {
|
type serverStream struct {
|
||||||
grpc.ServerStream
|
grpc.ServerStream
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ func (s *engine) bindRoute(fr featuredRoutes, router httpx.Router, metrics *stat
|
|||||||
route Route, verifier func(chain alice.Chain) alice.Chain) error {
|
route Route, verifier func(chain alice.Chain) alice.Chain) error {
|
||||||
chain := alice.New(
|
chain := alice.New(
|
||||||
handler.TracingHandler,
|
handler.TracingHandler,
|
||||||
handler.OtelHandler(route.Path),
|
handler.OtelHandler(s.conf.Name, route.Path),
|
||||||
s.getLogHandler(),
|
s.getLogHandler(),
|
||||||
handler.PrometheusHandler(route.Path),
|
handler.PrometheusHandler(route.Path),
|
||||||
handler.MaxConns(s.conf.MaxConns),
|
handler.MaxConns(s.conf.MaxConns),
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// OtelHandler return a middleware that process the opentelemetry.
|
// OtelHandler return a middleware that process the opentelemetry.
|
||||||
func OtelHandler(path string) func(http.Handler) http.Handler {
|
func OtelHandler(serviceName, path string) func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
if !opentelemetry.Enabled() {
|
if !opentelemetry.Enabled() {
|
||||||
return next
|
return next
|
||||||
@@ -26,7 +26,8 @@ func OtelHandler(path string) func(http.Handler) http.Handler {
|
|||||||
ctx,
|
ctx,
|
||||||
path,
|
path,
|
||||||
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
|
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
|
||||||
oteltrace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest("", path, r)...),
|
oteltrace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest(
|
||||||
|
serviceName, path, r)...),
|
||||||
)
|
)
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
|
|||||||
@@ -21,9 +21,9 @@ import (
|
|||||||
{{.imports}}
|
{{.imports}}
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/conf"
|
"github.com/tal-tech/go-zero/core/conf"
|
||||||
|
"github.com/tal-tech/go-zero/core/service"
|
||||||
"github.com/tal-tech/go-zero/zrpc"
|
"github.com/tal-tech/go-zero/zrpc"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"github.com/tal-tech/go-zero/core/service"
|
|
||||||
"google.golang.org/grpc/reflection"
|
"google.golang.org/grpc/reflection"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user