diff --git a/request.go b/request.go index 3344661..d0b6824 100644 --- a/request.go +++ b/request.go @@ -1,6 +1,7 @@ package elemental import ( + "context" "crypto/tls" "encoding/base64" "encoding/json" @@ -51,17 +52,25 @@ type Request struct { span opentracing.Span wireContext opentracing.SpanContext + context context.Context } // NewRequest returns a new Request. func NewRequest() *Request { + return NewRequestWithContext(context.Background()) +} + +// NewRequestWithContext returns a new Request with the given context.Context. +func NewRequestWithContext(ctx context.Context) *Request { + return &Request{ RequestID: uuid.NewV4().String(), Parameters: url.Values{}, Headers: http.Header{}, Metadata: map[string]interface{}{}, TrackingData: opentracing.TextMapCarrier{}, + context: ctx, } } @@ -214,6 +223,7 @@ func NewRequestFromHTTPRequest(req *http.Request) (*Request, error) { wireContext: wireContext, Order: req.URL.Query()["order"], ClientIP: req.RemoteAddr, + context: req.Context(), }, nil } @@ -330,6 +340,7 @@ func (r *Request) Duplicate() *Request { req.ExternalTrackingType = r.ExternalTrackingType req.ClientIP = r.ClientIP req.Order = append([]string{}, r.Order...) + req.context = r.context for k, v := range r.Headers { req.Headers[k] = v @@ -369,6 +380,11 @@ func (r *Request) Decode(dst interface{}) error { return UnmarshalJSON(r.Data, &dst) } +// Context returns the request context.Context. +func (r *Request) Context() context.Context { + return r.context +} + func (r *Request) String() string { return fmt.Sprintf("",