-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
39 lines (31 loc) · 1.4 KB
/
context.go
File metadata and controls
39 lines (31 loc) · 1.4 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
package telemetry
import (
"context"
"github.com/google/uuid"
)
// correlationKeyType is the context key for correlation ID.
type correlationKeyType struct{}
// causationKeyType is the context key for causation ID.
type causationKeyType struct{}
// WithCorrelationID returns a new context with the given correlation ID.
// Correlation ID is used to track related operations across service boundaries.
func WithCorrelationID(ctx context.Context, correlationID uuid.UUID) context.Context {
return context.WithValue(ctx, correlationKeyType{}, correlationID)
}
// CorrelationIDFromContext extracts the correlation ID from the context.
// Returns the correlation ID and a boolean indicating if the ID was found.
func CorrelationIDFromContext(ctx context.Context) (uuid.UUID, bool) {
id, ok := ctx.Value(correlationKeyType{}).(uuid.UUID)
return id, ok
}
// WithCausationID returns a new context with the given causation ID.
// Causation ID is used to track the immediate cause of an operation.
func WithCausationID(ctx context.Context, causationID uuid.UUID) context.Context {
return context.WithValue(ctx, causationKeyType{}, causationID)
}
// CausationIDFromContext extracts the causation ID from the context.
// Returns the causation ID and a boolean indicating if the ID was found.
func CausationIDFromContext(ctx context.Context) (uuid.UUID, bool) {
id, ok := ctx.Value(causationKeyType{}).(uuid.UUID)
return id, ok
}