@@ -273,6 +273,66 @@ func Test_baggageToLabels(t *testing.T) {
273
273
require .Nil (t , labelSet )
274
274
})
275
275
}
276
+ func Test_setBaggageContextFromMetadata (t * testing.T ) {
277
+ t .Run ("sets_baggage_context" , func (t * testing.T ) {
278
+ ctx := testAddBaggageToGRPCRequest (t , context .Background (),
279
+ "k6.test_run_id" , "123" ,
280
+ "not_k6.some_other_key" , "value" ,
281
+ )
282
+
283
+ ctx , found := setBaggageContextFromMetadata (ctx )
284
+ require .True (t , found )
285
+
286
+ b := baggage .FromContext (ctx )
287
+ testAssertEqualMembers (t , b .Members (),
288
+ "k6.test_run_id" , "123" ,
289
+ "not_k6.some_other_key" , "value" ,
290
+ )
291
+ })
292
+
293
+ t .Run ("does_not_set_baggage_context_with_no_baggage" , func (t * testing.T ) {
294
+ ctx := context .Background ()
295
+
296
+ ctx , found := setBaggageContextFromMetadata (ctx )
297
+ require .False (t , found )
298
+
299
+ b := baggage .FromContext (ctx )
300
+ require .Equal (t , 0 , b .Len ())
301
+ })
302
+ }
303
+
304
+ func Test_getBaggageLabelsFromContext (t * testing.T ) {
305
+ t .Run ("with_k6_baggage" , func (t * testing.T ) {
306
+ ctx := testAddBaggageToRequest (t , httptest .NewRequest ("GET" , "http://example.com" , nil ),
307
+ "k6.test_run_id" , "123" ,
308
+ "not_k6.some_other_key" , "value" ,
309
+ ).Context ()
310
+
311
+ labelSet := getBaggageLabelsFromContext (ctx )
312
+ require .NotNil (t , labelSet )
313
+
314
+ gotLabels := testPprofLabelsToMap (t , * labelSet )
315
+ expectedLabels := map [string ]string {
316
+ "k6_test_run_id" : "123" ,
317
+ }
318
+
319
+ require .Equal (t , expectedLabels , gotLabels )
320
+ })
321
+
322
+ t .Run ("empty_values_are_skipped" , func (t * testing.T ) {
323
+ ctx := testAddBaggageToRequest (t , httptest .NewRequest ("GET" , "http://example.com" , nil )).Context ()
324
+
325
+ labelSet := getBaggageLabelsFromContext (ctx )
326
+ require .Nil (t , labelSet )
327
+ })
328
+
329
+ t .Run ("skips_missing_baggage_header" , func (t * testing.T ) {
330
+ ctx := httptest .NewRequest ("GET" , "http://example.com" , nil ).Context ()
331
+
332
+ labelSet := getBaggageLabelsFromContext (ctx )
333
+ require .Nil (t , labelSet )
334
+ })
335
+ }
276
336
277
337
func testAddBaggageToRequest (t * testing.T , req * http.Request , kvPairs ... string ) * http.Request {
278
338
t .Helper ()
0 commit comments