-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex03_propagation.go
More file actions
47 lines (38 loc) · 1.69 KB
/
ex03_propagation.go
File metadata and controls
47 lines (38 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package ctxexercises
// Context: Context Values and Layered Architecture
// You have a layered architecture: HTTP Handler -> Service -> Database.
//
// Why this matters: Tracing and observability require passing a "Trace ID"
// (or Request ID) through every single layer, from the incoming HTTP request
// down to the final SQL query so you can search logs effectively.
// `context.WithValue` is the idiomatic way to pass this metadata through
// layers that don't need to know about it.
//
// Rule: ALWAYS define custom, unexported types for your Context Keys to prevent
// collisions with other packages (e.g., `type contextKey int`, then `const TraceKey contextKey = 1`).
//
// Requirements:
// 1. Define a private custom type for context keys (e.g. `type traceKeyType string`).
// 2. Wrap `ctx` using `context.WithValue` in `Handler` to inject the `traceID`.
// 3. Extract the `traceID` from `ctx` in `DatabaseLayer` and return it.
import "context"
// TODO: Define a custom, unexported type for your Context Key here.
// e.g. type ctxKey string
// const TraceIDKey ctxKey = "traceID"
func Handler(traceID string) string {
ctx := context.Background()
// BUG: The context is currently empty.
// TODO: Inject `traceID` into a new context using `context.WithValue`.
// Be sure to use your custom key type!
return ServiceLayer(ctx)
}
func ServiceLayer(ctx context.Context) string {
// The service layer doesn't care about the traceID. It just passes the context down.
return DatabaseLayer(ctx)
}
func DatabaseLayer(ctx context.Context) string {
// BUG: Currently returns an empty string.
// TODO: Extract the trace ID using `ctx.Value(YourKey)`.
// You will need to type-assert the result to a string.
return ""
}