1 package context 2 3 import ( 4 "context" 5 "time" 6 ) 7 8 // Since looks up key, which should be a time.Time, and returns the duration 9 // since that time. If the key is not found, the value returned will be zero. 10 // This is helpful when inferring metrics related to context execution times. 11 func Since(ctx context.Context, key interface{}) time.Duration { 12 if startedAt, ok := ctx.Value(key).(time.Time); ok { 13 return time.Since(startedAt) 14 } 15 return 0 16 } 17 18 // GetStringValue returns a string value from the context. The empty string 19 // will be returned if not found. 20 func GetStringValue(ctx context.Context, key interface{}) (value string) { 21 if valuev, ok := ctx.Value(key).(string); ok { 22 value = valuev 23 } 24 return value 25 } 26