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

Add trace ID to middleware loggers #2286

Merged
merged 2 commits into from
Nov 17, 2021
Merged
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
3 changes: 3 additions & 0 deletions changelog/unreleased/logger-traceid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Enhancement: Add trace ID to middleware loggers

https://github.com/cs3org/reva/pull/2286
23 changes: 21 additions & 2 deletions internal/grpc/interceptors/appctx/appctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@ import (
"context"

"github.com/cs3org/reva/pkg/appctx"
rtrace "github.com/cs3org/reva/pkg/trace"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
)

// NewUnary returns a new unary interceptor that creates the application context.
func NewUnary(log zerolog.Logger) grpc.UnaryServerInterceptor {
interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
ctx = appctx.WithLogger(ctx, &log)
span := trace.SpanFromContext(ctx)
defer span.End()
if !span.SpanContext().HasTraceID() {
ctx, span = rtrace.Provider.Tracer("grpc").Start(ctx, "grpc unary")
}

sub := log.With().Str("traceid", span.SpanContext().TraceID().String()).Logger()
ctx = appctx.WithLogger(ctx, &sub)
res, err := handler(ctx, req)
return res, err
}
Expand All @@ -40,7 +49,17 @@ func NewUnary(log zerolog.Logger) grpc.UnaryServerInterceptor {
// that creates the application context.
func NewStream(log zerolog.Logger) grpc.StreamServerInterceptor {
interceptor := func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
ctx := appctx.WithLogger(ss.Context(), &log)
ctx := ss.Context()
span := trace.SpanFromContext(ctx)
defer span.End()

if !span.SpanContext().HasTraceID() {
ctx, span = rtrace.Provider.Tracer("grpc").Start(ctx, "grpc stream")
}

sub := log.With().Str("traceid", span.SpanContext().TraceID().String()).Logger()
ctx = appctx.WithLogger(ctx, &sub)

wrapped := newWrappedServerStream(ctx, ss)
err := handler(srv, wrapped)
return err
Expand Down
12 changes: 11 additions & 1 deletion internal/http/interceptors/appctx/appctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"net/http"

"github.com/cs3org/reva/pkg/appctx"
rtrace "github.com/cs3org/reva/pkg/trace"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/trace"
)

// New returns a new HTTP middleware that stores the log
Expand All @@ -40,7 +42,15 @@ func New(log zerolog.Logger) func(http.Handler) http.Handler {
func handler(log zerolog.Logger, h http.Handler) http.Handler {

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := appctx.WithLogger(r.Context(), &log)
ctx := r.Context()
span := trace.SpanFromContext(ctx)
defer span.End()
if !span.SpanContext().HasTraceID() {
ctx, span = rtrace.Provider.Tracer("http").Start(ctx, "http interceptor")
}

sub := log.With().Str("traceid", span.SpanContext().TraceID().String()).Logger()
ctx = appctx.WithLogger(ctx, &sub)
r = r.WithContext(ctx)
h.ServeHTTP(w, r)
})
Expand Down
6 changes: 3 additions & 3 deletions pkg/eosclient/eosbinary/eosbinary.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"github.com/cs3org/reva/pkg/storage/utils/acl"
"github.com/google/uuid"
"github.com/pkg/errors"
"go.opencensus.io/trace"
"go.opentelemetry.io/otel/trace"
)

const (
Expand Down Expand Up @@ -249,8 +249,8 @@ func (c *Client) executeEOS(ctx context.Context, cmdArgs []string, auth eosclien

cmd.Args = append(cmd.Args, cmdArgs...)

trace := trace.FromContext(ctx).SpanContext().TraceID.String()
cmd.Args = append(cmd.Args, "--comment", trace)
span := trace.SpanFromContext(ctx)
cmd.Args = append(cmd.Args, "--comment", span.SpanContext().TraceID().String())

err := cmd.Run()

Expand Down