43 lines
770 B
Go
43 lines
770 B
Go
package rctx
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// ContextString
|
|
// Assign context implementation was stolen
|
|
// here: https://medium.com/@funfoolsuzi/logging-with-request-id-in-go-microservice-21485c6730da
|
|
type ContextString string
|
|
|
|
const (
|
|
ContextRequestIDKey ContextString = "requestID"
|
|
)
|
|
|
|
// AssignRequestID
|
|
//
|
|
// Assigns request id to the context.Context variable
|
|
func AssignRequestID(ctx context.Context) context.Context {
|
|
|
|
reqID := uuid.New()
|
|
|
|
return context.WithValue(ctx, ContextRequestIDKey, reqID.String())
|
|
|
|
}
|
|
|
|
// GetRequestID
|
|
//
|
|
// Gets request id from the context.Context variable
|
|
func GetRequestID(ctx context.Context) string {
|
|
|
|
reqID := ctx.Value(ContextRequestIDKey)
|
|
|
|
if ret, ok := reqID.(string); ok {
|
|
return ret
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|