Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log traces only when these are available #2352

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/fix-traces-logging.md
Original file line number Diff line number Diff line change
@@ -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
11 changes: 9 additions & 2 deletions internal/grpc/interceptors/appctx/appctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 ""
}
18 changes: 9 additions & 9 deletions internal/grpc/services/appregistry/appregistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Test_ListAppProviders(t *testing.T) {
want: &registrypb.ListAppProvidersResponse{
Status: &rpcv1beta1.Status{
Code: 1,
Trace: "00000000000000000000000000000000",
Trace: "",
Message: "",
},
Providers: []*registrypb.ProviderInfo{
Expand All @@ -98,7 +98,7 @@ func Test_ListAppProviders(t *testing.T) {
want: &registrypb.ListAppProvidersResponse{
Status: &rpcv1beta1.Status{
Code: 1,
Trace: "00000000000000000000000000000000",
Trace: "",
},
Providers: []*registrypb.ProviderInfo{},
},
Expand All @@ -112,7 +112,7 @@ func Test_ListAppProviders(t *testing.T) {
want: &registrypb.ListAppProvidersResponse{
Status: &rpcv1beta1.Status{
Code: 1,
Trace: "00000000000000000000000000000000",
Trace: "",
Message: "",
},
Providers: []*registrypb.ProviderInfo{},
Expand Down Expand Up @@ -218,7 +218,7 @@ func Test_GetAppProviders(t *testing.T) {
want: &registrypb.GetAppProvidersResponse{
Status: &rpcv1beta1.Status{
Code: 1,
Trace: "00000000000000000000000000000000",
Trace: "",
Message: "",
},
Providers: []*registrypb.ProviderInfo{
Expand All @@ -235,7 +235,7 @@ func Test_GetAppProviders(t *testing.T) {
want: &registrypb.GetAppProvidersResponse{
Status: &rpcv1beta1.Status{
Code: 1,
Trace: "00000000000000000000000000000000",
Trace: "",
Message: "",
},
Providers: []*registrypb.ProviderInfo{
Expand All @@ -252,7 +252,7 @@ func Test_GetAppProviders(t *testing.T) {
want: &registrypb.GetAppProvidersResponse{
Status: &rpcv1beta1.Status{
Code: 15,
Trace: "00000000000000000000000000000000",
Trace: "",
Message: "error looking for the app provider",
},
Providers: nil,
Expand All @@ -264,7 +264,7 @@ func Test_GetAppProviders(t *testing.T) {
want: &registrypb.GetAppProvidersResponse{
Status: &rpcv1beta1.Status{
Code: 15,
Trace: "00000000000000000000000000000000",
Trace: "",
Message: "error looking for the app provider",
},
Providers: nil,
Expand All @@ -276,7 +276,7 @@ func Test_GetAppProviders(t *testing.T) {
want: &registrypb.GetAppProvidersResponse{
Status: &rpcv1beta1.Status{
Code: 15,
Trace: "00000000000000000000000000000000",
Trace: "",
Message: "error looking for the app provider",
},
Providers: nil,
Expand All @@ -288,7 +288,7 @@ func Test_GetAppProviders(t *testing.T) {
want: &registrypb.GetAppProvidersResponse{
Status: &rpcv1beta1.Status{
Code: 15,
Trace: "00000000000000000000000000000000",
Trace: "",
Message: "error looking for the app provider",
},
Providers: nil,
Expand Down
10 changes: 9 additions & 1 deletion internal/http/interceptors/appctx/appctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
6 changes: 5 additions & 1 deletion pkg/rgrpc/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}