util.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Unless explicitly stated otherwise all files in this repository are licensed
  2. // under the Apache License Version 2.0.
  3. // This product includes software developed at Datadog (https://www.datadoghq.com/).
  4. // Copyright 2016 Datadog, Inc.
  5. package tracer
  6. import (
  7. "fmt"
  8. "strconv"
  9. "strings"
  10. "gopkg.in/DataDog/dd-trace-go.v1/internal/samplernames"
  11. )
  12. // toFloat64 attempts to convert value into a float64. If the value is an integer
  13. // greater or equal to 2^53 or less than or equal to -2^53, it will not be converted
  14. // into a float64 to avoid losing precision. If it succeeds in converting, toFloat64
  15. // returns the value and true, otherwise 0 and false.
  16. func toFloat64(value interface{}) (f float64, ok bool) {
  17. const max = (int64(1) << 53) - 1
  18. const min = -max
  19. switch i := value.(type) {
  20. case byte:
  21. return float64(i), true
  22. case float32:
  23. return float64(i), true
  24. case float64:
  25. return i, true
  26. case int:
  27. return float64(i), true
  28. case int8:
  29. return float64(i), true
  30. case int16:
  31. return float64(i), true
  32. case int32:
  33. return float64(i), true
  34. case int64:
  35. if i > max || i < min {
  36. return 0, false
  37. }
  38. return float64(i), true
  39. case uint:
  40. return float64(i), true
  41. case uint16:
  42. return float64(i), true
  43. case uint32:
  44. return float64(i), true
  45. case uint64:
  46. if i > uint64(max) {
  47. return 0, false
  48. }
  49. return float64(i), true
  50. case samplernames.SamplerName:
  51. return float64(i), true
  52. default:
  53. return 0, false
  54. }
  55. }
  56. // parseUint64 parses a uint64 from either an unsigned 64 bit base-10 string
  57. // or a signed 64 bit base-10 string representing an unsigned integer
  58. func parseUint64(str string) (uint64, error) {
  59. if strings.HasPrefix(str, "-") {
  60. id, err := strconv.ParseInt(str, 10, 64)
  61. if err != nil {
  62. return 0, err
  63. }
  64. return uint64(id), nil
  65. }
  66. return strconv.ParseUint(str, 10, 64)
  67. }
  68. func isValidPropagatableTag(k, v string) error {
  69. if len(k) == 0 {
  70. return fmt.Errorf("key length must be greater than zero")
  71. }
  72. for _, ch := range k {
  73. if ch < 32 || ch > 126 || ch == ' ' || ch == '=' || ch == ',' {
  74. return fmt.Errorf("key contains an invalid character %d", ch)
  75. }
  76. }
  77. if len(v) == 0 {
  78. return fmt.Errorf("value length must be greater than zero")
  79. }
  80. for _, ch := range v {
  81. if ch < 32 || ch > 126 || ch == ',' {
  82. return fmt.Errorf("value contains an invalid character %d", ch)
  83. }
  84. }
  85. return nil
  86. }
  87. func parsePropagatableTraceTags(s string) (map[string]string, error) {
  88. if len(s) == 0 {
  89. return nil, nil
  90. }
  91. tags := make(map[string]string)
  92. searchingKey, start := true, 0
  93. var key string
  94. for i, ch := range s {
  95. switch ch {
  96. case '=':
  97. if searchingKey {
  98. if i-start == 0 {
  99. return nil, fmt.Errorf("invalid format")
  100. }
  101. key = s[start:i]
  102. searchingKey, start = false, i+1
  103. }
  104. case ',':
  105. if searchingKey || i-start == 0 {
  106. return nil, fmt.Errorf("invalid format")
  107. }
  108. tags[key] = s[start:i]
  109. searchingKey, start = true, i+1
  110. }
  111. }
  112. if searchingKey || len(s)-start == 0 {
  113. return nil, fmt.Errorf("invalid format")
  114. }
  115. tags[key] = s[start:]
  116. return tags, nil
  117. }