123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package googlecloudpubsubexporter
- import (
- "context"
- "testing"
- "time"
- pb "cloud.google.com/go/pubsub/apiv1/pubsubpb"
- "cloud.google.com/go/pubsub/pstest"
- "github.com/stretchr/testify/assert"
- "go.opentelemetry.io/collector/exporter/exporterhelper"
- "go.opentelemetry.io/collector/exporter/exportertest"
- "go.opentelemetry.io/collector/pdata/plog"
- "go.opentelemetry.io/collector/pdata/pmetric"
- "go.opentelemetry.io/collector/pdata/ptrace"
- "google.golang.org/api/option"
- )
- func TestName(t *testing.T) {
- exporter := &pubsubExporter{}
- assert.Equal(t, "googlecloudpubsub", exporter.Name())
- }
- func TestGenerateClientOptions(t *testing.T) {
- // Start a fake server running locally.
- srv := pstest.NewServer()
- defer srv.Close()
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- exporterConfig := cfg.(*Config)
- exporterConfig.endpoint = srv.Addr
- exporterConfig.UserAgent = "test-user-agent"
- exporterConfig.insecure = true
- exporterConfig.ProjectID = "my-project"
- exporterConfig.Topic = "projects/my-project/topics/otlp"
- exporterConfig.TimeoutSettings = exporterhelper.TimeoutSettings{
- Timeout: 12 * time.Second,
- }
- exporter := ensureExporter(exportertest.NewNopCreateSettings(), exporterConfig)
- options := exporter.generateClientOptions()
- assert.Equal(t, option.WithUserAgent("test-user-agent"), options[0])
- exporter.config.insecure = false
- options = exporter.generateClientOptions()
- assert.Equal(t, option.WithUserAgent("test-user-agent"), options[0])
- assert.Equal(t, option.WithEndpoint(srv.Addr), options[1])
- }
- func TestExporterDefaultSettings(t *testing.T) {
- ctx := context.Background()
- // Start a fake server running locally.
- srv := pstest.NewServer()
- defer srv.Close()
- _, err := srv.GServer.CreateTopic(ctx, &pb.Topic{
- Name: "projects/my-project/topics/otlp",
- })
- assert.NoError(t, err)
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- exporterConfig := cfg.(*Config)
- exporterConfig.endpoint = srv.Addr
- exporterConfig.insecure = true
- exporterConfig.ProjectID = "my-project"
- exporterConfig.Topic = "projects/my-project/topics/otlp"
- exporterConfig.TimeoutSettings = exporterhelper.TimeoutSettings{
- Timeout: 12 * time.Second,
- }
- exporter := ensureExporter(exportertest.NewNopCreateSettings(), exporterConfig)
- assert.NoError(t, exporter.start(ctx, nil))
- assert.NoError(t, exporter.consumeTraces(ctx, ptrace.NewTraces()))
- assert.NoError(t, exporter.consumeMetrics(ctx, pmetric.NewMetrics()))
- assert.NoError(t, exporter.consumeLogs(ctx, plog.NewLogs()))
- assert.NoError(t, exporter.shutdown(ctx))
- }
- func TestExporterCompression(t *testing.T) {
- ctx := context.Background()
- // Start a fake server running locally.
- srv := pstest.NewServer()
- defer srv.Close()
- _, err := srv.GServer.CreateTopic(ctx, &pb.Topic{
- Name: "projects/my-project/topics/otlp",
- })
- assert.NoError(t, err)
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- exporterConfig := cfg.(*Config)
- exporterConfig.endpoint = srv.Addr
- exporterConfig.UserAgent = "test-user-agent"
- exporterConfig.insecure = true
- exporterConfig.ProjectID = "my-project"
- exporterConfig.Topic = "projects/my-project/topics/otlp"
- exporterConfig.TimeoutSettings = exporterhelper.TimeoutSettings{
- Timeout: 12 * time.Second,
- }
- exporterConfig.Compression = "gzip"
- exporter := ensureExporter(exportertest.NewNopCreateSettings(), exporterConfig)
- assert.NoError(t, exporter.start(ctx, nil))
- assert.NoError(t, exporter.consumeTraces(ctx, ptrace.NewTraces()))
- assert.NoError(t, exporter.consumeMetrics(ctx, pmetric.NewMetrics()))
- assert.NoError(t, exporter.consumeLogs(ctx, plog.NewLogs()))
- assert.NoError(t, exporter.shutdown(ctx))
- }
|