opencensus_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package opencensusexporter
  4. import (
  5. "context"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/component/componenttest"
  11. "go.opentelemetry.io/collector/config/configgrpc"
  12. "go.opentelemetry.io/collector/config/configtls"
  13. "go.opentelemetry.io/collector/consumer/consumertest"
  14. "go.opentelemetry.io/collector/exporter/exportertest"
  15. "go.opentelemetry.io/collector/pdata/ptrace"
  16. "go.opentelemetry.io/collector/receiver/receivertest"
  17. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil"
  18. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata"
  19. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/opencensusreceiver"
  20. )
  21. func TestSendTraces(t *testing.T) {
  22. sink := new(consumertest.TracesSink)
  23. rFactory := opencensusreceiver.NewFactory()
  24. rCfg := rFactory.CreateDefaultConfig().(*opencensusreceiver.Config)
  25. endpoint := testutil.GetAvailableLocalAddress(t)
  26. rCfg.GRPCServerSettings.NetAddr.Endpoint = endpoint
  27. set := receivertest.NewNopCreateSettings()
  28. recv, err := rFactory.CreateTracesReceiver(context.Background(), set, rCfg, sink)
  29. assert.NoError(t, err)
  30. assert.NoError(t, recv.Start(context.Background(), componenttest.NewNopHost()))
  31. t.Cleanup(func() {
  32. assert.NoError(t, recv.Shutdown(context.Background()))
  33. })
  34. factory := NewFactory()
  35. cfg := factory.CreateDefaultConfig().(*Config)
  36. cfg.GRPCClientSettings = configgrpc.GRPCClientSettings{
  37. Endpoint: endpoint,
  38. TLSSetting: configtls.TLSClientSetting{
  39. Insecure: true,
  40. },
  41. }
  42. cfg.NumWorkers = 1
  43. exp, err := factory.CreateTracesExporter(context.Background(), exportertest.NewNopCreateSettings(), cfg)
  44. require.NoError(t, err)
  45. require.NotNil(t, exp)
  46. host := componenttest.NewNopHost()
  47. require.NoError(t, exp.Start(context.Background(), host))
  48. t.Cleanup(func() {
  49. assert.NoError(t, exp.Shutdown(context.Background()))
  50. })
  51. td := testdata.GenerateTracesOneSpan()
  52. assert.NoError(t, exp.ConsumeTraces(context.Background(), td))
  53. assert.Eventually(t, func() bool {
  54. return len(sink.AllTraces()) == 1
  55. }, 10*time.Second, 5*time.Millisecond)
  56. traces := sink.AllTraces()
  57. require.Len(t, traces, 1)
  58. assert.Equal(t, td, traces[0])
  59. sink.Reset()
  60. // Sending data no Node.
  61. td.ResourceSpans().At(0).Resource().Attributes().Clear()
  62. newData := ptrace.NewTraces()
  63. td.CopyTo(newData)
  64. assert.NoError(t, exp.ConsumeTraces(context.Background(), newData))
  65. assert.Eventually(t, func() bool {
  66. return len(sink.AllTraces()) == 1
  67. }, 10*time.Second, 5*time.Millisecond)
  68. traces = sink.AllTraces()
  69. require.Len(t, traces, 1)
  70. assert.EqualValues(t, newData, traces[0])
  71. }
  72. func TestSendTraces_NoBackend(t *testing.T) {
  73. factory := NewFactory()
  74. cfg := factory.CreateDefaultConfig().(*Config)
  75. cfg.GRPCClientSettings = configgrpc.GRPCClientSettings{
  76. Endpoint: "localhost:56569",
  77. TLSSetting: configtls.TLSClientSetting{
  78. Insecure: true,
  79. },
  80. }
  81. exp, err := factory.CreateTracesExporter(context.Background(), exportertest.NewNopCreateSettings(), cfg)
  82. require.NoError(t, err)
  83. require.NotNil(t, exp)
  84. host := componenttest.NewNopHost()
  85. require.NoError(t, exp.Start(context.Background(), host))
  86. t.Cleanup(func() {
  87. assert.NoError(t, exp.Shutdown(context.Background()))
  88. })
  89. td := testdata.GenerateTracesOneSpan()
  90. for i := 0; i < 10000; i++ {
  91. assert.Error(t, exp.ConsumeTraces(context.Background(), td))
  92. }
  93. }
  94. func TestSendTraces_AfterStop(t *testing.T) {
  95. factory := NewFactory()
  96. cfg := factory.CreateDefaultConfig().(*Config)
  97. cfg.GRPCClientSettings = configgrpc.GRPCClientSettings{
  98. Endpoint: "localhost:56569",
  99. TLSSetting: configtls.TLSClientSetting{
  100. Insecure: true,
  101. },
  102. }
  103. exp, err := factory.CreateTracesExporter(context.Background(), exportertest.NewNopCreateSettings(), cfg)
  104. require.NoError(t, err)
  105. require.NotNil(t, exp)
  106. host := componenttest.NewNopHost()
  107. require.NoError(t, exp.Start(context.Background(), host))
  108. assert.NoError(t, exp.Shutdown(context.Background()))
  109. td := testdata.GenerateTracesOneSpan()
  110. assert.Error(t, exp.ConsumeTraces(context.Background(), td))
  111. }
  112. func TestSendMetrics(t *testing.T) {
  113. sink := new(consumertest.MetricsSink)
  114. rFactory := opencensusreceiver.NewFactory()
  115. rCfg := rFactory.CreateDefaultConfig().(*opencensusreceiver.Config)
  116. endpoint := testutil.GetAvailableLocalAddress(t)
  117. rCfg.GRPCServerSettings.NetAddr.Endpoint = endpoint
  118. set := receivertest.NewNopCreateSettings()
  119. recv, err := rFactory.CreateMetricsReceiver(context.Background(), set, rCfg, sink)
  120. assert.NoError(t, err)
  121. assert.NoError(t, recv.Start(context.Background(), componenttest.NewNopHost()))
  122. t.Cleanup(func() {
  123. assert.NoError(t, recv.Shutdown(context.Background()))
  124. })
  125. factory := NewFactory()
  126. cfg := factory.CreateDefaultConfig().(*Config)
  127. cfg.GRPCClientSettings = configgrpc.GRPCClientSettings{
  128. Endpoint: endpoint,
  129. TLSSetting: configtls.TLSClientSetting{
  130. Insecure: true,
  131. },
  132. }
  133. cfg.NumWorkers = 1
  134. exp, err := factory.CreateMetricsExporter(context.Background(), exportertest.NewNopCreateSettings(), cfg)
  135. require.NoError(t, err)
  136. require.NotNil(t, exp)
  137. host := componenttest.NewNopHost()
  138. require.NoError(t, exp.Start(context.Background(), host))
  139. t.Cleanup(func() {
  140. assert.NoError(t, exp.Shutdown(context.Background()))
  141. })
  142. md := testdata.GenerateMetricsOneMetric()
  143. assert.NoError(t, exp.ConsumeMetrics(context.Background(), md))
  144. assert.Eventually(t, func() bool {
  145. return len(sink.AllMetrics()) == 1
  146. }, 10*time.Second, 5*time.Millisecond)
  147. metrics := sink.AllMetrics()
  148. require.Len(t, metrics, 1)
  149. assert.Equal(t, md, metrics[0])
  150. // Sending data no node.
  151. sink.Reset()
  152. md.ResourceMetrics().At(0).Resource().Attributes().Clear()
  153. assert.NoError(t, exp.ConsumeMetrics(context.Background(), md))
  154. assert.Eventually(t, func() bool {
  155. return len(sink.AllMetrics()) == 1
  156. }, 10*time.Second, 5*time.Millisecond)
  157. metrics = sink.AllMetrics()
  158. require.Len(t, metrics, 1)
  159. assert.Equal(t, md, metrics[0])
  160. }
  161. func TestSendMetrics_NoBackend(t *testing.T) {
  162. factory := NewFactory()
  163. cfg := factory.CreateDefaultConfig().(*Config)
  164. cfg.GRPCClientSettings = configgrpc.GRPCClientSettings{
  165. Endpoint: "localhost:56569",
  166. TLSSetting: configtls.TLSClientSetting{
  167. Insecure: true,
  168. },
  169. }
  170. exp, err := factory.CreateMetricsExporter(context.Background(), exportertest.NewNopCreateSettings(), cfg)
  171. require.NoError(t, err)
  172. require.NotNil(t, exp)
  173. host := componenttest.NewNopHost()
  174. require.NoError(t, exp.Start(context.Background(), host))
  175. t.Cleanup(func() {
  176. assert.NoError(t, exp.Shutdown(context.Background()))
  177. })
  178. md := testdata.GenerateMetricsOneMetric()
  179. for i := 0; i < 10000; i++ {
  180. assert.Error(t, exp.ConsumeMetrics(context.Background(), md))
  181. }
  182. }
  183. func TestSendMetrics_AfterStop(t *testing.T) {
  184. factory := NewFactory()
  185. cfg := factory.CreateDefaultConfig().(*Config)
  186. cfg.GRPCClientSettings = configgrpc.GRPCClientSettings{
  187. Endpoint: "localhost:56569",
  188. TLSSetting: configtls.TLSClientSetting{
  189. Insecure: true,
  190. },
  191. }
  192. exp, err := factory.CreateMetricsExporter(context.Background(), exportertest.NewNopCreateSettings(), cfg)
  193. require.NoError(t, err)
  194. require.NotNil(t, exp)
  195. host := componenttest.NewNopHost()
  196. require.NoError(t, exp.Start(context.Background(), host))
  197. assert.NoError(t, exp.Shutdown(context.Background()))
  198. md := testdata.GenerateMetricsOneMetric()
  199. assert.Error(t, exp.ConsumeMetrics(context.Background(), md))
  200. }