diff --git a/changelog/unreleased/fix-traces-logging.md b/changelog/unreleased/fix-traces-logging.md new file mode 100644 index 0000000000..3053db0f84 --- /dev/null +++ b/changelog/unreleased/fix-traces-logging.md @@ -0,0 +1,5 @@ +Bugfix: If a trace is not available do not log default trace value + +Prevent from logging `traceid=0000000000000000` if there is no traceid for the given span. + +https://github.com/cs3org/reva/pull/2352 \ No newline at end of file diff --git a/internal/grpc/interceptors/appctx/appctx.go b/internal/grpc/interceptors/appctx/appctx.go index 1bb8238c46..d47dc0c48e 100644 --- a/internal/grpc/interceptors/appctx/appctx.go +++ b/internal/grpc/interceptors/appctx/appctx.go @@ -37,7 +37,7 @@ func NewUnary(log zerolog.Logger) grpc.UnaryServerInterceptor { ctx, span = rtrace.Provider.Tracer("grpc").Start(ctx, "grpc unary") } - sub := log.With().Str("traceid", span.SpanContext().TraceID().String()).Logger() + sub := log.With().Str("traceid", getTraceIDFromSpan(span)).Logger() ctx = appctx.WithLogger(ctx, &sub) res, err := handler(ctx, req) return res, err @@ -57,7 +57,7 @@ func NewStream(log zerolog.Logger) grpc.StreamServerInterceptor { ctx, span = rtrace.Provider.Tracer("grpc").Start(ctx, "grpc stream") } - sub := log.With().Str("traceid", span.SpanContext().TraceID().String()).Logger() + sub := log.With().Str("traceid", getTraceIDFromSpan(span)).Logger() ctx = appctx.WithLogger(ctx, &sub) wrapped := newWrappedServerStream(ctx, ss) @@ -79,3 +79,10 @@ type wrappedServerStream struct { func (ss *wrappedServerStream) Context() context.Context { return ss.newCtx } + +func getTraceIDFromSpan(span trace.Span) string { + if b := span.SpanContext().TraceID(); b != [16]byte{} { + return b.String() + } + return "" +} diff --git a/internal/grpc/services/appregistry/appregistry_test.go b/internal/grpc/services/appregistry/appregistry_test.go index 4230a1ffe8..7837128aee 100644 --- a/internal/grpc/services/appregistry/appregistry_test.go +++ b/internal/grpc/services/appregistry/appregistry_test.go @@ -76,7 +76,7 @@ func Test_ListAppProviders(t *testing.T) { want: ®istrypb.ListAppProvidersResponse{ Status: &rpcv1beta1.Status{ Code: 1, - Trace: "00000000000000000000000000000000", + Trace: "", Message: "", }, Providers: []*registrypb.ProviderInfo{ @@ -98,7 +98,7 @@ func Test_ListAppProviders(t *testing.T) { want: ®istrypb.ListAppProvidersResponse{ Status: &rpcv1beta1.Status{ Code: 1, - Trace: "00000000000000000000000000000000", + Trace: "", }, Providers: []*registrypb.ProviderInfo{}, }, @@ -112,7 +112,7 @@ func Test_ListAppProviders(t *testing.T) { want: ®istrypb.ListAppProvidersResponse{ Status: &rpcv1beta1.Status{ Code: 1, - Trace: "00000000000000000000000000000000", + Trace: "", Message: "", }, Providers: []*registrypb.ProviderInfo{}, @@ -218,7 +218,7 @@ func Test_GetAppProviders(t *testing.T) { want: ®istrypb.GetAppProvidersResponse{ Status: &rpcv1beta1.Status{ Code: 1, - Trace: "00000000000000000000000000000000", + Trace: "", Message: "", }, Providers: []*registrypb.ProviderInfo{ @@ -235,7 +235,7 @@ func Test_GetAppProviders(t *testing.T) { want: ®istrypb.GetAppProvidersResponse{ Status: &rpcv1beta1.Status{ Code: 1, - Trace: "00000000000000000000000000000000", + Trace: "", Message: "", }, Providers: []*registrypb.ProviderInfo{ @@ -252,7 +252,7 @@ func Test_GetAppProviders(t *testing.T) { want: ®istrypb.GetAppProvidersResponse{ Status: &rpcv1beta1.Status{ Code: 15, - Trace: "00000000000000000000000000000000", + Trace: "", Message: "error looking for the app provider", }, Providers: nil, @@ -264,7 +264,7 @@ func Test_GetAppProviders(t *testing.T) { want: ®istrypb.GetAppProvidersResponse{ Status: &rpcv1beta1.Status{ Code: 15, - Trace: "00000000000000000000000000000000", + Trace: "", Message: "error looking for the app provider", }, Providers: nil, @@ -276,7 +276,7 @@ func Test_GetAppProviders(t *testing.T) { want: ®istrypb.GetAppProvidersResponse{ Status: &rpcv1beta1.Status{ Code: 15, - Trace: "00000000000000000000000000000000", + Trace: "", Message: "error looking for the app provider", }, Providers: nil, @@ -288,7 +288,7 @@ func Test_GetAppProviders(t *testing.T) { want: ®istrypb.GetAppProvidersResponse{ Status: &rpcv1beta1.Status{ Code: 15, - Trace: "00000000000000000000000000000000", + Trace: "", Message: "error looking for the app provider", }, Providers: nil, diff --git a/internal/http/interceptors/appctx/appctx.go b/internal/http/interceptors/appctx/appctx.go index 9d34a78981..5b089e3c9a 100644 --- a/internal/http/interceptors/appctx/appctx.go +++ b/internal/http/interceptors/appctx/appctx.go @@ -49,9 +49,17 @@ func handler(log zerolog.Logger, h http.Handler) http.Handler { ctx, span = rtrace.Provider.Tracer("http").Start(ctx, "http interceptor") } - sub := log.With().Str("traceid", span.SpanContext().TraceID().String()).Logger() + sub := log.With().Str("traceid", getTraceIDFromSpan(span)).Logger() ctx = appctx.WithLogger(ctx, &sub) r = r.WithContext(ctx) h.ServeHTTP(w, r) }) } + +func getTraceIDFromSpan(span trace.Span) string { + if b := span.SpanContext().TraceID(); b != [16]byte{} { + return b.String() + } + + return "" +} diff --git a/pkg/rgrpc/status/status.go b/pkg/rgrpc/status/status.go index 8881ba984c..9fed7f06ac 100644 --- a/pkg/rgrpc/status/status.go +++ b/pkg/rgrpc/status/status.go @@ -204,5 +204,9 @@ func NewErrorFromCode(code rpc.Code, pkgname string) error { // internal function to attach the trace to a context func getTrace(ctx context.Context) string { span := trace.SpanFromContext(ctx) - return span.SpanContext().TraceID().String() + if b := span.SpanContext().TraceID(); b != [16]byte{} { + return b.String() + } + + return "" }