2015-02-05 21:56:50 +00: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.
|
2015-04-01 16:46:35 +00:00
|
|
|
func Since(ctx Context, key interface{}) time.Duration {
|
2015-02-05 21:56:50 +00:00
|
|
|
startedAtI := ctx.Value(key)
|
|
|
|
if startedAtI != nil {
|
|
|
|
if startedAt, ok := startedAtI.(time.Time); ok {
|
|
|
|
return time.Since(startedAt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStringValue returns a string value from the context. The empty string
|
|
|
|
// will be returned if not found.
|
2015-09-11 03:40:01 +00:00
|
|
|
func GetStringValue(ctx Context, key interface{}) (value string) {
|
2015-02-05 21:56:50 +00:00
|
|
|
stringi := ctx.Value(key)
|
|
|
|
if stringi != nil {
|
|
|
|
if valuev, ok := stringi.(string); ok {
|
|
|
|
value = valuev
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|