Skip to content

Commit 6e9c663

Browse files
mwdd146980claude
andcommitted
Address PR 3a review: protocol gaps, transitional comment, timeout handling
Add ok/reason properties to HTTPResponseProtocol to match production usage, widen traefik_mesh timeout handler with HTTPTimeoutError, add transitional comment to OM V2 except clause, and add tests for ok/reason on MockHTTPResponse. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent aeb154c commit 6e9c663

4 files changed

Lines changed: 21 additions & 1 deletion

File tree

datadog_checks_base/datadog_checks/base/checks/openmetrics/v2/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def check(self, _):
7272
with self.adopt_namespace(scraper.namespace):
7373
try:
7474
scraper.scrape()
75+
# Pairs requests-native + library-agnostic exceptions; simplify to HTTPError after migration.
7576
except (ConnectionError, RequestException, HTTPRequestError, HTTPStatusError) as e:
7677
self.log.error("There was an error scraping endpoint %s: %s", endpoint, str(e))
7778
raise type(e)("There was an error scraping endpoint {}: {}".format(endpoint, e)) from None

datadog_checks_base/datadog_checks/base/utils/http_protocol.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class HTTPResponseProtocol(Protocol):
1313
text: str
1414
headers: Mapping[str, str]
1515

16+
@property
17+
def ok(self) -> bool: ...
18+
@property
19+
def reason(self) -> str: ...
20+
1621
def json(self, **kwargs: Any) -> Any: ...
1722
def raise_for_status(self) -> None: ...
1823
def close(self) -> None: ...

datadog_checks_base/tests/base/utils/http/test_http_testing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,16 @@ def test_mock_response_normalize_leading_newline():
7070
response = MockHTTPResponse(content=content)
7171

7272
assert response.text == 'Actual content'
73+
74+
75+
def test_mock_response_ok_property():
76+
assert MockHTTPResponse(status_code=200).ok is True
77+
assert MockHTTPResponse(status_code=399).ok is True
78+
assert MockHTTPResponse(status_code=400).ok is False
79+
assert MockHTTPResponse(status_code=500).ok is False
80+
81+
82+
def test_mock_response_reason_property():
83+
assert MockHTTPResponse(status_code=200).reason == 'OK'
84+
assert MockHTTPResponse(status_code=404).reason == 'Not Found'
85+
assert MockHTTPResponse(status_code=999).reason == ''

traefik_mesh/datadog_checks/traefik_mesh/check.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from datadog_checks.base import AgentCheck, OpenMetricsBaseCheckV2
1111
from datadog_checks.base.utils.http_exceptions import HTTPConnectionError as _HTTPConnectionError
1212
from datadog_checks.base.utils.http_exceptions import HTTPStatusError
13+
from datadog_checks.base.utils.http_exceptions import HTTPTimeoutError as _HTTPTimeoutError
1314
from datadog_checks.traefik_mesh.config_models import ConfigMixin
1415
from datadog_checks.traefik_mesh.metrics import METRIC_MAP, RENAME_LABELS
1516

@@ -123,6 +124,6 @@ def _get_json(self, url):
123124
self.warning(
124125
"Couldn't connect to URL: %s with exception: %s. Please verify the address is reachable", url, e
125126
)
126-
except requests.exceptions.Timeout as e:
127+
except (requests.exceptions.Timeout, _HTTPTimeoutError) as e:
127128
self.warning("Connection timeout when connecting to %s: %s", url, e)
128129
return None

0 commit comments

Comments
 (0)