span.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2022 The OpenZipkin Authors
  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 zipkintracer
  15. import (
  16. "fmt"
  17. "time"
  18. otobserver "github.com/opentracing-contrib/go-observer"
  19. opentracing "github.com/opentracing/opentracing-go"
  20. "github.com/opentracing/opentracing-go/ext"
  21. "github.com/opentracing/opentracing-go/log"
  22. "github.com/openzipkin/zipkin-go"
  23. )
  24. // FinisherWithDuration allows to finish span with given duration
  25. type FinisherWithDuration interface {
  26. FinishedWithDuration(d time.Duration)
  27. }
  28. type spanImpl struct {
  29. tracer *tracerImpl
  30. zipkinSpan zipkin.Span
  31. startTime time.Time
  32. observer otobserver.SpanObserver
  33. }
  34. func (s *spanImpl) SetOperationName(operationName string) opentracing.Span {
  35. if s.observer != nil {
  36. s.observer.OnSetOperationName(operationName)
  37. }
  38. s.zipkinSpan.SetName(operationName)
  39. return s
  40. }
  41. func (s *spanImpl) SetTag(key string, value interface{}) opentracing.Span {
  42. if s.observer != nil {
  43. s.observer.OnSetTag(key, value)
  44. }
  45. if key == string(ext.SamplingPriority) {
  46. // there are no means for now to change the sampling decision
  47. // but when finishedSpanHandler is in place we could change this.
  48. return s
  49. }
  50. if key == string(ext.SpanKind) ||
  51. key == string(ext.PeerService) ||
  52. key == string(ext.PeerHostIPv4) ||
  53. key == string(ext.PeerHostIPv6) ||
  54. key == string(ext.PeerPort) {
  55. // this tags are translated into kind and remoteEndpoint which can
  56. // only be set on span creation
  57. return s
  58. }
  59. s.zipkinSpan.Tag(key, fmt.Sprint(value))
  60. return s
  61. }
  62. func (s *spanImpl) LogKV(keyValues ...interface{}) {
  63. fields, err := log.InterleavedKVToFields(keyValues...)
  64. if err != nil {
  65. return
  66. }
  67. for _, field := range fields {
  68. s.zipkinSpan.Annotate(time.Now(), field.String())
  69. }
  70. }
  71. func (s *spanImpl) LogFields(fields ...log.Field) {
  72. s.logFields(time.Now(), fields...)
  73. }
  74. func (s *spanImpl) logFields(t time.Time, fields ...log.Field) {
  75. for _, field := range fields {
  76. s.zipkinSpan.Annotate(t, field.String())
  77. }
  78. }
  79. func (s *spanImpl) LogEvent(event string) {
  80. s.Log(opentracing.LogData{
  81. Event: event,
  82. })
  83. }
  84. func (s *spanImpl) LogEventWithPayload(event string, payload interface{}) {
  85. s.Log(opentracing.LogData{
  86. Event: event,
  87. Payload: payload,
  88. })
  89. }
  90. func (s *spanImpl) Log(ld opentracing.LogData) {
  91. if ld.Timestamp.IsZero() {
  92. ld.Timestamp = time.Now()
  93. }
  94. s.zipkinSpan.Annotate(ld.Timestamp, fmt.Sprintf("%s:%s", ld.Event, ld.Payload))
  95. }
  96. func (s *spanImpl) Finish() {
  97. if s.observer != nil {
  98. s.observer.OnFinish(opentracing.FinishOptions{})
  99. }
  100. s.zipkinSpan.Finish()
  101. }
  102. func (s *spanImpl) FinishWithOptions(opts opentracing.FinishOptions) {
  103. if s.observer != nil {
  104. s.observer.OnFinish(opts)
  105. }
  106. for _, lr := range opts.LogRecords {
  107. s.logFields(lr.Timestamp, lr.Fields...)
  108. }
  109. if !opts.FinishTime.IsZero() {
  110. f, ok := s.zipkinSpan.(FinisherWithDuration)
  111. if !ok {
  112. return
  113. }
  114. f.FinishedWithDuration(opts.FinishTime.Sub(s.startTime))
  115. return
  116. }
  117. s.Finish()
  118. }
  119. func (s *spanImpl) Tracer() opentracing.Tracer {
  120. return s.tracer
  121. }
  122. func (s *spanImpl) Context() opentracing.SpanContext {
  123. return SpanContext(s.zipkinSpan.Context())
  124. }
  125. func (s *spanImpl) SetBaggageItem(key, val string) opentracing.Span {
  126. return s
  127. }
  128. func (s *spanImpl) BaggageItem(key string) string {
  129. return ""
  130. }