span.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright The OpenTelemetry 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 tracetest // import "go.opentelemetry.io/otel/sdk/trace/tracetest"
  15. import (
  16. "time"
  17. "go.opentelemetry.io/otel/attribute"
  18. "go.opentelemetry.io/otel/sdk/instrumentation"
  19. "go.opentelemetry.io/otel/sdk/resource"
  20. tracesdk "go.opentelemetry.io/otel/sdk/trace"
  21. "go.opentelemetry.io/otel/trace"
  22. )
  23. // SpanStubs is a slice of SpanStub use for testing an SDK.
  24. type SpanStubs []SpanStub
  25. // SpanStubsFromReadOnlySpans returns SpanStubs populated from ro.
  26. func SpanStubsFromReadOnlySpans(ro []tracesdk.ReadOnlySpan) SpanStubs {
  27. if len(ro) == 0 {
  28. return nil
  29. }
  30. s := make(SpanStubs, 0, len(ro))
  31. for _, r := range ro {
  32. s = append(s, SpanStubFromReadOnlySpan(r))
  33. }
  34. return s
  35. }
  36. // Snapshots returns s as a slice of ReadOnlySpans.
  37. func (s SpanStubs) Snapshots() []tracesdk.ReadOnlySpan {
  38. if len(s) == 0 {
  39. return nil
  40. }
  41. ro := make([]tracesdk.ReadOnlySpan, len(s))
  42. for i := 0; i < len(s); i++ {
  43. ro[i] = s[i].Snapshot()
  44. }
  45. return ro
  46. }
  47. // SpanStub is a stand-in for a Span.
  48. type SpanStub struct {
  49. Name string
  50. SpanContext trace.SpanContext
  51. Parent trace.SpanContext
  52. SpanKind trace.SpanKind
  53. StartTime time.Time
  54. EndTime time.Time
  55. Attributes []attribute.KeyValue
  56. Events []tracesdk.Event
  57. Links []tracesdk.Link
  58. Status tracesdk.Status
  59. DroppedAttributes int
  60. DroppedEvents int
  61. DroppedLinks int
  62. ChildSpanCount int
  63. Resource *resource.Resource
  64. InstrumentationLibrary instrumentation.Library
  65. }
  66. // SpanStubFromReadOnlySpan returns a SpanStub populated from ro.
  67. func SpanStubFromReadOnlySpan(ro tracesdk.ReadOnlySpan) SpanStub {
  68. if ro == nil {
  69. return SpanStub{}
  70. }
  71. return SpanStub{
  72. Name: ro.Name(),
  73. SpanContext: ro.SpanContext(),
  74. Parent: ro.Parent(),
  75. SpanKind: ro.SpanKind(),
  76. StartTime: ro.StartTime(),
  77. EndTime: ro.EndTime(),
  78. Attributes: ro.Attributes(),
  79. Events: ro.Events(),
  80. Links: ro.Links(),
  81. Status: ro.Status(),
  82. DroppedAttributes: ro.DroppedAttributes(),
  83. DroppedEvents: ro.DroppedEvents(),
  84. DroppedLinks: ro.DroppedLinks(),
  85. ChildSpanCount: ro.ChildSpanCount(),
  86. Resource: ro.Resource(),
  87. InstrumentationLibrary: ro.InstrumentationScope(),
  88. }
  89. }
  90. // Snapshot returns a read-only copy of the SpanStub.
  91. func (s SpanStub) Snapshot() tracesdk.ReadOnlySpan {
  92. return spanSnapshot{
  93. name: s.Name,
  94. spanContext: s.SpanContext,
  95. parent: s.Parent,
  96. spanKind: s.SpanKind,
  97. startTime: s.StartTime,
  98. endTime: s.EndTime,
  99. attributes: s.Attributes,
  100. events: s.Events,
  101. links: s.Links,
  102. status: s.Status,
  103. droppedAttributes: s.DroppedAttributes,
  104. droppedEvents: s.DroppedEvents,
  105. droppedLinks: s.DroppedLinks,
  106. childSpanCount: s.ChildSpanCount,
  107. resource: s.Resource,
  108. instrumentationScope: s.InstrumentationLibrary,
  109. }
  110. }
  111. type spanSnapshot struct {
  112. // Embed the interface to implement the private method.
  113. tracesdk.ReadOnlySpan
  114. name string
  115. spanContext trace.SpanContext
  116. parent trace.SpanContext
  117. spanKind trace.SpanKind
  118. startTime time.Time
  119. endTime time.Time
  120. attributes []attribute.KeyValue
  121. events []tracesdk.Event
  122. links []tracesdk.Link
  123. status tracesdk.Status
  124. droppedAttributes int
  125. droppedEvents int
  126. droppedLinks int
  127. childSpanCount int
  128. resource *resource.Resource
  129. instrumentationScope instrumentation.Scope
  130. }
  131. func (s spanSnapshot) Name() string { return s.name }
  132. func (s spanSnapshot) SpanContext() trace.SpanContext { return s.spanContext }
  133. func (s spanSnapshot) Parent() trace.SpanContext { return s.parent }
  134. func (s spanSnapshot) SpanKind() trace.SpanKind { return s.spanKind }
  135. func (s spanSnapshot) StartTime() time.Time { return s.startTime }
  136. func (s spanSnapshot) EndTime() time.Time { return s.endTime }
  137. func (s spanSnapshot) Attributes() []attribute.KeyValue { return s.attributes }
  138. func (s spanSnapshot) Links() []tracesdk.Link { return s.links }
  139. func (s spanSnapshot) Events() []tracesdk.Event { return s.events }
  140. func (s spanSnapshot) Status() tracesdk.Status { return s.status }
  141. func (s spanSnapshot) DroppedAttributes() int { return s.droppedAttributes }
  142. func (s spanSnapshot) DroppedLinks() int { return s.droppedLinks }
  143. func (s spanSnapshot) DroppedEvents() int { return s.droppedEvents }
  144. func (s spanSnapshot) ChildSpanCount() int { return s.childSpanCount }
  145. func (s spanSnapshot) Resource() *resource.Resource { return s.resource }
  146. func (s spanSnapshot) InstrumentationScope() instrumentation.Scope {
  147. return s.instrumentationScope
  148. }
  149. func (s spanSnapshot) InstrumentationLibrary() instrumentation.Library {
  150. return s.instrumentationScope
  151. }