github.com/newrelic/newrelic-client-go@v1.1.0/pkg/contextkeys/contextkeys.go (about) 1 package contextkeys 2 3 import "context" 4 5 // contextKeys gets and sets context values from a context. 6 type contextKeys struct { 7 accountID struct{} 8 } 9 10 var ( 11 keys = contextKeys{ 12 accountID: struct{}{}, 13 } 14 ) 15 16 // SetAccountID inserts the account ID value into context. 17 func SetAccountID(ctx context.Context, value string) context.Context { 18 return keys.SetAccountID(ctx, value) 19 } 20 21 // GetAccountID returns the account ID from the context. 22 func GetAccountID(ctx context.Context) (string, bool) { 23 return keys.GetAccountID(ctx) 24 } 25 26 // SetAccountID inserts the account ID value into context. 27 func (c contextKeys) SetAccountID(ctx context.Context, value string) context.Context { 28 if nil == ctx { 29 ctx = context.Background() 30 } 31 32 return context.WithValue(ctx, c.accountID, value) 33 } 34 35 // GetAccountID returns the account ID from the context. 36 func (c contextKeys) GetAccountID(ctx context.Context) (string, bool) { 37 if nil != ctx { 38 accountID, ok := ctx.Value(keys.accountID).(string) 39 return accountID, ok 40 } 41 42 return "", false 43 }