From ad7ea4dcd23733e08f10571113a94012ec3a4e28 Mon Sep 17 00:00:00 2001 From: Mike Alfare <13974384+mikealfare@users.noreply.github.com> Date: Thu, 21 Mar 2024 15:40:00 -0400 Subject: [PATCH] Acknowledge `send_anonymous_usage_stats` setting for python models (#933) * add draft integration test demonstrating the issue * changie * condition on the send_anonymous_usage_stats setting when building a python model --- .../unreleased/Fixes-20240318-203635.yaml | 6 +++ dbt/adapters/snowflake/impl.py | 7 ++- .../adapter/test_anonymous_usage_stats.py | 43 +++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 .changes/unreleased/Fixes-20240318-203635.yaml create mode 100644 tests/functional/adapter/test_anonymous_usage_stats.py diff --git a/.changes/unreleased/Fixes-20240318-203635.yaml b/.changes/unreleased/Fixes-20240318-203635.yaml new file mode 100644 index 000000000..d2c1cd05f --- /dev/null +++ b/.changes/unreleased/Fixes-20240318-203635.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Acknowledge `send_anonymous_usage_stats` setting for python models +time: 2024-03-18T20:36:35.396323-04:00 +custom: + Author: mikealfare + Issue: "830" diff --git a/dbt/adapters/snowflake/impl.py b/dbt/adapters/snowflake/impl.py index 4dc57a1b4..3583c888c 100644 --- a/dbt/adapters/snowflake/impl.py +++ b/dbt/adapters/snowflake/impl.py @@ -217,10 +217,13 @@ def submit_python_job(self, parsed_model: dict, compiled_code: str): if imports: imports = f"IMPORTS = ('{imports}')" - snowpark_telemetry_string = "dbtLabs_dbtPython" - snowpark_telemetry_snippet = f""" + if self.config.args.SEND_ANONYMOUS_USAGE_STATS: + snowpark_telemetry_string = "dbtLabs_dbtPython" + snowpark_telemetry_snippet = f""" import sys sys._xoptions['snowflake_partner_attribution'].append("{snowpark_telemetry_string}")""" + else: + snowpark_telemetry_snippet = "" common_procedure_code = f""" RETURNS STRING diff --git a/tests/functional/adapter/test_anonymous_usage_stats.py b/tests/functional/adapter/test_anonymous_usage_stats.py new file mode 100644 index 000000000..bc5a4d334 --- /dev/null +++ b/tests/functional/adapter/test_anonymous_usage_stats.py @@ -0,0 +1,43 @@ +from dbt.tests.util import run_dbt_and_capture +import pytest + + +ANONYMOUS_USAGE_MESSAGE = """ +sys._xoptions['snowflake_partner_attribution'].append("dbtLabs_dbtPython") +""".strip() + + +MY_PYTHON_MODEL = """ +import pandas + +def model(dbt, session): + dbt.config(materialized='table') + data = [[1,2]] * 10 + return pandas.DataFrame(data, columns=['test', 'test2']) +""" + + +class AnonymousUsageStatsBase: + @pytest.fixture(scope="class") + def models(self): + return {"my_python_model.py": MY_PYTHON_MODEL} + + +class TestAnonymousUsageStatsOn(AnonymousUsageStatsBase): + @pytest.fixture(scope="class") + def project_config_update(self): + return {"flags": {"send_anonymous_usage_stats": True}} + + def test_stats_get_sent(self, project): + _, logs = run_dbt_and_capture(["--debug", "run"]) + assert ANONYMOUS_USAGE_MESSAGE in logs + + +class TestAnonymousUsageStatsOff(AnonymousUsageStatsBase): + @pytest.fixture(scope="class") + def project_config_update(self, dbt_profile_target): + return {"flags": {"send_anonymous_usage_stats": False}} + + def test_stats_do_not_get_sent(self, project): + _, logs = run_dbt_and_capture(["--debug", "run"]) + assert ANONYMOUS_USAGE_MESSAGE not in logs