observer.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (c) 2017 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package jaeger
  21. import opentracing "github.com/opentracing/opentracing-go"
  22. // Observer can be registered with the Tracer to receive notifications about
  23. // new Spans.
  24. //
  25. // Deprecated: use jaeger.ContribObserver instead.
  26. type Observer interface {
  27. OnStartSpan(operationName string, options opentracing.StartSpanOptions) SpanObserver
  28. }
  29. // SpanObserver is created by the Observer and receives notifications about
  30. // other Span events.
  31. //
  32. // Deprecated: use jaeger.ContribSpanObserver instead.
  33. type SpanObserver interface {
  34. OnSetOperationName(operationName string)
  35. OnSetTag(key string, value interface{})
  36. OnFinish(options opentracing.FinishOptions)
  37. }
  38. // compositeObserver is a dispatcher to other observers
  39. type compositeObserver struct {
  40. observers []ContribObserver
  41. }
  42. // compositeSpanObserver is a dispatcher to other span observers
  43. type compositeSpanObserver struct {
  44. observers []ContribSpanObserver
  45. }
  46. // noopSpanObserver is used when there are no observers registered
  47. // on the Tracer or none of them returns span observers from OnStartSpan.
  48. var noopSpanObserver = &compositeSpanObserver{}
  49. func (o *compositeObserver) append(contribObserver ContribObserver) {
  50. o.observers = append(o.observers, contribObserver)
  51. }
  52. func (o *compositeObserver) OnStartSpan(sp opentracing.Span, operationName string, options opentracing.StartSpanOptions) ContribSpanObserver {
  53. var spanObservers []ContribSpanObserver
  54. for _, obs := range o.observers {
  55. spanObs, ok := obs.OnStartSpan(sp, operationName, options)
  56. if ok {
  57. if spanObservers == nil {
  58. spanObservers = make([]ContribSpanObserver, 0, len(o.observers))
  59. }
  60. spanObservers = append(spanObservers, spanObs)
  61. }
  62. }
  63. if len(spanObservers) == 0 {
  64. return noopSpanObserver
  65. }
  66. return &compositeSpanObserver{observers: spanObservers}
  67. }
  68. func (o *compositeSpanObserver) OnSetOperationName(operationName string) {
  69. for _, obs := range o.observers {
  70. obs.OnSetOperationName(operationName)
  71. }
  72. }
  73. func (o *compositeSpanObserver) OnSetTag(key string, value interface{}) {
  74. for _, obs := range o.observers {
  75. obs.OnSetTag(key, value)
  76. }
  77. }
  78. func (o *compositeSpanObserver) OnFinish(options opentracing.FinishOptions) {
  79. for _, obs := range o.observers {
  80. obs.OnFinish(options)
  81. }
  82. }