Skip to content
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

Improve error logging in DbtLocalBaseOperator #1004

Merged

Conversation

davidsteinar
Copy link
Contributor

Improve error logging when the dbt command returns a non-zero exit code. Instead of raising an AirflowException with the full output, log the output using the logger and then raise the exception with a concise error message. This makes the dbt output more readable and not in a single line.

Description

AirflowException logs message in a single line, can be very long.

Related Issue(s)

closes #1003

Breaking Change?

No

Improve error logging when the `dbt` command returns a non-zero exit code. Instead of raising an `AirflowException` with the full output, log the output using the logger and then raise the exception with a concise error message. This makes the dbt output more readable and not in a single line.
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label May 28, 2024
Copy link

netlify bot commented May 28, 2024

Deploy Preview for sunny-pastelito-5ecb04 canceled.

Name Link
🔨 Latest commit 7e558f6
🔍 Latest deploy log https://app.netlify.com/sites/sunny-pastelito-5ecb04/deploys/6659a069d7717100088ee2dc

@dosubot dosubot bot added area:logging Related to logging, like log levels, log formats, error logging, etc execution:local Related to Local execution environment labels May 28, 2024
@dwreeves dwreeves self-requested a review May 28, 2024 15:04
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label May 28, 2024
Copy link

codecov bot commented May 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.69%. Comparing base (712ef7d) to head (7e558f6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1004      +/-   ##
==========================================
+ Coverage   95.65%   95.69%   +0.03%     
==========================================
  Files          60       60              
  Lines        2925     2926       +1     
==========================================
+ Hits         2798     2800       +2     
+ Misses        127      126       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@tatiana tatiana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks much better, @davidsteinar , thanks a lot for the contribution!

Is there any chance we could add a test to make sure this improvement continues working as expected in the future?

@davidsteinar
Copy link
Contributor Author

@tatiana something like this?

def test_handle_exception_subprocess():
    """
    Test the handle_exception_subprocess method of the DbtLocalBaseOperator class.
    """
    operator = DbtLocalBaseOperator(profile_config=None)
    result = FullOutputSubprocessResult(exit_code=1, output="test", full_output=["n" * n for n in range(1, 1000)])
    
    # Test when exit_code is non-zero
    with pytest.raises(AirflowException) as err_context:
        operator.handle_exception_subprocess(result)

    assert len(err_context.value) < 100 # Ensure the error message is not too long

@tatiana
Copy link
Collaborator

tatiana commented May 29, 2024

@tatiana something like this?

def test_handle_exception_subprocess():
    """
    Test the handle_exception_subprocess method of the DbtLocalBaseOperator class.
    """
    operator = DbtLocalBaseOperator(profile_config=None)
    result = FullOutputSubprocessResult(exit_code=1, output="test", full_output=["n" * n for n in range(1, 1000)])
    
    # Test when exit_code is non-zero
    with pytest.raises(AirflowException) as err_context:
        operator.handle_exception_subprocess(result)

    assert len(err_context.value) < 100 # Ensure the error message is not too long

Hi @davidsteinar I think you're in the right path - thank you!

The one thing that could be improved is to use perhaps pytest caplog fixture (more info) and check the actual log message, confirming the new behaviour.

Example:

def test__copy_partial_parse_to_project_msg_fails_msgpack(mock_unpack, tmp_path, caplog):

@davidsteinar
Copy link
Contributor Author

hi again @tatiana , better?

def test_handle_exception_subprocess(caplog):
    """
    Test the handle_exception_subprocess method of the DbtLocalBaseOperator class.
    """
    operator = DbtLocalBaseOperator(profile_config=None)
    result = FullOutputSubprocessResult(exit_code=1, output="test", full_output=["n" * n for n in range(1, 1000)])
    
    caplog.set_level(logging.ERROR)
    # Test when exit_code is non-zero
    with pytest.raises(AirflowException) as err_context:
        operator.handle_exception_subprocess(result)
    assert len(str(err_context.value)) < 100 # Ensure the error message is not too long
    assert len(caplog.text) > 1000 # Ensure the log message is not truncated

@tatiana
Copy link
Collaborator

tatiana commented May 31, 2024

Thanks, @davidsteinar , it's looking good. Could you please add it to:
https://github.com/astronomer/astronomer-cosmos/blob/main/tests/operators/test_local.py?

Once the tests are passing, we can merge this PR! Thanks a lot for your contribution.

@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels May 31, 2024
Copy link
Contributor

@pankajkoti pankajkoti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like unfortunately there is a minor conflict with main in the file tests/operators/test_local.py. Could you please resolve the conflict and push the commit :) ?

@davidsteinar
Copy link
Contributor Author

Hi @pankajkoti , I fixed the conflict and a few small things to make the test run , all checks have passed now

@pankajkoti
Copy link
Contributor

Thank you @davidsteinar for improving the exception and error logging. Appreciate the contribution and quick fixes 👏🏽

PR looks good to me. I'm going ahead with the merge

cc: @tatiana

@pankajkoti pankajkoti merged commit 6ceb7f2 into astronomer:main May 31, 2024
63 checks passed
tatiana pushed a commit that referenced this pull request Jun 6, 2024
Improve error logging when the `dbt` command returns a non-zero exit
code. Instead of raising an `AirflowException` with the full output, log
the output using the logger and then raise the exception with a concise
error message. This makes the dbt output more readable and not in a
single line as AirflowException logs message in a single line, and it can 
get very long. 

closes #1003 
---------

Co-authored-by: David Steinar Asgrimsson <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@tatiana tatiana mentioned this pull request Jun 6, 2024
tatiana added a commit that referenced this pull request Jun 6, 2024
Bug fixes

* Fix the invocation mode for ``ExecutionMode.VIRTUALENV`` by @marco9663
in #1023
* Fix Cosmos ``enable_cache`` setting by @tatiana in #1025
* Make ``GoogleCloudServiceAccountDictProfileMapping`` dataset profile
arg optional by @oliverrmaa and @pankajastro in #839 and #1017
* Athena profile mapping set ``aws_session_token`` in profile only if it
exists by @pankajastro in #1022

Others

* Update dbt and Airflow conflicts matrix by @tatiana in #1026
* Enable Python 3.12 unittest by @pankajastro in #1018
* Improve error logging in ``DbtLocalBaseOperator`` by @davidsteinar in
#1004
* Add GitHub issue templates for bug reports and feature request by
@pankajkoti in #1009
* Add more fields in bug template to reduce turnaround in issue triaging
by @pankajkoti in #1027
* Fix ``dev/Dockerfile`` + Add ``uv pip install`` for faster build time
by @dwreeves in #997
* Drop support for Airflow 2.3 by @pankajkoti in #994
* Update Astro Runtime image by @RNHTTR in #988 and #989
* Enable ruff F linting by @pankajastro in #985
* Move Cosmos Airflow configuration to settings.py by @pankajastro in
#975
* Fix CI Issues by @tatiana in #1005
* Pre-commit hook updates in #1000, #1019

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Reeves <[email protected]>
Co-authored-by: Pankaj Koti <[email protected]>
Co-authored-by: davidsteinar <[email protected]>
Co-authored-by: David Steinar Asgrimsson <[email protected]>
Co-authored-by: Marco Yuen <[email protected]>
Co-authored-by: Pankaj Singh <[email protected]>
Co-authored-by: Ollie Ma <[email protected]>
arojasb3 pushed a commit to arojasb3/astronomer-cosmos that referenced this pull request Jul 14, 2024
Improve error logging when the `dbt` command returns a non-zero exit
code. Instead of raising an `AirflowException` with the full output, log
the output using the logger and then raise the exception with a concise
error message. This makes the dbt output more readable and not in a
single line as AirflowException logs message in a single line, and it can 
get very long. 

closes astronomer#1003 
---------

Co-authored-by: David Steinar Asgrimsson <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
arojasb3 pushed a commit to arojasb3/astronomer-cosmos that referenced this pull request Jul 14, 2024
Bug fixes

* Fix the invocation mode for ``ExecutionMode.VIRTUALENV`` by @marco9663
in astronomer#1023
* Fix Cosmos ``enable_cache`` setting by @tatiana in astronomer#1025
* Make ``GoogleCloudServiceAccountDictProfileMapping`` dataset profile
arg optional by @oliverrmaa and @pankajastro in astronomer#839 and astronomer#1017
* Athena profile mapping set ``aws_session_token`` in profile only if it
exists by @pankajastro in astronomer#1022

Others

* Update dbt and Airflow conflicts matrix by @tatiana in astronomer#1026
* Enable Python 3.12 unittest by @pankajastro in astronomer#1018
* Improve error logging in ``DbtLocalBaseOperator`` by @davidsteinar in
astronomer#1004
* Add GitHub issue templates for bug reports and feature request by
@pankajkoti in astronomer#1009
* Add more fields in bug template to reduce turnaround in issue triaging
by @pankajkoti in astronomer#1027
* Fix ``dev/Dockerfile`` + Add ``uv pip install`` for faster build time
by @dwreeves in astronomer#997
* Drop support for Airflow 2.3 by @pankajkoti in astronomer#994
* Update Astro Runtime image by @RNHTTR in astronomer#988 and astronomer#989
* Enable ruff F linting by @pankajastro in astronomer#985
* Move Cosmos Airflow configuration to settings.py by @pankajastro in
astronomer#975
* Fix CI Issues by @tatiana in astronomer#1005
* Pre-commit hook updates in astronomer#1000, astronomer#1019

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Reeves <[email protected]>
Co-authored-by: Pankaj Koti <[email protected]>
Co-authored-by: davidsteinar <[email protected]>
Co-authored-by: David Steinar Asgrimsson <[email protected]>
Co-authored-by: Marco Yuen <[email protected]>
Co-authored-by: Pankaj Singh <[email protected]>
Co-authored-by: Ollie Ma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:logging Related to logging, like log levels, log formats, error logging, etc execution:local Related to Local execution environment lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix hard to read AirflowException error logs from dbt command
5 participants