Add utilities for handling the traceparent header (see Trace Context W3C standard).
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "0.4.8"
|
version = "0.4.9"
|
||||||
author = "Jonathan Bernard"
|
author = "Jonathan Bernard"
|
||||||
description = "Jonathan's opinionated extensions and auth layer for Jester."
|
description = "Jonathan's opinionated extensions and auth layer for Jester."
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
@@ -90,6 +90,40 @@ func origin*(req: Request): Option[string] =
|
|||||||
else: none[string]()
|
else: none[string]()
|
||||||
|
|
||||||
|
|
||||||
|
func traceparent*(req: Request): Option[string] =
|
||||||
|
## Extract the traceparent from the request headers, if present.
|
||||||
|
if req.headers.contains("traceparent"):
|
||||||
|
return some(req.headers["traceparent"])
|
||||||
|
else:
|
||||||
|
return none[string]()
|
||||||
|
|
||||||
|
|
||||||
|
proc makeTraceContextHeaders*(req: Request, traceParentId: string): HttpHeaders =
|
||||||
|
var headers = HttpHeaders(@[])
|
||||||
|
|
||||||
|
if request.headers.contains("traceparent"):
|
||||||
|
# If the traceparent header is present, we should update it with our
|
||||||
|
# parent-id.
|
||||||
|
let traceparentParts = request.headers["traceparent"].split("-")
|
||||||
|
if traceparentParts.len != 4:
|
||||||
|
headers["traceparent"] = "00-$#-$#-00" % [
|
||||||
|
replace($genUUID(), "-", ""), # trace-id
|
||||||
|
traceParentId, # parent-id
|
||||||
|
]
|
||||||
|
|
||||||
|
else:
|
||||||
|
headers["traceparent"] = "00-$#-$#-$#" % [
|
||||||
|
traceparentParts[1], # trace-id
|
||||||
|
traceParentId, # parent-id
|
||||||
|
traceparentParts[3], # flags
|
||||||
|
]
|
||||||
|
|
||||||
|
if request.headers.contains("tracestate"):
|
||||||
|
headers["tracestate"] = request.headers["tracestate"]
|
||||||
|
|
||||||
|
return headers
|
||||||
|
|
||||||
|
|
||||||
proc respondWithRawJson*(
|
proc respondWithRawJson*(
|
||||||
req: Request,
|
req: Request,
|
||||||
body: JsonNode,
|
body: JsonNode,
|
||||||
|
Reference in New Issue
Block a user