A request ID links one response to its log event. It is useful context, not a user identity, permission or proof that a request is trustworthy.
This lab always generates server-owned IDs. It does not copy arbitrary incoming X-Request-ID header values into logs. The final middleware places its own value in a response header and a private context key for application code.
Use an unexported key type so another package’s string request_id value cannot collide. Derive from the supplied parent with context.WithValue rather than starting a new background context.
The identifier’s length/alphabet checks are part of the local format. They cannot prove that arbitrary client text contains no sensitive data; generating server-owned tokens is a separate policy decision.
package main
import ("fmt";"context")
func main(){
type requestKey struct{};parent:=context.Background();child:=context.WithValue(parent,requestKey{},"lab-1");fmt.Println(child.Value(requestKey{}),parent.Value(requestKey{}))
}Cancellation and deadlines remain attached to the parent request. A context used only to carry an ID should not disconnect that lifecycle.
The local command’s IDs are process-scoped correlation values and can restart when the process restarts. The course does not claim global uniqueness or W3C distributed-trace propagation. Those are a later operational boundary.
Keep IDs in events, not metric labels. Metrics aggregate a closed set of normalized method/route/status groups; logs provide a way to inspect one exemplar. Mixing the two creates unbounded cardinality and makes a tiny metric collector grow with traffic.
package main
import ("fmt";"context")
func main(){
parent,cancel:=context.WithCancel(context.Background());defer cancel();child:=context.WithValue(parent,struct{}{},"lab-2");cancel();fmt.Println(child.Err())
}Complete the context helper and test private-key separation, exact ID lengths and cancellation. The final middleware supplies the server generator and response header.
Next we serialize only approved event fields and prove that sensitive source fields are omitted.