2015-07-13 21:13:28 -04:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Since looks up key, which should be a time.Time, and returns the duration
|
|
|
|
// since that time. If the key is not found, the value returned will be zero.
|
|
|
|
// This is helpful when inferring metrics related to context execution times.
|
|
|
|
func Since(ctx Context, key interface{}) time.Duration {
|
2016-04-11 20:29:17 -04:00
|
|
|
if startedAt, ok := ctx.Value(key).(time.Time); ok {
|
|
|
|
return time.Since(startedAt)
|
2015-07-13 21:13:28 -04:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStringValue returns a string value from the context. The empty string
|
|
|
|
// will be returned if not found.
|
2015-10-30 20:46:25 -04:00
|
|
|
func GetStringValue(ctx Context, key interface{}) (value string) {
|
2016-04-11 20:29:17 -04:00
|
|
|
if valuev, ok := ctx.Value(key).(string); ok {
|
|
|
|
value = valuev
|
2015-07-13 21:13:28 -04:00
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|