@@ -334,3 +334,81 @@ def test_namespace_env_var(monkeypatch):
334
334
335
335
# THEN namespace should match the explicitly passed variable and not the env var
336
336
assert output [0 ]["m" ] == f"{ env_namespace } .item_sold"
337
+
338
+
339
+ ####################
340
+ def test_metrics_disabled_with_env_var (monkeypatch ):
341
+ # GIVEN environment variable is set to disable metrics
342
+ monkeypatch .setenv ("POWERTOOLS_METRICS_DISABLED" , "true" )
343
+
344
+ # WHEN metrics is initialized and adding metrics
345
+ metrics = DatadogMetrics ()
346
+ metrics .add_metric (name = "test_metric" , value = 1 )
347
+
348
+ # WHEN flushing metrics
349
+ metrics_output = metrics .flush_metrics ()
350
+
351
+ # THEN metrics output should be empty
352
+ assert metrics_output is None
353
+
354
+
355
+ def test_metrics_disabled_persists_after_flush (monkeypatch ):
356
+ # GIVEN environment variable is set to disable metrics
357
+ monkeypatch .setenv ("POWERTOOLS_METRICS_DISABLED" , "true" )
358
+ metrics = DatadogMetrics ()
359
+
360
+ # WHEN multiple operations are performed with flush in between
361
+ metrics .add_metric (name = "metric1" , unit = "Count" , value = 1 )
362
+ first_flush = metrics .flush_metrics ()
363
+
364
+ metrics .add_metric (name = "metric2" , unit = "Count" , value = 2 )
365
+ second_flush = metrics .flush_metrics ()
366
+
367
+ # THEN all flush operations should return None
368
+ assert first_flush is None
369
+ assert second_flush is None
370
+
371
+
372
+ def test_metrics_disabled_with_namespace (monkeypatch ):
373
+ # GIVEN environment variable is set to disable metrics
374
+ monkeypatch .setenv ("POWERTOOLS_METRICS_DISABLED" , "true" )
375
+
376
+ # WHEN metrics is initialized with namespace and service
377
+ metrics = DatadogMetrics (namespace = "test_namespace" )
378
+ metrics .add_metric (name = "test_metric" , value = 1 )
379
+ metrics_output = metrics .flush_metrics ()
380
+
381
+ # THEN metrics should still be disabled
382
+ assert metrics_output is None
383
+
384
+
385
+ def test_metrics_enabled_with_env_var_false (monkeypatch , capsys ):
386
+ # GIVEN environment variable is set to enable metrics
387
+ monkeypatch .setenv ("POWERTOOLS_METRICS_DISABLED" , "false" )
388
+
389
+ # WHEN metrics is initialized with namespace and metrics added
390
+ metrics = DatadogMetrics (namespace = "test" )
391
+ metrics .add_metric (name = "test_metric" , value = 1 )
392
+ metrics .flush_metrics ()
393
+
394
+ # THEN Datadog metrics should be written to stdout
395
+ output = capsys .readouterr ().out
396
+ metrics_output = json .loads (output )
397
+
398
+ assert metrics_output
399
+
400
+
401
+ def test_metrics_enabled_with_env_var_not_set (monkeypatch , capsys ):
402
+ # GIVEN environment variable is not set
403
+ monkeypatch .delenv ("POWERTOOLS_METRICS_DISABLED" , raising = False )
404
+
405
+ # WHEN metrics is initialized with namespace and metrics added
406
+ metrics = DatadogMetrics (namespace = "test" )
407
+ metrics .add_metric (name = "test_metric" , value = 1 )
408
+ metrics .flush_metrics ()
409
+
410
+ # THEN metrics should be written to stdout
411
+ output = capsys .readouterr ().out
412
+ metrics_output = json .loads (output )
413
+
414
+ assert "test.test_metric" in metrics_output ["m" ]
0 commit comments