util.go 689 B

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