22
33from unittest .mock import Mock , patch
44
5- from cadence ._internal . visibility . prometheus import (
5+ from cadence .metrics import (
66 PrometheusMetrics ,
77 PrometheusConfig ,
8- CadenceMetrics ,
98)
109
1110
@@ -15,7 +14,6 @@ class TestPrometheusConfig:
1514 def test_default_config (self ):
1615 """Test default configuration values."""
1716 config = PrometheusConfig ()
18- assert config .metric_prefix == "cadence_"
1917 assert config .default_labels == {}
2018 assert config .registry is None
2119
@@ -25,11 +23,9 @@ def test_custom_config(self):
2523
2624 registry = CollectorRegistry ()
2725 config = PrometheusConfig (
28- metric_prefix = "my_" ,
2926 default_labels = {"env" : "test" },
3027 registry = registry
3128 )
32- assert config .metric_prefix == "my_"
3329 assert config .default_labels == {"env" : "test" }
3430 assert config .registry is registry
3531
@@ -40,7 +36,6 @@ class TestPrometheusMetrics:
4036 def test_init_with_default_config (self ):
4137 """Test initialization with default config."""
4238 metrics = PrometheusMetrics ()
43- assert metrics .config .metric_prefix == "cadence_"
4439 assert metrics .registry is not None
4540
4641 def test_init_with_custom_config (self ):
@@ -49,15 +44,13 @@ def test_init_with_custom_config(self):
4944
5045 registry = CollectorRegistry ()
5146 config = PrometheusConfig (
52- metric_prefix = "custom_" ,
5347 default_labels = {"service" : "test" },
5448 registry = registry
5549 )
5650 metrics = PrometheusMetrics (config )
57- assert metrics .config .metric_prefix == "custom_"
5851 assert metrics .registry is registry
5952
60- @patch ('cadence._internal.visibility .prometheus.Counter' )
53+ @patch ('cadence.metrics .prometheus.Counter' )
6154 def test_counter_metric (self , mock_counter_class ):
6255 """Test counter metric creation and usage."""
6356 mock_counter = Mock ()
@@ -71,7 +64,7 @@ def test_counter_metric(self, mock_counter_class):
7164 mock_counter .labels .assert_called_once_with (label = "value" )
7265 mock_counter .labels .return_value .inc .assert_called_once_with (5 )
7366
74- @patch ('cadence._internal.visibility .prometheus.Gauge' )
67+ @patch ('cadence.metrics .prometheus.Gauge' )
7568 def test_gauge_metric (self , mock_gauge_class ):
7669 """Test gauge metric creation and usage."""
7770 mock_gauge = Mock ()
@@ -85,7 +78,7 @@ def test_gauge_metric(self, mock_gauge_class):
8578 mock_gauge .labels .assert_called_once_with (env = "prod" )
8679 mock_gauge .labels .return_value .set .assert_called_once_with (42.5 )
8780
88- @patch ('cadence._internal.visibility .prometheus.Histogram' )
81+ @patch ('cadence.metrics .prometheus.Histogram' )
8982 def test_histogram_metric (self , mock_histogram_class ):
9083 """Test histogram metric creation and usage."""
9184 mock_histogram = Mock ()
@@ -101,12 +94,11 @@ def test_histogram_metric(self, mock_histogram_class):
10194
10295
10396 def test_metric_name_generation (self ):
104- """Test metric name generation with prefix."""
105- config = PrometheusConfig (metric_prefix = "my_app_" )
106- metrics = PrometheusMetrics (config )
97+ """Test metric name generation."""
98+ metrics = PrometheusMetrics ()
10799
108100 metric_name = metrics ._get_metric_name ("test_metric" )
109- assert metric_name == "my_app_test_metric "
101+ assert metric_name == "test_metric "
110102
111103 def test_label_merging (self ):
112104 """Test label merging with default labels."""
@@ -124,7 +116,7 @@ def test_label_merging(self):
124116 merged_none = metrics ._merge_labels (None )
125117 assert merged_none == {"service" : "cadence" , "version" : "1.0" }
126118
127- @patch ('cadence._internal.visibility .prometheus.generate_latest' )
119+ @patch ('cadence.metrics .prometheus.generate_latest' )
128120 def test_get_metrics_text (self , mock_generate_latest ):
129121 """Test getting metrics in text format."""
130122 mock_generate_latest .return_value = b"# HELP test_metric Test metric\n # TYPE test_metric counter\n test_metric 1.0\n "
@@ -153,32 +145,3 @@ def test_error_handling_in_gauge(self):
153145 metrics .gauge ("test_gauge" , 1.0 )
154146 # Should not raise, just log error
155147
156-
157- class TestCadenceMetrics :
158- """Test cases for CadenceMetrics enum."""
159-
160- def test_workflow_metrics (self ):
161- """Test workflow metric names."""
162- assert CadenceMetrics .WORKFLOW_STARTED_TOTAL .value == "workflow_started_total"
163- assert CadenceMetrics .WORKFLOW_COMPLETED_TOTAL .value == "workflow_completed_total"
164- assert CadenceMetrics .WORKFLOW_FAILED_TOTAL .value == "workflow_failed_total"
165- assert CadenceMetrics .WORKFLOW_DURATION_SECONDS .value == "workflow_duration_seconds"
166-
167- def test_activity_metrics (self ):
168- """Test activity metric names."""
169- assert CadenceMetrics .ACTIVITY_STARTED_TOTAL .value == "activity_started_total"
170- assert CadenceMetrics .ACTIVITY_COMPLETED_TOTAL .value == "activity_completed_total"
171- assert CadenceMetrics .ACTIVITY_FAILED_TOTAL .value == "activity_failed_total"
172- assert CadenceMetrics .ACTIVITY_DURATION_SECONDS .value == "activity_duration_seconds"
173-
174- def test_worker_metrics (self ):
175- """Test worker metric names."""
176- assert CadenceMetrics .WORKER_TASK_POLLS_TOTAL .value == "worker_task_polls_total"
177- assert CadenceMetrics .WORKER_TASK_POLL_ERRORS_TOTAL .value == "worker_task_poll_errors_total"
178- assert CadenceMetrics .WORKER_ACTIVE_TASKS .value == "worker_active_tasks"
179-
180- def test_client_metrics (self ):
181- """Test client metric names."""
182- assert CadenceMetrics .CLIENT_REQUESTS_TOTAL .value == "client_requests_total"
183- assert CadenceMetrics .CLIENT_REQUEST_DURATION_SECONDS .value == "client_request_duration_seconds"
184- assert CadenceMetrics .CLIENT_REQUEST_ERRORS_TOTAL .value == "client_request_errors_total"
0 commit comments