encoder.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package opensearchexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/opensearchexporter"
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "time"
  8. "go.opentelemetry.io/collector/pdata/pcommon"
  9. "go.opentelemetry.io/collector/pdata/plog"
  10. "go.opentelemetry.io/collector/pdata/ptrace"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/opensearchexporter/internal/objmodel"
  12. )
  13. type mappingModel interface {
  14. encodeLog(resource pcommon.Resource,
  15. scope pcommon.InstrumentationScope,
  16. schemaURL string,
  17. record plog.LogRecord) ([]byte, error)
  18. encodeTrace(resource pcommon.Resource,
  19. scope pcommon.InstrumentationScope,
  20. schemaURL string,
  21. record ptrace.Span) ([]byte, error)
  22. }
  23. // encodeModel supports multiple encoding OpenTelemetry signals to multiple schemas.
  24. type encodeModel struct {
  25. dedup bool
  26. dedot bool
  27. sso bool
  28. flattenAttributes bool
  29. timestampField string
  30. unixTime bool
  31. dataset string
  32. namespace string
  33. }
  34. func (m *encodeModel) encodeLog(resource pcommon.Resource,
  35. scope pcommon.InstrumentationScope,
  36. schemaURL string,
  37. record plog.LogRecord) ([]byte, error) {
  38. if m.sso {
  39. return m.encodeLogSSO(resource, scope, schemaURL, record)
  40. }
  41. return m.encodeLogDataModel(resource, record)
  42. }
  43. // encodeLogSSO encodes a plog.LogRecord following the Simple Schema for Observability.
  44. // See: https://github.com/opensearch-project/opensearch-catalog/tree/main/docs/schema/observability
  45. func (m *encodeModel) encodeLogSSO(
  46. resource pcommon.Resource,
  47. scope pcommon.InstrumentationScope,
  48. schemaURL string,
  49. record plog.LogRecord,
  50. ) ([]byte, error) {
  51. sso := ssoRecord{}
  52. sso.Attributes = record.Attributes().AsRaw()
  53. sso.Body = record.Body().AsString()
  54. now := time.Now()
  55. ts := record.Timestamp().AsTime()
  56. sso.ObservedTimestamp = &now
  57. sso.Timestamp = &ts
  58. sso.Resource = attributesToMapString(resource.Attributes())
  59. sso.SchemaURL = schemaURL
  60. sso.SpanID = record.SpanID().String()
  61. sso.TraceID = record.TraceID().String()
  62. ds := dataStream{}
  63. if m.dataset != "" {
  64. ds.Dataset = m.dataset
  65. }
  66. if m.namespace != "" {
  67. ds.Namespace = m.namespace
  68. }
  69. if ds != (dataStream{}) {
  70. ds.Type = "record"
  71. sso.Attributes["data_stream"] = ds
  72. }
  73. sso.InstrumentationScope.Name = scope.Name()
  74. sso.InstrumentationScope.Version = scope.Version()
  75. sso.InstrumentationScope.SchemaURL = schemaURL
  76. sso.InstrumentationScope.Attributes = scope.Attributes().AsRaw()
  77. sso.Severity.Text = record.SeverityText()
  78. sso.Severity.Number = int64(record.SeverityNumber())
  79. return json.Marshal(sso)
  80. }
  81. // encodeLogDataModel encodes a plog.LogRecord following the Log Data Model.
  82. // See: https://github.com/open-telemetry/oteps/blob/main/text/logs/0097-log-data-model.md
  83. func (m *encodeModel) encodeLogDataModel(resource pcommon.Resource, record plog.LogRecord) ([]byte, error) {
  84. var document objmodel.Document
  85. if m.flattenAttributes {
  86. document = objmodel.DocumentFromAttributes(resource.Attributes())
  87. } else {
  88. document.AddAttributes("Attributes", resource.Attributes())
  89. }
  90. timestampField := "@timestamp"
  91. if m.timestampField != "" {
  92. timestampField = m.timestampField
  93. }
  94. if m.unixTime {
  95. document.AddInt(timestampField, epochMilliTimestamp(record))
  96. } else {
  97. document.AddTimestamp(timestampField, record.Timestamp())
  98. }
  99. document.AddTraceID("TraceId", record.TraceID())
  100. document.AddSpanID("SpanId", record.SpanID())
  101. document.AddInt("TraceFlags", int64(record.Flags()))
  102. document.AddString("SeverityText", record.SeverityText())
  103. document.AddInt("SeverityNumber", int64(record.SeverityNumber()))
  104. document.AddAttribute("Body", record.Body())
  105. if m.flattenAttributes {
  106. document.AddAttributes("", record.Attributes())
  107. } else {
  108. document.AddAttributes("Attributes", record.Attributes())
  109. }
  110. if m.dedup {
  111. document.Dedup()
  112. } else if m.dedot {
  113. document.Sort()
  114. }
  115. var buf bytes.Buffer
  116. err := document.Serialize(&buf, m.dedot)
  117. return buf.Bytes(), err
  118. }
  119. // encodeTrace encodes a ptrace.Span following the Simple Schema For Observability
  120. // See: https://github.com/opensearch-project/opensearch-catalog/tree/main/docs/schema/observability
  121. func (m *encodeModel) encodeTrace(
  122. resource pcommon.Resource,
  123. scope pcommon.InstrumentationScope,
  124. schemaURL string,
  125. span ptrace.Span,
  126. ) ([]byte, error) {
  127. sso := ssoSpan{}
  128. sso.Attributes = span.Attributes().AsRaw()
  129. sso.DroppedAttributesCount = span.DroppedAttributesCount()
  130. sso.DroppedEventsCount = span.DroppedEventsCount()
  131. sso.DroppedLinksCount = span.DroppedLinksCount()
  132. sso.EndTime = span.EndTimestamp().AsTime()
  133. sso.Kind = span.Kind().String()
  134. sso.Name = span.Name()
  135. sso.ParentSpanID = span.ParentSpanID().String()
  136. sso.Resource = attributesToMapString(resource.Attributes())
  137. sso.SpanID = span.SpanID().String()
  138. sso.StartTime = span.StartTimestamp().AsTime()
  139. sso.Status.Code = span.Status().Code().String()
  140. sso.Status.Message = span.Status().Message()
  141. sso.TraceID = span.TraceID().String()
  142. sso.TraceState = span.TraceState().AsRaw()
  143. if span.Events().Len() > 0 {
  144. sso.Events = make([]ssoSpanEvent, span.Events().Len())
  145. for i := 0; i < span.Events().Len(); i++ {
  146. e := span.Events().At(i)
  147. ssoEvent := &sso.Events[i]
  148. ssoEvent.Attributes = e.Attributes().AsRaw()
  149. ssoEvent.DroppedAttributesCount = e.DroppedAttributesCount()
  150. ssoEvent.Name = e.Name()
  151. ts := e.Timestamp().AsTime()
  152. if ts.Unix() != 0 {
  153. ssoEvent.Timestamp = &ts
  154. } else {
  155. now := time.Now()
  156. ssoEvent.ObservedTimestamp = &now
  157. }
  158. }
  159. }
  160. ds := dataStream{}
  161. if m.dataset != "" {
  162. ds.Dataset = m.dataset
  163. }
  164. if m.namespace != "" {
  165. ds.Namespace = m.namespace
  166. }
  167. if ds != (dataStream{}) {
  168. ds.Type = "span"
  169. sso.Attributes["data_stream"] = ds
  170. }
  171. sso.InstrumentationScope.Name = scope.Name()
  172. sso.InstrumentationScope.DroppedAttributesCount = scope.DroppedAttributesCount()
  173. sso.InstrumentationScope.Version = scope.Version()
  174. sso.InstrumentationScope.SchemaURL = schemaURL
  175. sso.InstrumentationScope.Attributes = scope.Attributes().AsRaw()
  176. if span.Links().Len() > 0 {
  177. sso.Links = make([]ssoSpanLinks, span.Links().Len())
  178. for i := 0; i < span.Links().Len(); i++ {
  179. link := span.Links().At(i)
  180. ssoLink := &sso.Links[i]
  181. ssoLink.Attributes = link.Attributes().AsRaw()
  182. ssoLink.DroppedAttributesCount = link.DroppedAttributesCount()
  183. ssoLink.TraceID = link.TraceID().String()
  184. ssoLink.TraceState = link.TraceState().AsRaw()
  185. ssoLink.SpanID = link.SpanID().String()
  186. }
  187. }
  188. return json.Marshal(sso)
  189. }
  190. func epochMilliTimestamp(record plog.LogRecord) int64 {
  191. return record.Timestamp().AsTime().UnixMilli()
  192. }