You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

128 lines
3.8 KiB

  1. // Copyright 2016 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package trace
  15. import (
  16. crand "crypto/rand"
  17. "encoding/binary"
  18. "fmt"
  19. "math/rand"
  20. "sync"
  21. "time"
  22. "golang.org/x/time/rate"
  23. )
  24. // SamplingPolicy provides an interface for sampling.
  25. //
  26. // Deprecated: see https://cloud.google.com/trace/docs/setup/go.
  27. type SamplingPolicy interface {
  28. // Sample returns a Decision.
  29. // If Trace is false in the returned Decision, then the Decision should be
  30. // the zero value.
  31. Sample(p Parameters) Decision
  32. }
  33. // Parameters contains the values passed to a SamplingPolicy's Sample method.
  34. //
  35. // Deprecated: see https://cloud.google.com/trace/docs/setup/go.
  36. type Parameters struct {
  37. HasTraceHeader bool // whether the incoming request has a valid X-Cloud-Trace-Context header.
  38. }
  39. // Decision is the value returned by a call to a SamplingPolicy's Sample method.
  40. //
  41. // Deprecated: see https://cloud.google.com/trace/docs/setup/go.
  42. type Decision struct {
  43. Trace bool // Whether to trace the request.
  44. Sample bool // Whether the trace is included in the random sample.
  45. Policy string // Name of the sampling policy.
  46. Weight float64 // Sample weight to be used in statistical calculations.
  47. }
  48. type sampler struct {
  49. fraction float64
  50. skipped float64
  51. *rate.Limiter
  52. *rand.Rand
  53. sync.Mutex
  54. }
  55. // Deprecated: see https://cloud.google.com/trace/docs/setup/go.
  56. func (s *sampler) Sample(p Parameters) Decision {
  57. s.Lock()
  58. x := s.Float64()
  59. d := s.sample(p, time.Now(), x)
  60. s.Unlock()
  61. return d
  62. }
  63. // sample contains the a deterministic, time-independent logic of Sample.
  64. func (s *sampler) sample(p Parameters, now time.Time, x float64) (d Decision) {
  65. d.Sample = x < s.fraction
  66. d.Trace = p.HasTraceHeader || d.Sample
  67. if !d.Trace {
  68. // We have no reason to trace this request.
  69. return Decision{}
  70. }
  71. // We test separately that the rate limit is not tiny before calling AllowN,
  72. // because of overflow problems in x/time/rate.
  73. if s.Limit() < 1e-9 || !s.AllowN(now, 1) {
  74. // Rejected by the rate limit.
  75. if d.Sample {
  76. s.skipped++
  77. }
  78. return Decision{}
  79. }
  80. if d.Sample {
  81. d.Policy, d.Weight = "default", (1.0+s.skipped)/s.fraction
  82. s.skipped = 0.0
  83. }
  84. return
  85. }
  86. // NewLimitedSampler returns a sampling policy that randomly samples a given
  87. // fraction of requests. It also enforces a limit on the number of traces per
  88. // second. It tries to trace every request with a trace header, but will not
  89. // exceed the qps limit to do it.
  90. //
  91. // Deprecated: see https://cloud.google.com/trace/docs/setup/go.
  92. func NewLimitedSampler(fraction, maxqps float64) (SamplingPolicy, error) {
  93. if !(fraction >= 0) {
  94. return nil, fmt.Errorf("invalid fraction %f", fraction)
  95. }
  96. if !(maxqps >= 0) {
  97. return nil, fmt.Errorf("invalid maxqps %f", maxqps)
  98. }
  99. // Set a limit on the number of accumulated "tokens", to limit bursts of
  100. // traced requests. Use one more than a second's worth of tokens, or 100,
  101. // whichever is smaller.
  102. // See https://godoc.org/golang.org/x/time/rate#NewLimiter.
  103. maxTokens := 100
  104. if maxqps < 99.0 {
  105. maxTokens = 1 + int(maxqps)
  106. }
  107. var seed int64
  108. if err := binary.Read(crand.Reader, binary.LittleEndian, &seed); err != nil {
  109. seed = time.Now().UnixNano()
  110. }
  111. s := sampler{
  112. fraction: fraction,
  113. Limiter: rate.NewLimiter(rate.Limit(maxqps), maxTokens),
  114. Rand: rand.New(rand.NewSource(seed)),
  115. }
  116. return &s, nil
  117. }