-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
exporter_test.go
109 lines (98 loc) · 3.66 KB
/
exporter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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))
}