@@ -96,6 +96,10 @@ func TestHTTPTracesWithGrafanaOptions(t *testing.T) {
96
96
// Basic + output of: echo -n 12345:affafafaafkd | gbase64 -w 0
97
97
"Authorization" : "Basic MTIzNDU6YWZmYWZhZmFhZmtk" ,
98
98
},
99
+ GRPCHeaders : map [string ]string {
100
+ // Basic + output of: echo -n 12345:affafafaafkd | gbase64 -w 0
101
+ "Authorization" : "Basic MTIzNDU6YWZmYWZhZmFhZmtk" ,
102
+ },
99
103
}, & mcfg )
100
104
})
101
105
mcfg .CommonEndpoint = "https://localhost:3939"
@@ -108,6 +112,10 @@ func TestHTTPTracesWithGrafanaOptions(t *testing.T) {
108
112
// Base64 representation of 12345:affafafaafkd
109
113
"Authorization" : "Basic MTIzNDU6YWZmYWZhZmFhZmtk" ,
110
114
},
115
+ GRPCHeaders : map [string ]string {
116
+ // Base64 representation of 12345:affafafaafkd
117
+ "Authorization" : "Basic MTIzNDU6YWZmYWZhZmFhZmtk" ,
118
+ },
111
119
}, & mcfg )
112
120
})
113
121
}
@@ -200,7 +208,7 @@ func TestGRPCTracesEndpointOptions(t *testing.T) {
200
208
}
201
209
202
210
t .Run ("testing with two endpoints" , func (t * testing.T ) {
203
- testTracesGRPOptions (t , otlpOptions {Endpoint : "localhost:3232" }, & tcfg )
211
+ testTracesGRPCOptions (t , otlpOptions {Endpoint : "localhost:3232" , GRPCHeaders : map [ string ] string {} }, & tcfg )
204
212
})
205
213
206
214
tcfg = TracesConfig {
@@ -209,7 +217,7 @@ func TestGRPCTracesEndpointOptions(t *testing.T) {
209
217
}
210
218
211
219
t .Run ("testing with only common endpoint" , func (t * testing.T ) {
212
- testTracesGRPOptions (t , otlpOptions {Endpoint : "localhost:3131" }, & tcfg )
220
+ testTracesGRPCOptions (t , otlpOptions {Endpoint : "localhost:3131" , GRPCHeaders : map [ string ] string {} }, & tcfg )
213
221
})
214
222
215
223
tcfg = TracesConfig {
@@ -218,7 +226,7 @@ func TestGRPCTracesEndpointOptions(t *testing.T) {
218
226
Instrumentations : []string {instrumentations .InstrumentationALL },
219
227
}
220
228
t .Run ("testing with insecure endpoint" , func (t * testing.T ) {
221
- testTracesGRPOptions (t , otlpOptions {Endpoint : "localhost:3232" , Insecure : true }, & tcfg )
229
+ testTracesGRPCOptions (t , otlpOptions {Endpoint : "localhost:3232" , Insecure : true , GRPCHeaders : map [ string ] string {} }, & tcfg )
222
230
})
223
231
224
232
tcfg = TracesConfig {
@@ -228,11 +236,58 @@ func TestGRPCTracesEndpointOptions(t *testing.T) {
228
236
}
229
237
230
238
t .Run ("testing with skip TLS verification" , func (t * testing.T ) {
231
- testTracesGRPOptions (t , otlpOptions {Endpoint : "localhost:3232" , SkipTLSVerify : true }, & tcfg )
239
+ testTracesGRPCOptions (t , otlpOptions {Endpoint : "localhost:3232" , SkipTLSVerify : true , GRPCHeaders : map [ string ] string {} }, & tcfg )
232
240
})
233
241
}
234
242
235
- func testTracesGRPOptions (t * testing.T , expected otlpOptions , tcfg * TracesConfig ) {
243
+ func TestGRPCTracesEndpointHeaders (t * testing.T ) {
244
+ type testCase struct {
245
+ Description string
246
+ Env map [string ]string
247
+ ExpectedHeaders map [string ]string
248
+ Grafana GrafanaOTLP
249
+ }
250
+ for _ , tc := range []testCase {
251
+ {Description : "No headers" ,
252
+ ExpectedHeaders : map [string ]string {}},
253
+ {Description : "defining common OTLP_HEADERS" ,
254
+ Env : map [string ]string {"OTEL_EXPORTER_OTLP_HEADERS" : "Foo=Bar ==,Authorization=Base 2222==" },
255
+ ExpectedHeaders : map [string ]string {"Foo" : "Bar ==" , "Authorization" : "Base 2222==" }},
256
+ {Description : "defining common OTLP_TRACES_HEADERS" ,
257
+ Env : map [string ]string {"OTEL_EXPORTER_OTLP_TRACES_HEADERS" : "Foo=Bar ==,Authorization=Base 1234==" },
258
+ ExpectedHeaders : map [string ]string {"Foo" : "Bar ==" , "Authorization" : "Base 1234==" }},
259
+ {Description : "OTLP_TRACES_HEADERS takes precedence over OTLP_HEADERS" ,
260
+ Env : map [string ]string {
261
+ "OTEL_EXPORTER_OTLP_HEADERS" : "Foo=Bar ==,Authorization=Base 3210==" ,
262
+ "OTEL_EXPORTER_OTLP_TRACES_HEADERS" : "Authorization=Base 1111==" ,
263
+ },
264
+ ExpectedHeaders : map [string ]string {"Foo" : "Bar ==" , "Authorization" : "Base 1111==" }},
265
+ } {
266
+ // mutex to avoid running testcases in parallel so we don't mess up with env vars
267
+ mt := sync.Mutex {}
268
+ t .Run (fmt .Sprint (tc .Description ), func (t * testing.T ) {
269
+ mt .Lock ()
270
+ restore := restoreEnvAfterExecution ()
271
+ defer func () {
272
+ restore ()
273
+ mt .Unlock ()
274
+ }()
275
+ for k , v := range tc .Env {
276
+ require .NoError (t , os .Setenv (k , v ))
277
+ }
278
+
279
+ opts , err := getGRPCTracesEndpointOptions (& TracesConfig {
280
+ TracesEndpoint : "https://localhost:1234/v1/traces" ,
281
+ Grafana : & tc .Grafana ,
282
+ Instrumentations : []string {instrumentations .InstrumentationALL },
283
+ })
284
+ require .NoError (t , err )
285
+ assert .Equal (t , tc .ExpectedHeaders , opts .GRPCHeaders )
286
+ })
287
+ }
288
+ }
289
+
290
+ func testTracesGRPCOptions (t * testing.T , expected otlpOptions , tcfg * TracesConfig ) {
236
291
defer restoreEnvAfterExecution ()()
237
292
opts , err := getGRPCTracesEndpointOptions (tcfg )
238
293
require .NoError (t , err )
0 commit comments