Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | // Copyright (c) 2017 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 | import opentracing "github.com/opentracing/opentracing-go" |
| 18 | |
| 19 | // Observer can be registered with the Tracer to receive notifications about |
| 20 | // new Spans. |
| 21 | // |
| 22 | // Deprecated: use jaeger.ContribObserver instead. |
| 23 | type Observer interface { |
| 24 | OnStartSpan(operationName string, options opentracing.StartSpanOptions) SpanObserver |
| 25 | } |
| 26 | |
| 27 | // SpanObserver is created by the Observer and receives notifications about |
| 28 | // other Span events. |
| 29 | // |
| 30 | // Deprecated: use jaeger.ContribSpanObserver instead. |
| 31 | type SpanObserver interface { |
| 32 | OnSetOperationName(operationName string) |
| 33 | OnSetTag(key string, value interface{}) |
| 34 | OnFinish(options opentracing.FinishOptions) |
| 35 | } |
| 36 | |
| 37 | // compositeObserver is a dispatcher to other observers |
| 38 | type compositeObserver struct { |
| 39 | observers []ContribObserver |
| 40 | } |
| 41 | |
| 42 | // compositeSpanObserver is a dispatcher to other span observers |
| 43 | type compositeSpanObserver struct { |
| 44 | observers []ContribSpanObserver |
| 45 | } |
| 46 | |
| 47 | // noopSpanObserver is used when there are no observers registered |
| 48 | // on the Tracer or none of them returns span observers from OnStartSpan. |
| 49 | var noopSpanObserver = &compositeSpanObserver{} |
| 50 | |
| 51 | func (o *compositeObserver) append(contribObserver ContribObserver) { |
| 52 | o.observers = append(o.observers, contribObserver) |
| 53 | } |
| 54 | |
| 55 | func (o *compositeObserver) OnStartSpan(sp opentracing.Span, operationName string, options opentracing.StartSpanOptions) ContribSpanObserver { |
| 56 | var spanObservers []ContribSpanObserver |
| 57 | for _, obs := range o.observers { |
| 58 | spanObs, ok := obs.OnStartSpan(sp, operationName, options) |
| 59 | if ok { |
| 60 | if spanObservers == nil { |
| 61 | spanObservers = make([]ContribSpanObserver, 0, len(o.observers)) |
| 62 | } |
| 63 | spanObservers = append(spanObservers, spanObs) |
| 64 | } |
| 65 | } |
| 66 | if len(spanObservers) == 0 { |
| 67 | return noopSpanObserver |
| 68 | } |
| 69 | return &compositeSpanObserver{observers: spanObservers} |
| 70 | } |
| 71 | |
| 72 | func (o *compositeSpanObserver) OnSetOperationName(operationName string) { |
| 73 | for _, obs := range o.observers { |
| 74 | obs.OnSetOperationName(operationName) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func (o *compositeSpanObserver) OnSetTag(key string, value interface{}) { |
| 79 | for _, obs := range o.observers { |
| 80 | obs.OnSetTag(key, value) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func (o *compositeSpanObserver) OnFinish(options opentracing.FinishOptions) { |
| 85 | for _, obs := range o.observers { |
| 86 | obs.OnFinish(options) |
| 87 | } |
| 88 | } |