golden.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package golden // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "os"
  8. "strings"
  9. "testing"
  10. "go.opentelemetry.io/collector/pdata/plog"
  11. "go.opentelemetry.io/collector/pdata/pmetric"
  12. "go.opentelemetry.io/collector/pdata/ptrace"
  13. "gopkg.in/yaml.v3"
  14. )
  15. // ReadMetrics reads a pmetric.Metrics from the specified YAML or JSON file.
  16. func ReadMetrics(filePath string) (pmetric.Metrics, error) {
  17. b, err := os.ReadFile(filePath)
  18. if err != nil {
  19. return pmetric.Metrics{}, err
  20. }
  21. if strings.HasSuffix(filePath, ".yaml") || strings.HasSuffix(filePath, ".yml") {
  22. var m map[string]any
  23. if err = yaml.Unmarshal(b, &m); err != nil {
  24. return pmetric.Metrics{}, err
  25. }
  26. b, err = json.Marshal(m)
  27. if err != nil {
  28. return pmetric.Metrics{}, err
  29. }
  30. }
  31. unmarshaller := &pmetric.JSONUnmarshaler{}
  32. return unmarshaller.UnmarshalMetrics(b)
  33. }
  34. // WriteMetrics writes a pmetric.Metrics to the specified file in YAML format.
  35. func WriteMetrics(t *testing.T, filePath string, metrics pmetric.Metrics) error {
  36. if err := writeMetrics(filePath, metrics); err != nil {
  37. return err
  38. }
  39. t.Logf("Golden file successfully written to %s.", filePath)
  40. t.Log("NOTE: The WriteMetrics call must be removed in order to pass the test.")
  41. t.Fail()
  42. return nil
  43. }
  44. // MarshalMetricsYAML marshals a pmetric.Metrics to YAML format.
  45. func MarshalMetricsYAML(metrics pmetric.Metrics) ([]byte, error) {
  46. unmarshaler := &pmetric.JSONMarshaler{}
  47. fileBytes, err := unmarshaler.MarshalMetrics(metrics)
  48. if err != nil {
  49. return nil, err
  50. }
  51. var jsonVal map[string]any
  52. if err = json.Unmarshal(fileBytes, &jsonVal); err != nil {
  53. return nil, err
  54. }
  55. b := &bytes.Buffer{}
  56. enc := yaml.NewEncoder(b)
  57. enc.SetIndent(2)
  58. if err := enc.Encode(jsonVal); err != nil {
  59. return nil, err
  60. }
  61. return b.Bytes(), nil
  62. }
  63. // writeMetrics writes a pmetric.Metrics to the specified file in YAML format.
  64. func writeMetrics(filePath string, metrics pmetric.Metrics) error {
  65. sortMetrics(metrics)
  66. normalizeTimestamps(metrics)
  67. b, err := MarshalMetricsYAML(metrics)
  68. if err != nil {
  69. return err
  70. }
  71. return os.WriteFile(filePath, b, 0600)
  72. }
  73. // ReadLogs reads a plog.Logs from the specified YAML or JSON file.
  74. func ReadLogs(filePath string) (plog.Logs, error) {
  75. b, err := os.ReadFile(filePath)
  76. if err != nil {
  77. return plog.Logs{}, err
  78. }
  79. if strings.HasSuffix(filePath, ".yaml") || strings.HasSuffix(filePath, ".yml") {
  80. var m map[string]any
  81. if err = yaml.Unmarshal(b, &m); err != nil {
  82. return plog.Logs{}, err
  83. }
  84. b, err = json.Marshal(m)
  85. if err != nil {
  86. return plog.Logs{}, err
  87. }
  88. }
  89. unmarshaler := plog.JSONUnmarshaler{}
  90. return unmarshaler.UnmarshalLogs(b)
  91. }
  92. // WriteLogs writes a plog.Logs to the specified file in YAML format.
  93. func WriteLogs(t *testing.T, filePath string, logs plog.Logs) error {
  94. if err := writeLogs(filePath, logs); err != nil {
  95. return err
  96. }
  97. t.Logf("Golden file successfully written to %s.", filePath)
  98. t.Log("NOTE: The WriteLogs call must be removed in order to pass the test.")
  99. t.Fail()
  100. return nil
  101. }
  102. // writeLogs writes a plog.Logs to the specified file in YAML format.
  103. func writeLogs(filePath string, logs plog.Logs) error {
  104. unmarshaler := &plog.JSONMarshaler{}
  105. fileBytes, err := unmarshaler.MarshalLogs(logs)
  106. if err != nil {
  107. return err
  108. }
  109. var jsonVal map[string]any
  110. if err = json.Unmarshal(fileBytes, &jsonVal); err != nil {
  111. return err
  112. }
  113. b := &bytes.Buffer{}
  114. enc := yaml.NewEncoder(b)
  115. enc.SetIndent(2)
  116. if err := enc.Encode(jsonVal); err != nil {
  117. return err
  118. }
  119. return os.WriteFile(filePath, b.Bytes(), 0600)
  120. }
  121. // ReadTraces reads a ptrace.Traces from the specified YAML or JSON file.
  122. func ReadTraces(filePath string) (ptrace.Traces, error) {
  123. b, err := os.ReadFile(filePath)
  124. if err != nil {
  125. return ptrace.Traces{}, err
  126. }
  127. if strings.HasSuffix(filePath, ".yaml") || strings.HasSuffix(filePath, ".yml") {
  128. var m map[string]any
  129. if err = yaml.Unmarshal(b, &m); err != nil {
  130. return ptrace.Traces{}, err
  131. }
  132. b, err = json.Marshal(m)
  133. if err != nil {
  134. return ptrace.Traces{}, err
  135. }
  136. }
  137. unmarshaler := ptrace.JSONUnmarshaler{}
  138. return unmarshaler.UnmarshalTraces(b)
  139. }
  140. // WriteTraces writes a ptrace.Traces to the specified file in YAML format.
  141. func WriteTraces(t *testing.T, filePath string, traces ptrace.Traces) error {
  142. if err := writeTraces(filePath, traces); err != nil {
  143. return err
  144. }
  145. t.Logf("Golden file successfully written to %s.", filePath)
  146. t.Log("NOTE: The WriteTraces call must be removed in order to pass the test.")
  147. t.Fail()
  148. return nil
  149. }
  150. // writeTraces writes a ptrace.Traces to the specified file
  151. func writeTraces(filePath string, traces ptrace.Traces) error {
  152. unmarshaler := &ptrace.JSONMarshaler{}
  153. fileBytes, err := unmarshaler.MarshalTraces(traces)
  154. if err != nil {
  155. return err
  156. }
  157. var jsonVal map[string]any
  158. if err = json.Unmarshal(fileBytes, &jsonVal); err != nil {
  159. return err
  160. }
  161. b := &bytes.Buffer{}
  162. enc := yaml.NewEncoder(b)
  163. enc.SetIndent(2)
  164. if err := enc.Encode(jsonVal); err != nil {
  165. return err
  166. }
  167. return os.WriteFile(filePath, b.Bytes(), 0600)
  168. }