Skip to content

Commit

Permalink
Merge pull request #53 from smallstep/herman/legacy-request-id-interop
Browse files Browse the repository at this point in the history
Log propagated request ID and allow interoperability with legacy tracing
  • Loading branch information
hslatman authored Mar 5, 2024
2 parents 7a752a5 + 7b1887f commit 0b7093e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
21 changes: 19 additions & 2 deletions grpclog/server_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"google.golang.org/grpc/status"

"github.com/smallstep/logging"
"github.com/smallstep/logging/requestid"
)

type serverLogger struct {
Expand Down Expand Up @@ -50,14 +51,22 @@ func (l *serverLogger) Log(ctx context.Context, fullMethod string, t time.Time,
}

var name, requestID, tracingID string

// Use (reflected) request ID for logging. It _could_ be empty if it wasn't set
// by some (external) middleware, but we stil log the legacy request ID too, so
// it shouldn't be too big of an issue.
requestID = requestid.FromContext(ctx)

if s, ok := logging.GetName(ctx); ok {
name = s
} else {
name = l.name
}
if tp, ok := logging.GetTraceparent(ctx); ok {
requestID = tp.TraceID()
tracingID = tp.String()
if requestID == "" {
requestID = tp.TraceID()
}
}

fields := []zap.Field{
Expand Down Expand Up @@ -119,14 +128,22 @@ func (l *serverLogger) LogStream(ctx context.Context, fullMethod, msg string, ex
}

var name, requestID, tracingID string

// Use (reflected) request ID for logging. It _could_ be empty if it wasn't set
// by some (external) middleware, but we stil log the legacy request ID too, so
// it shouldn't be too big of an issue.
requestID = requestid.FromContext(ctx)

if s, ok := logging.GetName(ctx); ok {
name = s
} else {
name = l.name
}
if tp, ok := logging.GetTraceparent(ctx); ok {
requestID = tp.TraceID()
tracingID = tp.String()
if requestID == "" {
requestID = tp.TraceID()
}
}

fields := []zap.Field{
Expand Down
11 changes: 10 additions & 1 deletion httplog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/smallstep/logging"
"github.com/smallstep/logging/requestid"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -92,14 +93,22 @@ func (l *LoggerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (l *LoggerHandler) writeEntry(w ResponseLogger, r *http.Request, t time.Time, d time.Duration) {
ctx := r.Context()
var name, requestID, tracingID string

// Use (reflected) request ID for logging. It _could_ be empty if it wasn't set
// by some (external) middleware, but we stil log the legacy request ID too, so
// it shouldn't be too big of an issue.
requestID = requestid.FromContext(ctx)

if s, ok := logging.GetName(ctx); ok {
name = s
} else {
name = l.name
}
if tp, ok := logging.GetTraceparent(ctx); ok {
requestID = tp.TraceID()
tracingID = tp.String()
if requestID == "" {
requestID = tp.TraceID()
}
}

// Remote hostname
Expand Down
16 changes: 16 additions & 0 deletions requestid/requestid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package requestid

import "context"

type contextKey struct{}

func NewContext(ctx context.Context, requestID string) context.Context {
return context.WithValue(ctx, contextKey{}, requestID)
}

func FromContext(ctx context.Context) string {
if v, ok := ctx.Value(contextKey{}).(string); ok {
return v
}
return ""
}

0 comments on commit 0b7093e

Please sign in to comment.