observer.go 2.8 KB

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