traces.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package telemetry // import "go.opentelemetry.io/otel/trace/internal/telemetry"
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io"
  10. )
  11. // Traces represents the traces data that can be stored in a persistent storage,
  12. // OR can be embedded by other protocols that transfer OTLP traces data but do
  13. // not implement the OTLP protocol.
  14. //
  15. // The main difference between this message and collector protocol is that
  16. // in this message there will not be any "control" or "metadata" specific to
  17. // OTLP protocol.
  18. //
  19. // When new fields are added into this message, the OTLP request MUST be updated
  20. // as well.
  21. type Traces struct {
  22. // An array of ResourceSpans.
  23. // For data coming from a single resource this array will typically contain
  24. // one element. Intermediary nodes that receive data from multiple origins
  25. // typically batch the data before forwarding further and in that case this
  26. // array will contain multiple elements.
  27. ResourceSpans []*ResourceSpans `json:"resourceSpans,omitempty"`
  28. }
  29. // UnmarshalJSON decodes the OTLP formatted JSON contained in data into td.
  30. func (td *Traces) UnmarshalJSON(data []byte) error {
  31. decoder := json.NewDecoder(bytes.NewReader(data))
  32. t, err := decoder.Token()
  33. if err != nil {
  34. return err
  35. }
  36. if t != json.Delim('{') {
  37. return errors.New("invalid TracesData type")
  38. }
  39. for decoder.More() {
  40. keyIface, err := decoder.Token()
  41. if err != nil {
  42. if errors.Is(err, io.EOF) {
  43. // Empty.
  44. return nil
  45. }
  46. return err
  47. }
  48. key, ok := keyIface.(string)
  49. if !ok {
  50. return fmt.Errorf("invalid TracesData field: %#v", keyIface)
  51. }
  52. switch key {
  53. case "resourceSpans", "resource_spans":
  54. err = decoder.Decode(&td.ResourceSpans)
  55. default:
  56. // Skip unknown.
  57. }
  58. if err != nil {
  59. return err
  60. }
  61. }
  62. return nil
  63. }
  64. // A collection of ScopeSpans from a Resource.
  65. type ResourceSpans struct {
  66. // The resource for the spans in this message.
  67. // If this field is not set then no resource info is known.
  68. Resource Resource `json:"resource"`
  69. // A list of ScopeSpans that originate from a resource.
  70. ScopeSpans []*ScopeSpans `json:"scopeSpans,omitempty"`
  71. // This schema_url applies to the data in the "resource" field. It does not apply
  72. // to the data in the "scope_spans" field which have their own schema_url field.
  73. SchemaURL string `json:"schemaUrl,omitempty"`
  74. }
  75. // UnmarshalJSON decodes the OTLP formatted JSON contained in data into rs.
  76. func (rs *ResourceSpans) UnmarshalJSON(data []byte) error {
  77. decoder := json.NewDecoder(bytes.NewReader(data))
  78. t, err := decoder.Token()
  79. if err != nil {
  80. return err
  81. }
  82. if t != json.Delim('{') {
  83. return errors.New("invalid ResourceSpans type")
  84. }
  85. for decoder.More() {
  86. keyIface, err := decoder.Token()
  87. if err != nil {
  88. if errors.Is(err, io.EOF) {
  89. // Empty.
  90. return nil
  91. }
  92. return err
  93. }
  94. key, ok := keyIface.(string)
  95. if !ok {
  96. return fmt.Errorf("invalid ResourceSpans field: %#v", keyIface)
  97. }
  98. switch key {
  99. case "resource":
  100. err = decoder.Decode(&rs.Resource)
  101. case "scopeSpans", "scope_spans":
  102. err = decoder.Decode(&rs.ScopeSpans)
  103. case "schemaUrl", "schema_url":
  104. err = decoder.Decode(&rs.SchemaURL)
  105. default:
  106. // Skip unknown.
  107. }
  108. if err != nil {
  109. return err
  110. }
  111. }
  112. return nil
  113. }
  114. // A collection of Spans produced by an InstrumentationScope.
  115. type ScopeSpans struct {
  116. // The instrumentation scope information for the spans in this message.
  117. // Semantically when InstrumentationScope isn't set, it is equivalent with
  118. // an empty instrumentation scope name (unknown).
  119. Scope *Scope `json:"scope"`
  120. // A list of Spans that originate from an instrumentation scope.
  121. Spans []*Span `json:"spans,omitempty"`
  122. // The Schema URL, if known. This is the identifier of the Schema that the span data
  123. // is recorded in. To learn more about Schema URL see
  124. // https://opentelemetry.io/docs/specs/otel/schemas/#schema-url
  125. // This schema_url applies to all spans and span events in the "spans" field.
  126. SchemaURL string `json:"schemaUrl,omitempty"`
  127. }
  128. // UnmarshalJSON decodes the OTLP formatted JSON contained in data into ss.
  129. func (ss *ScopeSpans) UnmarshalJSON(data []byte) error {
  130. decoder := json.NewDecoder(bytes.NewReader(data))
  131. t, err := decoder.Token()
  132. if err != nil {
  133. return err
  134. }
  135. if t != json.Delim('{') {
  136. return errors.New("invalid ScopeSpans type")
  137. }
  138. for decoder.More() {
  139. keyIface, err := decoder.Token()
  140. if err != nil {
  141. if errors.Is(err, io.EOF) {
  142. // Empty.
  143. return nil
  144. }
  145. return err
  146. }
  147. key, ok := keyIface.(string)
  148. if !ok {
  149. return fmt.Errorf("invalid ScopeSpans field: %#v", keyIface)
  150. }
  151. switch key {
  152. case "scope":
  153. err = decoder.Decode(&ss.Scope)
  154. case "spans":
  155. err = decoder.Decode(&ss.Spans)
  156. case "schemaUrl", "schema_url":
  157. err = decoder.Decode(&ss.SchemaURL)
  158. default:
  159. // Skip unknown.
  160. }
  161. if err != nil {
  162. return err
  163. }
  164. }
  165. return nil
  166. }