Skip to content

Commit 8ba4ac5

Browse files
fix(ci_visibility): fixes unittest enabling the CI VIsibility plugin [backport 2.1] (#7301)
Backport 02b0eeb from #7292 to 2.1. ## Motivation This PR aims to fix [this GitHub issue](#7257) from dd-trace-py, which was causing unexpected log messages. Related JIRA issue: [CIVIS-7652](https://datadoghq.atlassian.net/browse/CIVIS-7652?atlOrigin=eyJpIjoiM2EyMTI2YzY5YWI3NDBiMGI5OWE2ODk3MzE1Nzc2ZDUiLCJwIjoiaiJ9). ## Changes Now, the `unittest` integration will enable `CIVisibility` only if `unittest` is running any tests. This will prevent log messages as CIVisibility won't be enabled until tests are detected. ## Checklist - [X] Change(s) are motivated and described in the PR description. - [X] Testing strategy is described if automated tests are not included in the PR. - [X] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [X] Change is maintainable (easy to change, telemetry, documentation). - [X] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [X] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [X] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) - [x] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [x] This PR doesn't touch any of that. [CIVIS-7652]: https://datadoghq.atlassian.net/browse/CIVIS-7652?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ Co-authored-by: Eric Navarro <[email protected]>
1 parent fddae71 commit 8ba4ac5

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

ddtrace/contrib/unittest/patch.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ def get_version():
5050
return ""
5151

5252

53+
def _enable_unittest_if_not_started():
54+
if _CIVisibility.enabled:
55+
return
56+
_CIVisibility.enable(config=ddtrace.config.unittest)
57+
58+
5359
def _set_tracer(tracer: ddtrace.tracer):
5460
"""Manually sets the tracer instance to `unittest.`"""
5561
unittest._datadog_tracer = tracer
@@ -299,8 +305,6 @@ def patch():
299305
if getattr(unittest, "_datadog_patch", False) or _CIVisibility.enabled:
300306
return
301307

302-
_CIVisibility.enable(config=ddtrace.config.unittest)
303-
304308
unittest._datadog_patch = True
305309

306310
_w = wrapt.wrap_function_wrapper
@@ -613,6 +617,7 @@ def handle_cli_run(func, instance: unittest.TestProgram, args: tuple, kwargs: di
613617
"""
614618
test_session_span = None
615619
if _is_invoked_by_cli(instance):
620+
_enable_unittest_if_not_started()
616621
if not hasattr(_CIVisibility, "_unittest_data"):
617622
_CIVisibility._unittest_data = {"suites": {}, "modules": {}}
618623
for parent_module in instance.test._tests:
@@ -643,6 +648,7 @@ def handle_text_test_runner_wrapper(func, instance: unittest.TextTestResult, arg
643648
"""
644649
if _is_invoked_by_cli(instance):
645650
return func(*args, **kwargs)
651+
_enable_unittest_if_not_started()
646652
_CIVisibility._datadog_entry = "TextTestRunner"
647653
if not hasattr(_CIVisibility, "_datadog_session_span"):
648654
_CIVisibility._datadog_session_span = _start_test_session_span(instance)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
CI Visibility: fixes an issue where just importing `unittest` enabled CIVisibility and potentially caused unexpected logs and API requests

0 commit comments

Comments
 (0)