-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.go
More file actions
40 lines (33 loc) · 959 Bytes
/
scope.go
File metadata and controls
40 lines (33 loc) · 959 Bytes
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
package cortex
import "context"
type contextKey int
const (
tenantKey contextKey = iota
appKey
)
// TenantFromContext extracts the tenant identifier from the context.
// Returns an empty string if no tenant is set.
func TenantFromContext(ctx context.Context) string {
v, ok := ctx.Value(tenantKey).(string)
if !ok {
return ""
}
return v
}
// WithTenant returns a copy of ctx with the tenant identifier attached.
func WithTenant(ctx context.Context, tenant string) context.Context {
return context.WithValue(ctx, tenantKey, tenant)
}
// AppFromContext extracts the app identifier from the context.
// Returns an empty string if no app is set.
func AppFromContext(ctx context.Context) string {
v, ok := ctx.Value(appKey).(string)
if !ok {
return ""
}
return v
}
// WithApp returns a copy of ctx with the app identifier attached.
func WithApp(ctx context.Context, app string) context.Context {
return context.WithValue(ctx, appKey, app)
}