@@ -29,6 +29,22 @@ import (
29
29
"go.opentelemetry.io/otel/sdk/trace"
30
30
)
31
31
32
+ type testType struct { string }
33
+
34
+ func factory (val string ) func (ctx context.Context ) (* testType , error ) {
35
+ return func (ctx context.Context ) (* testType , error ) { return & testType {val }, nil }
36
+ }
37
+
38
+ func newTestRegistry () registry [* testType ] {
39
+ return registry [* testType ]{
40
+ names : map [string ]func (context.Context ) (* testType , error ){
41
+ "" : factory ("" ),
42
+ "otlp" : factory ("otlp" ),
43
+ "none" : factory ("none" ),
44
+ },
45
+ }
46
+ }
47
+
32
48
var stdoutMetricFactory = func (ctx context.Context ) (metric.Reader , error ) {
33
49
exp , err := stdoutmetric .New ()
34
50
if err != nil {
@@ -46,45 +62,27 @@ var stdoutSpanFactory = func(ctx context.Context) (trace.SpanExporter, error) {
46
62
}
47
63
48
64
func TestCanStoreExporterFactory (t * testing.T ) {
49
- t .Run ("spans" , func (t * testing.T ) {
50
- r := newSpanExporterRegistry ()
51
- assert .NotPanics (t , func () {
52
- require .NoError (t , r .store ("first" , stdoutSpanFactory ))
53
- })
54
- })
55
- t .Run ("metrics" , func (t * testing.T ) {
56
- r := newMetricReaderRegistry ()
57
- assert .NotPanics (t , func () {
58
- require .NoError (t , r .store ("first" , stdoutMetricFactory ))
59
- })
65
+ r := newTestRegistry ()
66
+ assert .NotPanics (t , func () {
67
+ require .NoError (t , r .store ("first" , factory ("first" )))
60
68
})
61
69
}
62
70
63
71
func TestLoadOfUnknownExporterReturnsError (t * testing.T ) {
64
- t .Run ("spans" , func (t * testing.T ) {
65
- r := newSpanExporterRegistry ()
66
- assert .NotPanics (t , func () {
67
- exp , err := r .load (context .Background (), "non-existent" )
68
- assert .Equal (t , err , errUnknownExporter , "empty registry should hold nothing" )
69
- assert .Nil (t , exp , "non-nil exporter returned" )
70
- })
71
- })
72
- t .Run ("metrics" , func (t * testing.T ) {
73
- r := newMetricReaderRegistry ()
74
- assert .NotPanics (t , func () {
75
- exp , err := r .load (context .Background (), "non-existent" )
76
- assert .Equal (t , err , errUnknownExporter , "empty registry should hold nothing" )
77
- assert .Nil (t , exp , "non-nil exporter returned" )
78
- })
72
+ r := newTestRegistry ()
73
+ assert .NotPanics (t , func () {
74
+ exp , err := r .load (context .Background (), "non-existent" )
75
+ assert .Equal (t , err , errUnknownExporter , "empty registry should hold nothing" )
76
+ assert .Nil (t , exp , "non-nil exporter returned" )
79
77
})
80
78
}
81
79
82
80
func TestRegistryIsConcurrentSafe (t * testing.T ) {
83
81
const exporterName = "stdout"
84
82
85
- r := newSpanExporterRegistry ()
83
+ r := newTestRegistry ()
86
84
assert .NotPanics (t , func () {
87
- require .NoError (t , r .store (exporterName , stdoutSpanFactory ))
85
+ require .NoError (t , r .store (exporterName , factory ( "stdout" ) ))
88
86
})
89
87
90
88
var wg sync.WaitGroup
@@ -93,63 +91,108 @@ func TestRegistryIsConcurrentSafe(t *testing.T) {
93
91
go func () {
94
92
defer wg .Done ()
95
93
assert .NotPanics (t , func () {
96
- require .ErrorIs (t , r .store (exporterName , stdoutSpanFactory ), errDuplicateRegistration )
94
+ require .ErrorIs (t , r .store (exporterName , factory ( "stdout" ) ), errDuplicateRegistration )
97
95
})
98
96
}()
99
97
100
98
wg .Add (1 )
101
99
go func () {
102
100
defer wg .Done ()
103
101
assert .NotPanics (t , func () {
104
- exp , err := r .load (context .Background (), exporterName )
102
+ _ , err := r .load (context .Background (), exporterName )
105
103
assert .NoError (t , err , "missing exporter in registry" )
106
- assert .IsType (t , & stdouttrace.Exporter {}, exp )
107
104
})
108
105
}()
109
106
110
107
wg .Wait ()
111
108
}
112
109
113
- func TestSubsequentCallsToGetExporterReturnsNewInstances (t * testing.T ) {
110
+ type funcs [T any ] struct {
111
+ makeExporter func (context.Context , string ) (T , error )
112
+ assertOTLPHTTP func (* testing.T , T )
113
+ registerFactory func (string , func (context.Context ) (T , error ))
114
+ stdoutFactory func (ctx context.Context ) (T , error )
115
+ registry * registry [T ]
116
+ }
117
+
118
+ var (
119
+ spanFuncs = funcs [trace.SpanExporter ]{
120
+ makeExporter : spanExporter ,
121
+ assertOTLPHTTP : assertOTLPHTTPSpanExporter ,
122
+ registerFactory : RegisterSpanExporter ,
123
+ stdoutFactory : stdoutSpanFactory ,
124
+ registry : & spanExporterRegistry ,
125
+ }
126
+
127
+ metricFuncs = funcs [metric.Reader ]{
128
+ makeExporter : metricReader ,
129
+ assertOTLPHTTP : assertOTLPHTTPMetricReader ,
130
+ registerFactory : RegisterMetricReader ,
131
+ stdoutFactory : stdoutMetricFactory ,
132
+ registry : & metricReaderRegistry ,
133
+ }
134
+ )
135
+
136
+ func (f funcs [T ]) testSubsequentCallsToGetExporterReturnsNewInstances (t * testing.T ) {
114
137
const exporterType = "otlp"
115
- exp1 , err := spanExporter (context .Background (), exporterType )
138
+
139
+ exp1 , err := f .makeExporter (context .Background (), exporterType )
116
140
assert .NoError (t , err )
117
- assertOTLPHTTPExporter (t , exp1 )
141
+ f . assertOTLPHTTP (t , exp1 )
118
142
119
143
exp2 , err := spanExporter (context .Background (), exporterType )
120
144
assert .NoError (t , err )
121
- assertOTLPHTTPExporter (t , exp2 )
145
+ assertOTLPHTTPSpanExporter (t , exp2 )
122
146
123
147
assert .NotSame (t , exp1 , exp2 )
124
148
}
125
149
126
- func TestDefaultOTLPExporterFactoriesAreAutomaticallyRegistered (t * testing.T ) {
127
- exp1 , err := spanExporter (context .Background (), "" )
150
+ func TestSubsequentCallsToGetExporterReturnsNewInstances (t * testing.T ) {
151
+ t .Run ("spans" , spanFuncs .testSubsequentCallsToGetExporterReturnsNewInstances )
152
+ t .Run ("metrics" , metricFuncs .testSubsequentCallsToGetExporterReturnsNewInstances )
153
+ }
154
+
155
+ func (f funcs [T ]) testDefaultOTLPExporterFactoriesAreAutomaticallyRegistered (t * testing.T ) {
156
+ exp1 , err := f .makeExporter (context .Background (), "" )
128
157
assert .Nil (t , err )
129
- assertOTLPHTTPExporter (t , exp1 )
158
+ f . assertOTLPHTTP (t , exp1 )
130
159
131
- exp2 , err := spanExporter (context .Background (), "otlp" )
160
+ exp2 , err := f . makeExporter (context .Background (), "otlp" )
132
161
assert .Nil (t , err )
133
- assertOTLPHTTPExporter (t , exp2 )
162
+ f . assertOTLPHTTP (t , exp2 )
134
163
}
135
164
136
- func TestEnvRegistryCanRegisterExporterFactory (t * testing.T ) {
165
+ func TestDefaultOTLPExporterFactoriesAreAutomaticallyRegistered (t * testing.T ) {
166
+ t .Run ("spans" , spanFuncs .testDefaultOTLPExporterFactoriesAreAutomaticallyRegistered )
167
+ t .Run ("metrics" , metricFuncs .testDefaultOTLPExporterFactoriesAreAutomaticallyRegistered )
168
+ }
169
+
170
+ func (f funcs [T ]) testEnvRegistryCanRegisterExporterFactory (t * testing.T ) {
137
171
const exporterName = "custom"
138
- RegisterSpanExporter (exporterName , stdoutSpanFactory )
139
- t .Cleanup (func () { spanExporterRegistry .drop (exporterName ) })
172
+ f . registerFactory (exporterName , f . stdoutFactory )
173
+ t .Cleanup (func () { f . registry .drop (exporterName ) })
140
174
141
- exp , err := spanExporterRegistry .load (context .Background (), exporterName )
175
+ _ , err := f . registry .load (context .Background (), exporterName )
142
176
assert .Nil (t , err , "missing exporter in envRegistry" )
143
- assert .IsType (t , & stdouttrace.Exporter {}, exp )
144
177
}
145
178
146
- func TestEnvRegistryPanicsOnDuplicateRegisterCalls (t * testing.T ) {
179
+ func TestEnvRegistryCanRegisterExporterFactory (t * testing.T ) {
180
+ t .Run ("spans" , spanFuncs .testEnvRegistryCanRegisterExporterFactory )
181
+ t .Run ("metrics" , metricFuncs .testEnvRegistryCanRegisterExporterFactory )
182
+ }
183
+
184
+ func (f funcs [T ]) testEnvRegistryPanicsOnDuplicateRegisterCalls (t * testing.T ) {
147
185
const exporterName = "custom"
148
- RegisterSpanExporter (exporterName , stdoutSpanFactory )
149
- t .Cleanup (func () { spanExporterRegistry .drop (exporterName ) })
186
+ f . registerFactory (exporterName , f . stdoutFactory )
187
+ t .Cleanup (func () { f . registry .drop (exporterName ) })
150
188
151
189
errString := fmt .Sprintf ("%s: %q" , errDuplicateRegistration , exporterName )
152
190
assert .PanicsWithError (t , errString , func () {
153
- RegisterSpanExporter (exporterName , stdoutSpanFactory )
191
+ f . registerFactory (exporterName , f . stdoutFactory )
154
192
})
155
193
}
194
+
195
+ func TestEnvRegistryPanicsOnDuplicateRegisterCalls (t * testing.T ) {
196
+ t .Run ("spans" , spanFuncs .testEnvRegistryPanicsOnDuplicateRegisterCalls )
197
+ t .Run ("metrics" , metricFuncs .testEnvRegistryPanicsOnDuplicateRegisterCalls )
198
+ }
0 commit comments