@@ -239,6 +239,78 @@ func Test_Agent_configFromUnits(t *testing.T) {
239
239
assert .Equal (t , "fleet-server" , cfg .Inputs [0 ].Type )
240
240
require .Len (t , cfg .Output .Elasticsearch .Hosts , 2 )
241
241
})
242
+ t .Run ("Minimal APM config is specified" , func (t * testing.T ) {
243
+ outStruct , err := structpb .NewStruct (map [string ]interface {}{
244
+ "service_token" : "test-token" ,
245
+ })
246
+ require .NoError (t , err )
247
+ mockOutClient := & mockClientUnit {}
248
+ mockOutClient .On ("Expected" ).Return (
249
+ client.Expected {
250
+ State : client .UnitStateHealthy ,
251
+ LogLevel : client .UnitLogLevelInfo ,
252
+ Config : & proto.UnitExpectedConfig {Source : outStruct },
253
+ })
254
+
255
+ inStruct , err := structpb .NewStruct (map [string ]interface {}{
256
+ "type" : "fleet-server" ,
257
+ "server" : map [string ]interface {}{
258
+ "host" : "0.0.0.0" ,
259
+ },
260
+ "policy" : map [string ]interface {}{
261
+ "id" : "test-policy" ,
262
+ },
263
+ })
264
+ require .NoError (t , err )
265
+
266
+ mockInClient := & mockClientUnit {}
267
+ mockInClient .On ("Expected" ).Return (
268
+ client.Expected {
269
+ State : client .UnitStateHealthy ,
270
+ LogLevel : client .UnitLogLevelInfo ,
271
+ Config : & proto.UnitExpectedConfig {Source : inStruct },
272
+ APMConfig : & proto.APMConfig {
273
+ Elastic : & proto.ElasticAPM {
274
+ Environment : "test" ,
275
+ SecretToken : "secretToken" ,
276
+ Hosts : []string {"testhost:8080" },
277
+ },
278
+ },
279
+ })
280
+
281
+ cliCfg , err := ucfg .NewFrom (map [string ]interface {}{
282
+ "inputs" : []interface {}{
283
+ map [string ]interface {}{
284
+ "policy" : map [string ]interface {}{
285
+ "id" : "test-policy" ,
286
+ },
287
+ },
288
+ },
289
+ })
290
+ require .NoError (t , err )
291
+ a := & Agent {
292
+ cliCfg : cliCfg ,
293
+ agent : mockAgent ,
294
+ inputUnit : mockInClient ,
295
+ outputUnit : mockOutClient ,
296
+ }
297
+
298
+ cfg , err := a .configFromUnits (context .Background ())
299
+ require .NoError (t , err )
300
+ require .Len (t , cfg .Inputs , 1 )
301
+ assert .Equal (t , "fleet-server" , cfg .Inputs [0 ].Type )
302
+ assert .Equal (t , "test-policy" , cfg .Inputs [0 ].Policy .ID )
303
+ assert .Equal (t , "0.0.0.0" , cfg .Inputs [0 ].Server .Host )
304
+ assert .True (t , cfg .Inputs [0 ].Server .Instrumentation .Enabled )
305
+ assert .False (t , cfg .Inputs [0 ].Server .Instrumentation .TLS .SkipVerify )
306
+ assert .Empty (t , cfg .Inputs [0 ].Server .Instrumentation .TLS .ServerCA )
307
+ assert .Equal (t , "test" , cfg .Inputs [0 ].Server .Instrumentation .Environment )
308
+ assert .Empty (t , cfg .Inputs [0 ].Server .Instrumentation .APIKey )
309
+ assert .Equal (t , "secretToken" , cfg .Inputs [0 ].Server .Instrumentation .SecretToken )
310
+ assert .Equal (t , []string {"testhost:8080" }, cfg .Inputs [0 ].Server .Instrumentation .Hosts )
311
+ assert .Empty (t , cfg .Inputs [0 ].Server .Instrumentation .GlobalLabels )
312
+ assert .Equal (t , "test-token" , cfg .Output .Elasticsearch .ServiceToken )
313
+ })
242
314
t .Run ("APM config is specified" , func (t * testing.T ) {
243
315
outStruct , err := structpb .NewStruct (map [string ]interface {}{
244
316
"service_token" : "test-token" ,
@@ -262,6 +334,8 @@ func Test_Agent_configFromUnits(t *testing.T) {
262
334
},
263
335
})
264
336
require .NoError (t , err )
337
+
338
+ samplingRate := float32 (0.5 )
265
339
mockInClient := & mockClientUnit {}
266
340
mockInClient .On ("Expected" ).Return (
267
341
client.Expected {
@@ -279,6 +353,7 @@ func Test_Agent_configFromUnits(t *testing.T) {
279
353
SecretToken : "secretToken" ,
280
354
Hosts : []string {"testhost:8080" },
281
355
GlobalLabels : "test" ,
356
+ SamplingRate : & samplingRate ,
282
357
},
283
358
},
284
359
})
@@ -315,6 +390,7 @@ func Test_Agent_configFromUnits(t *testing.T) {
315
390
assert .Equal (t , []string {"testhost:8080" }, cfg .Inputs [0 ].Server .Instrumentation .Hosts )
316
391
assert .Equal (t , "test" , cfg .Inputs [0 ].Server .Instrumentation .GlobalLabels )
317
392
assert .Equal (t , "test-token" , cfg .Output .Elasticsearch .ServiceToken )
393
+ assert .Equal (t , "0.5" , cfg .Inputs [0 ].Server .Instrumentation .TransactionSampleRate )
318
394
})
319
395
t .Run ("APM config no tls" , func (t * testing.T ) {
320
396
outStruct , err := structpb .NewStruct (map [string ]interface {}{
@@ -336,6 +412,8 @@ func Test_Agent_configFromUnits(t *testing.T) {
336
412
},
337
413
})
338
414
require .NoError (t , err )
415
+
416
+ samplingRate := float32 (0.01 )
339
417
mockInClient := & mockClientUnit {}
340
418
mockInClient .On ("Expected" ).Return (
341
419
client.Expected {
@@ -349,6 +427,7 @@ func Test_Agent_configFromUnits(t *testing.T) {
349
427
SecretToken : "secretToken" ,
350
428
Hosts : []string {"testhost:8080" },
351
429
GlobalLabels : "test" ,
430
+ SamplingRate : & samplingRate ,
352
431
},
353
432
},
354
433
})
@@ -374,6 +453,7 @@ func Test_Agent_configFromUnits(t *testing.T) {
374
453
assert .Equal (t , []string {"testhost:8080" }, cfg .Inputs [0 ].Server .Instrumentation .Hosts )
375
454
assert .Equal (t , "test" , cfg .Inputs [0 ].Server .Instrumentation .GlobalLabels )
376
455
assert .Equal (t , "test-token" , cfg .Output .Elasticsearch .ServiceToken )
456
+ assert .Equal (t , "0.01" , cfg .Inputs [0 ].Server .Instrumentation .TransactionSampleRate )
377
457
})
378
458
t .Run ("APM config and instrumentation is specified" , func (t * testing.T ) {
379
459
outStruct , err := structpb .NewStruct (map [string ]interface {}{
@@ -398,14 +478,17 @@ func Test_Agent_configFromUnits(t *testing.T) {
398
478
"skip_verify" : true ,
399
479
"server_certificate" : "/path/to/cert.crt" ,
400
480
},
401
- "environment" : "replace" ,
402
- "api_key" : "replace" ,
403
- "secret_token" : "replace" ,
404
- "hosts" : []interface {}{"replace" },
481
+ "environment" : "replace" ,
482
+ "api_key" : "replace" ,
483
+ "secret_token" : "replace" ,
484
+ "hosts" : []interface {}{"replace" },
485
+ "transaction_sample_rate" : "0.75" ,
405
486
},
406
487
},
407
488
})
408
489
require .NoError (t , err )
490
+
491
+ samplingRate := float32 (0.01 )
409
492
mockInClient := & mockClientUnit {}
410
493
mockInClient .On ("Expected" ).Return (
411
494
client.Expected {
@@ -423,6 +506,7 @@ func Test_Agent_configFromUnits(t *testing.T) {
423
506
SecretToken : "secretToken" ,
424
507
Hosts : []string {"testhost:8080" },
425
508
GlobalLabels : "test" ,
509
+ SamplingRate : & samplingRate ,
426
510
},
427
511
},
428
512
})
@@ -449,6 +533,7 @@ func Test_Agent_configFromUnits(t *testing.T) {
449
533
assert .Equal (t , []string {"testhost:8080" }, cfg .Inputs [0 ].Server .Instrumentation .Hosts )
450
534
assert .Equal (t , "test" , cfg .Inputs [0 ].Server .Instrumentation .GlobalLabels )
451
535
assert .Equal (t , "test-token" , cfg .Output .Elasticsearch .ServiceToken )
536
+ assert .Equal (t , "0.01" , cfg .Inputs [0 ].Server .Instrumentation .TransactionSampleRate )
452
537
})
453
538
t .Run ("APM config error" , func (t * testing.T ) {
454
539
outStruct , err := structpb .NewStruct (map [string ]interface {}{
0 commit comments