-
Notifications
You must be signed in to change notification settings - Fork 17.3k
Add metrics for how often a dag gets serialised. #68906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| from sqlalchemy import delete, func, select, update | ||
|
|
||
| import airflow.example_dags as example_dags_module | ||
| from airflow._shared.observability.metrics.base_stats_logger import StatsLogger | ||
| from airflow.dag_processing.dagbag import DagBag | ||
| from airflow.models.asset import AssetActive, AssetAliasModel, AssetModel | ||
| from airflow.models.dag import DagModel | ||
|
|
@@ -90,6 +91,8 @@ def make_example_dags(module): | |
| class TestSerializedDagModel: | ||
| """Unit tests for SerializedDagModel.""" | ||
|
|
||
| SERIALIZED_DAG_STATS = "airflow.models.serialized_dag.stats" | ||
|
|
||
| @pytest.fixture( | ||
| autouse=True, | ||
| params=[ | ||
|
|
@@ -201,6 +204,76 @@ def test_serialized_dag_is_updated_if_dag_is_changed(self, testing_dag_bundle): | |
| assert s_dag_2.data["dag"]["tags"] == ["example", "new_tag", "params"] | ||
| assert dag_updated is True | ||
|
|
||
| def test_serialization_metric_incremented_on_new_write(self, testing_dag_bundle): | ||
| """A brand new serialized DAG write emits the ``dag.serialization`` metric.""" | ||
| dag = make_example_dags(example_dags_module).get("example_params_trigger_ui") | ||
| with mock.patch(self.SERIALIZED_DAG_STATS) as mock_stats: | ||
| assert SDM.write_dag(LazyDeserializedDAG.from_dag(dag), bundle_name="testing") is True | ||
|
|
||
| mock_stats.incr.assert_called_once_with( | ||
| "dag.serialization", | ||
| tags={"dag_id": dag.dag_id, "bundle_name": "testing"}, | ||
| ) | ||
|
|
||
| def test_serialization_metric_not_incremented_when_unchanged(self, testing_dag_bundle): | ||
| """Re-writing an unchanged DAG must not emit the ``dag.serialization`` metric.""" | ||
| dag = make_example_dags(example_dags_module).get("example_params_trigger_ui") | ||
| assert SDM.write_dag(LazyDeserializedDAG.from_dag(dag), bundle_name="testing") is True | ||
|
|
||
| with mock.patch(self.SERIALIZED_DAG_STATS) as mock_stats: | ||
| assert SDM.write_dag(LazyDeserializedDAG.from_dag(dag), bundle_name="testing") is False | ||
|
|
||
| mock_stats.incr.assert_not_called() | ||
|
|
||
| def test_serialization_metric_incremented_on_inplace_update(self, dag_maker, session): | ||
| """Updating a DAG version in place (no dag runs) emits the metric once.""" | ||
| with dag_maker("metric_dag") as dag: | ||
| PythonOperator(task_id="task1", python_callable=lambda: None) | ||
| # Change the DAG so the hash differs; with no dag runs this updates in place. | ||
| PythonOperator(task_id="task2", python_callable=lambda: None, dag=dag) | ||
|
|
||
| with mock.patch(self.SERIALIZED_DAG_STATS) as mock_stats: | ||
| assert SDM.write_dag(LazyDeserializedDAG.from_dag(dag), bundle_name="dag_maker") is True | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Half of the tests are setting |
||
|
|
||
| assert session.scalar(select(func.count()).select_from(DagVersion)) == 1 | ||
| mock_stats.incr.assert_called_once_with( | ||
| "dag.serialization", | ||
| tags={"dag_id": "metric_dag", "bundle_name": "dag_maker"}, | ||
| ) | ||
|
|
||
| def test_serialization_metric_incremented_on_new_version(self, dag_maker, session): | ||
| """Writing a new DAG version (existing run) emits the metric once.""" | ||
| with dag_maker("metric_dag") as dag: | ||
| PythonOperator(task_id="task1", python_callable=lambda: None) | ||
| dag_maker.create_dagrun(run_id="run1", logical_date=pendulum.datetime(2025, 1, 1)) | ||
| PythonOperator(task_id="task2", python_callable=lambda: None, dag=dag) | ||
|
|
||
| with mock.patch(self.SERIALIZED_DAG_STATS) as mock_stats: | ||
| assert SDM.write_dag(LazyDeserializedDAG.from_dag(dag), bundle_name="dag_maker") is True | ||
|
|
||
| assert session.scalar(select(func.count()).select_from(DagVersion)) == 2 | ||
| mock_stats.incr.assert_called_once_with( | ||
| "dag.serialization", | ||
| tags={"dag_id": "metric_dag", "bundle_name": "dag_maker"}, | ||
| ) | ||
|
|
||
| @mock.patch("airflow._shared.observability.metrics.stats._export_legacy_names", True) | ||
| @mock.patch("airflow._shared.observability.metrics.stats._get_backend") | ||
| def test_serialization_metric_exports_new_and_legacy_names(self, mock_get_backend, testing_dag_bundle): | ||
| """Serializing a DAG emits both the modern ``dag.serialization`` metric and its legacy name.""" | ||
| mock_backend = mock.MagicMock(spec=StatsLogger) | ||
| mock_get_backend.return_value = mock_backend | ||
| dag = make_example_dags(example_dags_module).get("example_params_trigger_ui") | ||
|
|
||
| assert SDM.write_dag(LazyDeserializedDAG.from_dag(dag), bundle_name="testing") is True | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: the |
||
|
|
||
| mock_backend.incr.assert_has_calls( | ||
| [ | ||
| mock.call(f"dag.serialization.{dag.dag_id}.testing"), | ||
| mock.call("dag.serialization", tags={"dag_id": dag.dag_id, "bundle_name": "testing"}), | ||
| ] | ||
| ) | ||
|
|
||
| def test_read_dags(self): | ||
| """DAGs can be read from database.""" | ||
| example_dags = self._write_example_dags() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.