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