2015-02-05 22:56:50 +01:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2017-08-12 00:31:16 +02:00
|
|
|
"context"
|
2015-02-05 22:56:50 +01:00
|
|
|
"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.
|
2017-08-12 00:31:16 +02:00
|
|
|
func Since(ctx context.Context, key interface{}) time.Duration {
|
2016-03-30 11:35:22 +02:00
|
|
|
if startedAt, ok := ctx.Value(key).(time.Time); ok {
|
|
|
|
return time.Since(startedAt)
|
2015-02-05 22:56:50 +01:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStringValue returns a string value from the context. The empty string
|
|
|
|
// will be returned if not found.
|
2017-08-12 00:31:16 +02:00
|
|
|
func GetStringValue(ctx context.Context, key interface{}) (value string) {
|
2016-03-30 11:35:22 +02:00
|
|
|
if valuev, ok := ctx.Value(key).(string); ok {
|
|
|
|
value = valuev
|
2015-02-05 22:56:50 +01:00
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|