Skip to content

Commit

Permalink
dbt_runner: make sure we don't fail on unrelated logs (#1590)
Browse files Browse the repository at this point in the history
* dbt_runner: make sure we don't fail on unrelated logs

* add newline

* Update packages.yml to reflect newer package dependency

* oops

* command_line_dbt_runner: allow returning raw logs so the deprecated tests will work
  • Loading branch information
haritamar authored Jul 7, 2024
1 parent abb7278 commit e9c119c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
26 changes: 18 additions & 8 deletions elementary/clients/dbt/command_line_dbt_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import re
from dataclasses import dataclass
from typing import Any, Dict, List, Optional

Expand All @@ -14,6 +15,11 @@

logger = get_logger(__name__)

MACRO_RESULT_PATTERN = re.compile(
"Elementary: --ELEMENTARY-MACRO-OUTPUT-START--(.*)--ELEMENTARY-MACRO-OUTPUT-END--"
)
RAW_EDR_LOGS_PATTERN = re.compile("Elementary: (.*)")


@dataclass
class DbtCommandResult:
Expand All @@ -22,8 +28,6 @@ class DbtCommandResult:


class CommandLineDbtRunner(BaseDbtRunner):
ELEMENTARY_LOG_PREFIX = "Elementary: "

def __init__(
self,
project_dir: str,
Expand Down Expand Up @@ -150,8 +154,8 @@ def run_operation(
log_errors: bool = True,
vars: Optional[dict] = None,
quiet: bool = False,
should_log: bool = True,
log_output: bool = False,
return_raw_edr_logs: bool = False,
) -> list:
if "." not in macro_name and not self.allow_macros_without_package_prefix:
raise ValueError(
Expand All @@ -160,7 +164,7 @@ def run_operation(
)
macro_to_run = macro_name
macro_to_run_args = macro_args if macro_args else dict()
if should_log:
if not return_raw_edr_logs:
macro_to_run = "elementary.log_macro_results"
macro_to_run_args = dict(
macro_name=macro_name, macro_args=macro_args if macro_args else dict()
Expand All @@ -180,15 +184,21 @@ def run_operation(
f'Failed to run macro: "{macro_name}"\nRun output: {result.output}'
)
run_operation_results = []

log_pattern = (
RAW_EDR_LOGS_PATTERN if return_raw_edr_logs else MACRO_RESULT_PATTERN
)
if capture_output and result.output is not None:
for log in parse_dbt_output(result.output):
if log_errors and log.level == "error":
logger.error(log.msg)
continue
if log.msg and log.msg.startswith(self.ELEMENTARY_LOG_PREFIX):
run_operation_results.append(
log.msg[len(self.ELEMENTARY_LOG_PREFIX) :]
)

if log.msg:
match = log_pattern.match(log.msg)
if match:
run_operation_results.append(match.group(1))

return run_operation_results

def run(
Expand Down
8 changes: 5 additions & 3 deletions elementary/monitor/dbt_project/packages.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
packages:
- package: dbt-labs/dbt_utils
version: [">=0.8.0", "<0.9.0"]
- package: elementary-data/elementary
version: 0.15.1
- git: https://github.com/elementary-data/dbt-data-reliability.git
revision: 42fb88ca59a06c945fb56cb16c66af588fa079fa
# - package: elementary-data/elementary
# version: 0.15.1

# NOTE - for unreleased CLI versions we often need to update the package version to a commit hash (please leave this
# commented, so it will be easy to access)
# - git: https://github.com/elementary-data/dbt-data-reliability.git
# revision: 449da8f16976889ebb8dcce441bfb461b924f008
# revision: 42fb88ca59a06c945fb56cb16c66af588fa079fa
5 changes: 5 additions & 0 deletions tests/tests_with_db/dbt_project/macros/dummy_macro.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% macro dummy_macro() %}
{# Validate that logs in the macro don't fail the dbt runner #}
{% do elementary.edr_log('Amazing macro, so happy to be here') %}
{% do return({'goodbye': 'toodleoo'}) %}
{% endmacro %}
1 change: 1 addition & 0 deletions tests/tests_with_db/pytest.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[pytest]
addopts = --ignore=dbt_project
markers =
skip_targets(targets): skip test for the given targets
only_on_targets(targets): skip test for non given targets
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_with_db/test_dbt_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

class BaseDbtRunnerTest:
def test_run_operation(self, custom_dbt_runner: BaseDbtRunner):
result = self._run_query(
custom_dbt_runner, "select 1 as bla union all select 2 as bla"
result = json.loads(
custom_dbt_runner.run_operation("elementary_tests.dummy_macro")[0]
)
assert result == [{"bla": 1}, {"bla": 2}]
assert result == {"goodbye": "toodleoo"}

def test_ls(self, custom_dbt_runner: BaseDbtRunner):
result = custom_dbt_runner.ls()
Expand Down

0 comments on commit e9c119c

Please sign in to comment.