logrecord_to_logdata.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package skywalkingexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/skywalkingexporter"
  4. import (
  5. "encoding/hex"
  6. "strconv"
  7. "time"
  8. "go.opentelemetry.io/collector/pdata/pcommon"
  9. "go.opentelemetry.io/collector/pdata/plog"
  10. conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
  11. common "skywalking.apache.org/repo/goapi/collect/common/v3"
  12. logpb "skywalking.apache.org/repo/goapi/collect/logging/v3"
  13. )
  14. const (
  15. spanIDField = "spanID"
  16. severityNumber = "severityNumber"
  17. severityText = "severityText"
  18. flags = "flags"
  19. instrumentationName = "otlp.name"
  20. instrumentationVersion = "otlp.version"
  21. defaultServiceName = "otel-collector"
  22. )
  23. func logRecordToLogData(ld plog.Logs) []*logpb.LogData {
  24. var lds []*logpb.LogData
  25. rls := ld.ResourceLogs()
  26. for i := 0; i < rls.Len(); i++ {
  27. rl := rls.At(i)
  28. ills := rl.ScopeLogs()
  29. resource := rl.Resource()
  30. for j := 0; j < ills.Len(); j++ {
  31. ils := ills.At(j)
  32. logs := ils.LogRecords()
  33. for k := 0; k < logs.Len(); k++ {
  34. logData := &logpb.LogData{}
  35. logData.Tags = &logpb.LogTags{}
  36. resourceToLogData(resource, logData)
  37. instrumentationLibraryToLogData(ils.Scope(), logData)
  38. mapLogRecordToLogData(logs.At(k), logData)
  39. lds = append(lds, logData)
  40. }
  41. }
  42. }
  43. return lds
  44. }
  45. func resourceToLogData(resource pcommon.Resource, logData *logpb.LogData) {
  46. attrs := resource.Attributes()
  47. if serviceName, ok := attrs.Get(conventions.AttributeServiceName); ok {
  48. logData.Service = serviceName.AsString()
  49. } else {
  50. logData.Service = defaultServiceName
  51. }
  52. if serviceInstanceID, ok := attrs.Get(conventions.AttributeServiceInstanceID); ok {
  53. logData.ServiceInstance = serviceInstanceID.AsString()
  54. }
  55. attrs.Range(func(k string, v pcommon.Value) bool {
  56. logData.Tags.Data = append(logData.Tags.Data, &common.KeyStringValuePair{
  57. Key: k,
  58. Value: v.AsString(),
  59. })
  60. return true
  61. })
  62. }
  63. func instrumentationLibraryToLogData(scope pcommon.InstrumentationScope, logData *logpb.LogData) {
  64. if nameValue := scope.Name(); nameValue != "" {
  65. logData.Tags.Data = append(logData.Tags.Data, &common.KeyStringValuePair{
  66. Key: instrumentationName,
  67. Value: nameValue,
  68. })
  69. }
  70. if version := scope.Version(); version != "" {
  71. logData.Tags.Data = append(logData.Tags.Data, &common.KeyStringValuePair{
  72. Key: instrumentationVersion,
  73. Value: version,
  74. })
  75. }
  76. }
  77. func mapLogRecordToLogData(lr plog.LogRecord, logData *logpb.LogData) {
  78. if lr.Body().Type() == pcommon.ValueTypeEmpty {
  79. return
  80. }
  81. if timestamp := lr.Timestamp(); timestamp > 0 {
  82. logData.Timestamp = lr.Timestamp().AsTime().UnixNano() / int64(time.Millisecond)
  83. }
  84. if sn := strconv.FormatInt(int64(lr.SeverityNumber()), 10); sn != "" {
  85. logData.Tags.Data = append(logData.Tags.Data, &common.KeyStringValuePair{
  86. Key: severityNumber,
  87. Value: sn,
  88. })
  89. }
  90. if st := lr.SeverityText(); st != "" {
  91. logData.Tags.Data = append(logData.Tags.Data, &common.KeyStringValuePair{
  92. Key: severityText,
  93. Value: st,
  94. })
  95. }
  96. lr.Attributes().Range(func(k string, v pcommon.Value) bool {
  97. logData.Tags.Data = append(logData.Tags.Data, &common.KeyStringValuePair{
  98. Key: k,
  99. Value: v.AsString(),
  100. })
  101. return true
  102. })
  103. logData.Body = &logpb.LogDataBody{
  104. Type: "body-type",
  105. Content: &logpb.LogDataBody_Text{
  106. Text: &logpb.TextLog{
  107. Text: lr.Body().AsString(),
  108. }},
  109. }
  110. if flag := strconv.FormatUint(uint64(lr.Flags()), 16); flag != "" {
  111. logData.Tags.Data = append(logData.Tags.Data, &common.KeyStringValuePair{
  112. Key: flags,
  113. Value: flag,
  114. })
  115. }
  116. if traceID := lr.TraceID(); !traceID.IsEmpty() {
  117. logData.TraceContext = &logpb.TraceContext{TraceId: hex.EncodeToString(traceID[:])}
  118. }
  119. if spanID := lr.SpanID(); !spanID.IsEmpty() {
  120. logData.Tags.Data = append(logData.Tags.Data, &common.KeyStringValuePair{
  121. Key: spanIDField,
  122. Value: hex.EncodeToString(spanID[:]),
  123. })
  124. }
  125. }