Skip to content

Commit 0b7093e

Browse files
authored
Merge pull request #53 from smallstep/herman/legacy-request-id-interop
Log propagated request ID and allow interoperability with legacy tracing
2 parents 7a752a5 + 7b1887f commit 0b7093e

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

grpclog/server_logger.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"google.golang.org/grpc/status"
1717

1818
"github.com/smallstep/logging"
19+
"github.com/smallstep/logging/requestid"
1920
)
2021

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

5253
var name, requestID, tracingID string
54+
55+
// Use (reflected) request ID for logging. It _could_ be empty if it wasn't set
56+
// by some (external) middleware, but we stil log the legacy request ID too, so
57+
// it shouldn't be too big of an issue.
58+
requestID = requestid.FromContext(ctx)
59+
5360
if s, ok := logging.GetName(ctx); ok {
5461
name = s
5562
} else {
5663
name = l.name
5764
}
5865
if tp, ok := logging.GetTraceparent(ctx); ok {
59-
requestID = tp.TraceID()
6066
tracingID = tp.String()
67+
if requestID == "" {
68+
requestID = tp.TraceID()
69+
}
6170
}
6271

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

121130
var name, requestID, tracingID string
131+
132+
// Use (reflected) request ID for logging. It _could_ be empty if it wasn't set
133+
// by some (external) middleware, but we stil log the legacy request ID too, so
134+
// it shouldn't be too big of an issue.
135+
requestID = requestid.FromContext(ctx)
136+
122137
if s, ok := logging.GetName(ctx); ok {
123138
name = s
124139
} else {
125140
name = l.name
126141
}
127142
if tp, ok := logging.GetTraceparent(ctx); ok {
128-
requestID = tp.TraceID()
129143
tracingID = tp.String()
144+
if requestID == "" {
145+
requestID = tp.TraceID()
146+
}
130147
}
131148

132149
fields := []zap.Field{

httplog/handler.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/smallstep/logging"
10+
"github.com/smallstep/logging/requestid"
1011
"go.uber.org/zap"
1112
)
1213

@@ -92,14 +93,22 @@ func (l *LoggerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9293
func (l *LoggerHandler) writeEntry(w ResponseLogger, r *http.Request, t time.Time, d time.Duration) {
9394
ctx := r.Context()
9495
var name, requestID, tracingID string
96+
97+
// Use (reflected) request ID for logging. It _could_ be empty if it wasn't set
98+
// by some (external) middleware, but we stil log the legacy request ID too, so
99+
// it shouldn't be too big of an issue.
100+
requestID = requestid.FromContext(ctx)
101+
95102
if s, ok := logging.GetName(ctx); ok {
96103
name = s
97104
} else {
98105
name = l.name
99106
}
100107
if tp, ok := logging.GetTraceparent(ctx); ok {
101-
requestID = tp.TraceID()
102108
tracingID = tp.String()
109+
if requestID == "" {
110+
requestID = tp.TraceID()
111+
}
103112
}
104113

105114
// Remote hostname

requestid/requestid.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package requestid
2+
3+
import "context"
4+
5+
type contextKey struct{}
6+
7+
func NewContext(ctx context.Context, requestID string) context.Context {
8+
return context.WithValue(ctx, contextKey{}, requestID)
9+
}
10+
11+
func FromContext(ctx context.Context) string {
12+
if v, ok := ctx.Value(contextKey{}).(string); ok {
13+
return v
14+
}
15+
return ""
16+
}

0 commit comments

Comments
 (0)