utils.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package correctnesstests // import "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/correctnesstests"
  4. import (
  5. "bufio"
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "testing"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/datareceivers"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/datasenders"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/testbed"
  14. )
  15. // CreateConfigYaml creates a yaml config for an otel collector given a testbed sender, testbed receiver, any
  16. // processors, and a pipeline type. A collector created from the resulting yaml string should be able to talk
  17. // the specified sender and receiver.
  18. func CreateConfigYaml(
  19. sender testbed.DataSender,
  20. receiver testbed.DataReceiver,
  21. processors map[string]string,
  22. pipelineType string,
  23. ) string {
  24. // Prepare extra processor config section and comma-separated list of extra processor
  25. // names to use in corresponding "processors" settings.
  26. processorsSections := ""
  27. processorsList := ""
  28. if len(processors) > 0 {
  29. first := true
  30. for name, cfg := range processors {
  31. processorsSections += cfg + "\n"
  32. if !first {
  33. processorsList += ","
  34. }
  35. processorsList += name
  36. first = false
  37. }
  38. }
  39. format := `
  40. receivers:%v
  41. exporters:%v
  42. processors:
  43. %s
  44. extensions:
  45. service:
  46. extensions:
  47. pipelines:
  48. %s:
  49. receivers: [%v]
  50. processors: [%s]
  51. exporters: [%v]
  52. `
  53. return fmt.Sprintf(
  54. format,
  55. sender.GenConfigYAMLStr(),
  56. receiver.GenConfigYAMLStr(),
  57. processorsSections,
  58. pipelineType,
  59. sender.ProtocolName(),
  60. processorsList,
  61. receiver.ProtocolName(),
  62. )
  63. }
  64. // PipelineDef holds the information necessary to run a single testbed configuration.
  65. type PipelineDef struct {
  66. Receiver string
  67. Exporter string
  68. TestName string
  69. DataSender testbed.DataSender
  70. DataReceiver testbed.DataReceiver
  71. ResourceSpec testbed.ResourceSpec
  72. }
  73. // LoadPictOutputPipelineDefs generates a slice of PipelineDefs from the passed-in generated PICT file. The
  74. // result should be a set of PipelineDefs that covers all possible pipeline configurations.
  75. func LoadPictOutputPipelineDefs(fileName string) ([]PipelineDef, error) {
  76. file, err := os.Open(filepath.Clean(fileName))
  77. if err != nil {
  78. return nil, err
  79. }
  80. defer func() {
  81. cerr := file.Close()
  82. if err == nil {
  83. err = cerr
  84. }
  85. }()
  86. var defs []PipelineDef
  87. scanner := bufio.NewScanner(file)
  88. for scanner.Scan() {
  89. s := strings.Split(scanner.Text(), "\t")
  90. if s[0] == "Receiver" {
  91. continue
  92. }
  93. defs = append(defs, PipelineDef{Receiver: s[0], Exporter: s[1]})
  94. }
  95. return defs, err
  96. }
  97. // ConstructTraceSender creates a testbed trace sender from the passed-in trace sender identifier.
  98. func ConstructTraceSender(t *testing.T, receiver string) testbed.DataSender {
  99. var sender testbed.DataSender
  100. switch receiver {
  101. case "otlp":
  102. sender = testbed.NewOTLPTraceDataSender(testbed.DefaultHost, testbed.GetAvailablePort(t))
  103. case "opencensus":
  104. sender = datasenders.NewOCTraceDataSender(testbed.DefaultHost, testbed.GetAvailablePort(t))
  105. case "jaeger":
  106. sender = datasenders.NewJaegerGRPCDataSender(testbed.DefaultHost, testbed.GetAvailablePort(t))
  107. case "zipkin":
  108. sender = datasenders.NewZipkinDataSender(testbed.DefaultHost, testbed.GetAvailablePort(t))
  109. default:
  110. t.Errorf("unknown receiver type: %s", receiver)
  111. }
  112. return sender
  113. }
  114. // ConstructMetricsSender creates a testbed metrics sender from the passed-in metrics sender identifier.
  115. func ConstructMetricsSender(t *testing.T, receiver string) testbed.MetricDataSender {
  116. var sender testbed.MetricDataSender
  117. switch receiver {
  118. case "otlp":
  119. sender = testbed.NewOTLPMetricDataSender(testbed.DefaultHost, testbed.GetAvailablePort(t))
  120. case "opencensus":
  121. sender = datasenders.NewOCMetricDataSender(testbed.DefaultHost, testbed.GetAvailablePort(t))
  122. case "prometheus":
  123. sender = datasenders.NewPrometheusDataSender(testbed.DefaultHost, testbed.GetAvailablePort(t))
  124. default:
  125. t.Errorf("unknown receiver type: %s", receiver)
  126. }
  127. return sender
  128. }
  129. // ConstructReceiver creates a testbed receiver from the passed-in recevier identifier.
  130. func ConstructReceiver(t *testing.T, exporter string) testbed.DataReceiver {
  131. var receiver testbed.DataReceiver
  132. switch exporter {
  133. case "otlp":
  134. receiver = testbed.NewOTLPDataReceiver(testbed.GetAvailablePort(t))
  135. case "opencensus":
  136. receiver = datareceivers.NewOCDataReceiver(testbed.GetAvailablePort(t))
  137. case "jaeger":
  138. receiver = datareceivers.NewJaegerDataReceiver(testbed.GetAvailablePort(t))
  139. case "zipkin":
  140. receiver = datareceivers.NewZipkinDataReceiver(testbed.GetAvailablePort(t))
  141. case "prometheus":
  142. receiver = datareceivers.NewPrometheusDataReceiver(testbed.GetAvailablePort(t))
  143. default:
  144. t.Errorf("unknown exporter type: %s", exporter)
  145. }
  146. return receiver
  147. }