exporter_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlecloudpubsubexporter
  4. import (
  5. "context"
  6. "testing"
  7. "time"
  8. pb "cloud.google.com/go/pubsub/apiv1/pubsubpb"
  9. "cloud.google.com/go/pubsub/pstest"
  10. "github.com/stretchr/testify/assert"
  11. "go.opentelemetry.io/collector/exporter/exporterhelper"
  12. "go.opentelemetry.io/collector/exporter/exportertest"
  13. "go.opentelemetry.io/collector/pdata/plog"
  14. "go.opentelemetry.io/collector/pdata/pmetric"
  15. "go.opentelemetry.io/collector/pdata/ptrace"
  16. "google.golang.org/api/option"
  17. )
  18. func TestName(t *testing.T) {
  19. exporter := &pubsubExporter{}
  20. assert.Equal(t, "googlecloudpubsub", exporter.Name())
  21. }
  22. func TestGenerateClientOptions(t *testing.T) {
  23. // Start a fake server running locally.
  24. srv := pstest.NewServer()
  25. defer srv.Close()
  26. factory := NewFactory()
  27. cfg := factory.CreateDefaultConfig()
  28. exporterConfig := cfg.(*Config)
  29. exporterConfig.endpoint = srv.Addr
  30. exporterConfig.UserAgent = "test-user-agent"
  31. exporterConfig.insecure = true
  32. exporterConfig.ProjectID = "my-project"
  33. exporterConfig.Topic = "projects/my-project/topics/otlp"
  34. exporterConfig.TimeoutSettings = exporterhelper.TimeoutSettings{
  35. Timeout: 12 * time.Second,
  36. }
  37. exporter := ensureExporter(exportertest.NewNopCreateSettings(), exporterConfig)
  38. options := exporter.generateClientOptions()
  39. assert.Equal(t, option.WithUserAgent("test-user-agent"), options[0])
  40. exporter.config.insecure = false
  41. options = exporter.generateClientOptions()
  42. assert.Equal(t, option.WithUserAgent("test-user-agent"), options[0])
  43. assert.Equal(t, option.WithEndpoint(srv.Addr), options[1])
  44. }
  45. func TestExporterDefaultSettings(t *testing.T) {
  46. ctx := context.Background()
  47. // Start a fake server running locally.
  48. srv := pstest.NewServer()
  49. defer srv.Close()
  50. _, err := srv.GServer.CreateTopic(ctx, &pb.Topic{
  51. Name: "projects/my-project/topics/otlp",
  52. })
  53. assert.NoError(t, err)
  54. factory := NewFactory()
  55. cfg := factory.CreateDefaultConfig()
  56. exporterConfig := cfg.(*Config)
  57. exporterConfig.endpoint = srv.Addr
  58. exporterConfig.insecure = true
  59. exporterConfig.ProjectID = "my-project"
  60. exporterConfig.Topic = "projects/my-project/topics/otlp"
  61. exporterConfig.TimeoutSettings = exporterhelper.TimeoutSettings{
  62. Timeout: 12 * time.Second,
  63. }
  64. exporter := ensureExporter(exportertest.NewNopCreateSettings(), exporterConfig)
  65. assert.NoError(t, exporter.start(ctx, nil))
  66. assert.NoError(t, exporter.consumeTraces(ctx, ptrace.NewTraces()))
  67. assert.NoError(t, exporter.consumeMetrics(ctx, pmetric.NewMetrics()))
  68. assert.NoError(t, exporter.consumeLogs(ctx, plog.NewLogs()))
  69. assert.NoError(t, exporter.shutdown(ctx))
  70. }
  71. func TestExporterCompression(t *testing.T) {
  72. ctx := context.Background()
  73. // Start a fake server running locally.
  74. srv := pstest.NewServer()
  75. defer srv.Close()
  76. _, err := srv.GServer.CreateTopic(ctx, &pb.Topic{
  77. Name: "projects/my-project/topics/otlp",
  78. })
  79. assert.NoError(t, err)
  80. factory := NewFactory()
  81. cfg := factory.CreateDefaultConfig()
  82. exporterConfig := cfg.(*Config)
  83. exporterConfig.endpoint = srv.Addr
  84. exporterConfig.UserAgent = "test-user-agent"
  85. exporterConfig.insecure = true
  86. exporterConfig.ProjectID = "my-project"
  87. exporterConfig.Topic = "projects/my-project/topics/otlp"
  88. exporterConfig.TimeoutSettings = exporterhelper.TimeoutSettings{
  89. Timeout: 12 * time.Second,
  90. }
  91. exporterConfig.Compression = "gzip"
  92. exporter := ensureExporter(exportertest.NewNopCreateSettings(), exporterConfig)
  93. assert.NoError(t, exporter.start(ctx, nil))
  94. assert.NoError(t, exporter.consumeTraces(ctx, ptrace.NewTraces()))
  95. assert.NoError(t, exporter.consumeMetrics(ctx, pmetric.NewMetrics()))
  96. assert.NoError(t, exporter.consumeLogs(ctx, plog.NewLogs()))
  97. assert.NoError(t, exporter.shutdown(ctx))
  98. }