Skip to content

Commit

Permalink
add request id to grpc logging fields
Browse files Browse the repository at this point in the history
Signed-off-by: Coleen Iona Quadros <coleen.quadros27@gmail.com>
  • Loading branch information
coleenquadros committed Oct 18, 2023
1 parent 6cf226c commit ae8e6ed
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/logging/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func NewGRPCOption(configYAML []byte) ([]grpc_logging.Option, []string, error) {
logOpts = []grpc_logging.Option{
grpc_logging.WithLevels(DefaultCodeToLevelGRPC),
grpc_logging.WithFieldsFromContext(GetTraceIDAsField),
grpc_logging.WithFieldsFromContext(GetRequestIDAsField),
}
// If the combination is valid, use them, otherwise return error.
reqLogDecision, err := getGRPCLoggingOption(globalStart, globalEnd)
Expand Down
19 changes: 19 additions & 0 deletions pkg/logging/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ package logging
import (
"context"
"fmt"
"math/rand"
"time"

"github.com/oklog/ulid"
"github.com/opentracing/opentracing-go"
"go.opentelemetry.io/otel/trace"

extflag "github.com/efficientgo/tools/extkingpin"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
grpc_logging "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
middleware "github.com/thanos-io/thanos/pkg/server/http/middleware"
"github.com/thanos-io/thanos/pkg/tracing/migration"
"google.golang.org/grpc/codes"
)
Expand Down Expand Up @@ -169,6 +172,22 @@ func GetTraceIDAsField(ctx context.Context) grpc_logging.Fields {
return grpc_logging.Fields{"traceID", TraceID}
}
return nil

}

func GetRequestIDAsField(ctx context.Context) grpc_logging.Fields {
reqID, ok := middleware.RequestIDFromContext(ctx)
if ok {
return grpc_logging.Fields{"requestID", reqID}
}

entropy := ulid.Monotonic(rand.New(rand.NewSource(time.Now().UnixNano())), 0)
newReqID := ulid.MustNew(ulid.Timestamp(time.Now()), entropy).String()

// Insert into context (Note: This updated context isn't being used later, so this might be redundant)
ctx = middleware.NewContextWithRequestID(ctx, newReqID)

return grpc_logging.Fields{"requestID", newReqID}
}

// TODO: @yashrsharma44 - To be deprecated in the next release.
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/http/middleware/request_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ctxKey int
const reqIDKey = ctxKey(0)

// newContextWithRequestID creates a context with a request id.
func newContextWithRequestID(ctx context.Context, rid string) context.Context {
func NewContextWithRequestID(ctx context.Context, rid string) context.Context {
return context.WithValue(ctx, reqIDKey, rid)
}

Expand All @@ -36,7 +36,7 @@ func RequestID(h http.Handler) http.HandlerFunc {
reqID := ulid.MustNew(ulid.Timestamp(time.Now()), entropy)
r.Header.Set("X-Request-ID", reqID.String())
}
ctx := newContextWithRequestID(r.Context(), reqID)
ctx := NewContextWithRequestID(r.Context(), reqID)
h.ServeHTTP(w, r.WithContext(ctx))
}
}

0 comments on commit ae8e6ed

Please sign in to comment.