-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeys.go
More file actions
87 lines (79 loc) · 2.78 KB
/
Copy pathkeys.go
File metadata and controls
87 lines (79 loc) · 2.78 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Package logctx provides a slog.Handler wrapper that auto-injects mandatory
// observability fields (service, commit_id, trace_id, tid, team_id) onto every
// log record by reading them from a context.Context.
//
// Setters and getters on this file are the only sanctioned way to put those
// fields onto a context; the handler in handler.go is the only sanctioned way
// to read them off again when emitting a record.
package logctx
import "context"
// Unexported context keys — these prevent collisions with other packages that
// might want to store strings on a context under the same name. Each type is
// a distinct empty struct so equality is identity, not value-based.
type (
traceIDCtxKey struct{}
tidCtxKey struct{}
teamIDCtxKey struct{}
)
// WithTraceID returns a copy of ctx carrying the supplied trace_id. The trace
// id is the W3C TraceContext trace ID when an OpenTelemetry span is in flight,
// falling back to the upstream request_id for non-span paths. Passing an empty
// string is permitted and behaves like no annotation — the handler will emit
// an empty trace_id field.
func WithTraceID(ctx context.Context, v string) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, traceIDCtxKey{}, v)
}
// TraceIDFromContext extracts the trace_id previously stored by WithTraceID.
// Returns an empty string when ctx is nil or carries no trace id — callers
// should NEVER panic on a missing field.
func TraceIDFromContext(ctx context.Context) string {
if ctx == nil {
return ""
}
if v, ok := ctx.Value(traceIDCtxKey{}).(string); ok {
return v
}
return ""
}
// WithTID returns a copy of ctx carrying the supplied tid (River job task ID
// for worker jobs; empty for non-job code paths).
func WithTID(ctx context.Context, v string) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, tidCtxKey{}, v)
}
// TIDFromContext extracts the tid previously stored by WithTID. Returns an
// empty string when absent or when ctx is nil.
func TIDFromContext(ctx context.Context) string {
if ctx == nil {
return ""
}
if v, ok := ctx.Value(tidCtxKey{}).(string); ok {
return v
}
return ""
}
// WithTeamID returns a copy of ctx carrying the supplied team_id (the JWT
// team_id claim, propagated from the auth middleware).
func WithTeamID(ctx context.Context, v string) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, teamIDCtxKey{}, v)
}
// TeamIDFromContext extracts the team_id previously stored by WithTeamID.
// Returns an empty string when absent (unauthenticated request) or when ctx
// is nil.
func TeamIDFromContext(ctx context.Context) string {
if ctx == nil {
return ""
}
if v, ok := ctx.Value(teamIDCtxKey{}).(string); ok {
return v
}
return ""
}