sampler_v2.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2019 Uber Technologies, Inc.
  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 jaeger
  15. // SamplingDecision is returned by the V2 samplers.
  16. type SamplingDecision struct {
  17. Sample bool
  18. Retryable bool
  19. Tags []Tag
  20. }
  21. // SamplerV2 is an extension of the V1 samplers that allows sampling decisions
  22. // be made at different points of the span lifecycle.
  23. type SamplerV2 interface {
  24. OnCreateSpan(span *Span) SamplingDecision
  25. OnSetOperationName(span *Span, operationName string) SamplingDecision
  26. OnSetTag(span *Span, key string, value interface{}) SamplingDecision
  27. OnFinishSpan(span *Span) SamplingDecision
  28. // Close does a clean shutdown of the sampler, stopping any background
  29. // go-routines it may have started.
  30. Close()
  31. }
  32. // samplerV1toV2 wraps legacy V1 sampler into an adapter that make it look like V2.
  33. func samplerV1toV2(s Sampler) SamplerV2 {
  34. if s2, ok := s.(SamplerV2); ok {
  35. return s2
  36. }
  37. type legacySamplerV1toV2Adapter struct {
  38. legacySamplerV1Base
  39. }
  40. return &legacySamplerV1toV2Adapter{
  41. legacySamplerV1Base: legacySamplerV1Base{
  42. delegate: s.IsSampled,
  43. },
  44. }
  45. }
  46. // SamplerV2Base can be used by V2 samplers to implement dummy V1 methods.
  47. // Supporting V1 API is required because Tracer configuration only accepts V1 Sampler
  48. // for backwards compatibility reasons.
  49. // TODO (breaking change) remove this in the next major release
  50. type SamplerV2Base struct{}
  51. // IsSampled implements IsSampled of Sampler.
  52. func (SamplerV2Base) IsSampled(id TraceID, operation string) (sampled bool, tags []Tag) {
  53. return false, nil
  54. }
  55. // Close implements Close of Sampler.
  56. func (SamplerV2Base) Close() {}
  57. // Equal implements Equal of Sampler.
  58. func (SamplerV2Base) Equal(other Sampler) bool { return false }
  59. // legacySamplerV1Base is used as a base for simple samplers that only implement
  60. // the legacy isSampled() function that is not sensitive to its arguments.
  61. type legacySamplerV1Base struct {
  62. delegate func(id TraceID, operation string) (sampled bool, tags []Tag)
  63. }
  64. func (s *legacySamplerV1Base) OnCreateSpan(span *Span) SamplingDecision {
  65. isSampled, tags := s.delegate(span.context.traceID, span.operationName)
  66. return SamplingDecision{Sample: isSampled, Retryable: false, Tags: tags}
  67. }
  68. func (s *legacySamplerV1Base) OnSetOperationName(span *Span, operationName string) SamplingDecision {
  69. isSampled, tags := s.delegate(span.context.traceID, span.operationName)
  70. return SamplingDecision{Sample: isSampled, Retryable: false, Tags: tags}
  71. }
  72. func (s *legacySamplerV1Base) OnSetTag(span *Span, key string, value interface{}) SamplingDecision {
  73. return SamplingDecision{Sample: false, Retryable: true}
  74. }
  75. func (s *legacySamplerV1Base) OnFinishSpan(span *Span) SamplingDecision {
  76. return SamplingDecision{Sample: false, Retryable: true}
  77. }
  78. func (s *legacySamplerV1Base) Close() {}