@@ -336,49 +336,68 @@ def test_namespace_env_var(monkeypatch):
336
336
assert output [0 ]["m" ] == f"{ env_namespace } .item_sold"
337
337
338
338
339
- def test_metrics_disabled_with_env_var (monkeypatch ):
339
+ def test_metrics_disabled_with_env_var (monkeypatch , capsys ):
340
340
# GIVEN environment variable is set to disable metrics
341
341
monkeypatch .setenv ("POWERTOOLS_METRICS_DISABLED" , "true" )
342
342
343
343
# WHEN metrics is initialized and adding metrics
344
344
metrics = DatadogMetrics ()
345
345
metrics .add_metric (name = "test_metric" , value = 1 )
346
+ metrics .flush_metrics ()
346
347
347
- # WHEN flushing metrics
348
- metrics_output = metrics .flush_metrics ()
349
-
350
- # THEN metrics output should be empty
351
- assert metrics_output is None
348
+ # THEN no metrics should have been recorded
349
+ captured = capsys .readouterr ()
350
+ assert not captured .out
352
351
353
352
354
- def test_metrics_disabled_persists_after_flush (monkeypatch ):
353
+ def test_metrics_disabled_persists_after_flush (monkeypatch , capsys ):
355
354
# GIVEN environment variable is set to disable metrics
356
355
monkeypatch .setenv ("POWERTOOLS_METRICS_DISABLED" , "true" )
357
356
metrics = DatadogMetrics ()
358
357
359
358
# WHEN multiple operations are performed with flush in between
360
- metrics .add_metric (name = "metric1" , unit = "Count" , value = 1 )
361
- first_flush = metrics .flush_metrics ()
359
+ metrics .add_metric (name = "metric1" , value = 1 )
360
+ metrics .flush_metrics ()
361
+
362
+ # THEN first flush should not emit any metrics
363
+ captured = capsys .readouterr ()
364
+ assert not captured .out
362
365
363
- metrics .add_metric (name = "metric2" , unit = "Count" , value = 2 )
364
- second_flush = metrics .flush_metrics ()
366
+ # WHEN adding and flushing more metrics
367
+ metrics .add_metric (name = "metric2" , value = 2 )
368
+ metrics .flush_metrics ()
365
369
366
- # THEN all flush operations should return None
367
- assert first_flush is None
368
- assert second_flush is None
370
+ # THEN second flush should also not emit any metrics
371
+ captured = capsys . readouterr ()
372
+ assert not captured . out
369
373
370
374
371
- def test_metrics_disabled_with_namespace (monkeypatch ):
375
+ def test_metrics_disabled_with_namespace (monkeypatch , capsys ):
372
376
# GIVEN environment variable is set to disable metrics
373
377
monkeypatch .setenv ("POWERTOOLS_METRICS_DISABLED" , "true" )
374
378
375
379
# WHEN metrics is initialized with namespace and service
376
380
metrics = DatadogMetrics (namespace = "test_namespace" )
377
381
metrics .add_metric (name = "test_metric" , value = 1 )
378
- metrics_output = metrics .flush_metrics ()
382
+ metrics .flush_metrics ()
383
+
384
+ # THEN no metrics should have been recorded
385
+ captured = capsys .readouterr ()
386
+ assert not captured .out
387
+
388
+
389
+ def test_metrics_disabled_with_dev_mode_true (monkeypatch , capsys ):
390
+ # GIVEN dev mode is enabled
391
+ monkeypatch .setenv ("POWERTOOLS_DEV" , "true" )
392
+
393
+ # WHEN metrics is initialized
394
+ metrics = DatadogMetrics (namespace = "test" )
395
+ metrics .add_metric (name = "test_metric" , value = 1 )
396
+ metrics .flush_metrics ()
379
397
380
- # THEN metrics should still be disabled
381
- assert metrics_output is None
398
+ # THEN no metrics should have been recorded
399
+ captured = capsys .readouterr ()
400
+ assert not captured .out
382
401
383
402
384
403
def test_metrics_enabled_with_env_var_false (monkeypatch , capsys ):
@@ -413,21 +432,6 @@ def test_metrics_enabled_with_env_var_not_set(monkeypatch, capsys):
413
432
assert "test.test_metric" in metrics_output ["m" ]
414
433
415
434
416
- def test_metrics_disabled_with_dev_mode_true (monkeypatch , capsys ):
417
- # GIVEN dev mode is enabled
418
- monkeypatch .setenv ("POWERTOOLS_DEV" , "true" )
419
-
420
- # WHEN metrics is initialized
421
- metrics = DatadogMetrics (namespace = "test" )
422
- metrics .add_metric (name = "test_metric" , value = 1 )
423
-
424
- # AND flushing metrics
425
- metrics_output = metrics .flush_metrics ()
426
-
427
- # THEN metrics output should be empty
428
- assert metrics_output is None
429
-
430
-
431
435
def test_metrics_enabled_with_dev_mode_false (monkeypatch , capsys ):
432
436
# GIVEN dev mode is disabled
433
437
monkeypatch .setenv ("POWERTOOLS_DEV" , "false" )
@@ -453,10 +457,12 @@ def test_metrics_disabled_dev_mode_overrides_metrics_disabled(monkeypatch, capsy
453
457
metrics .add_metric (name = "test_metric" , value = 1 )
454
458
metrics .flush_metrics ()
455
459
456
- # THEN metrics should be written to stdout ( POWERTOOLS_METRICS_DISABLED takes precedence)
460
+ # THEN metrics should be written to stdout since POWERTOOLS_METRICS_DISABLED is false
457
461
output = capsys .readouterr ().out
462
+ assert output # First verify we have output
458
463
metrics_output = json .loads (output )
459
- assert metrics_output
464
+ assert metrics_output # Then verify it's valid JSON
465
+ assert "test.test_metric" in metrics_output ["m" ] # Verify the metric is present
460
466
461
467
462
468
def test_metrics_enabled_with_both_false (monkeypatch , capsys ):
@@ -475,18 +481,16 @@ def test_metrics_enabled_with_both_false(monkeypatch, capsys):
475
481
assert metrics_output
476
482
477
483
478
- def test_metrics_disabled_with_dev_mode_false_and_metrics_disabled_true (monkeypatch ):
484
+ def test_metrics_disabled_with_dev_mode_false_and_metrics_disabled_true (monkeypatch , capsys ):
479
485
# GIVEN dev mode is false but metrics disabled is true
480
486
monkeypatch .setenv ("POWERTOOLS_DEV" , "false" )
481
487
monkeypatch .setenv ("POWERTOOLS_METRICS_DISABLED" , "true" )
482
488
483
489
# WHEN metrics is initialized
484
490
metrics = DatadogMetrics (namespace = "test" )
485
-
486
491
metrics .add_metric (name = "test_metric" , value = 1 )
492
+ metrics .flush_metrics ()
487
493
488
- # WHEN flushing metrics
489
- metrics_output = metrics .flush_metrics ()
490
-
491
- # THEN metrics output should be empty
492
- assert metrics_output is None
494
+ # THEN no metrics should have been recorded
495
+ captured = capsys .readouterr ()
496
+ assert not captured .out
0 commit comments