Skip to content

[DBMON-6669] Fix resolve_db_host for IPv6 loopback addresses#23849

Open
eric-weaver wants to merge 5 commits into
masterfrom
eric.weaver/DBMON-6669
Open

[DBMON-6669] Fix resolve_db_host for IPv6 loopback addresses#23849
eric-weaver wants to merge 5 commits into
masterfrom
eric.weaver/DBMON-6669

Conversation

@eric-weaver
Copy link
Copy Markdown
Contributor

@eric-weaver eric-weaver commented May 27, 2026

What does this PR do?

Fixes resolve_db_host in datadog-checks-base so loopback database endpoints use the agent hostname for metrics instead of the raw IP literal.

  • Adds _try_parse_db_host_ip to parse IPv4/IPv6 address literals (bracketed IPv6, zone-scoped IPv6 via IPv6Interface).
  • Adds _is_local_db_host to detect localhost, unix sockets, and loopback IPs (127.0.0.1, ::1, [::1], etc.).
  • When the DB host is local, returns datadog_agent.get_hostname() before gethostbyname, restoring correct host tags and datadog.yaml host-level tags on MySQL/Postgres/SQL Server metrics.
  • Expands and reorders test_resolve_db_host cases.

Motivation

Fixes DataDog/datadog-agent#51280: on Agent 7.79.0, MySQL metrics lost datadog.yaml host tags and used host:::1 when server was set to ::1. Agent 7.78.4 was unaffected.

Agent 7.79.0 bundles datadog-checks-base 37.35.x, which includes #23181: when gethostbyname fails, resolve_db_host returns the configured db_host rather than the agent hostname. That is correct for remote hostnames that fail DNS lookup, but loopback IP literals (e.g. ::1) also fail gethostbyname because it is IPv4-only—they must not be treated as DNS failures. This PR classifies loopback addresses as local before any name resolution, so we keep #23181’s semantics for genuinely unresolvable remote hosts while restoring the agent hostname for on-host database connections.

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Add qa/required if this PR needs QA validation, or qa/skip-qa if it does not. Exactly one of the two is required.
  • If you need to backport this PR to another branch, you can add the backport/<branch-name> label to the PR and it will automatically open a backport PR once this one is merged

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 80c7783c9f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread datadog_checks_base/datadog_checks/base/utils/db/utils.py Outdated
@datadog-official
Copy link
Copy Markdown
Contributor

datadog-official Bot commented May 27, 2026

Pipelines  Tests  Code Coverage

Fix all issues with BitsAI

⚠️ Warnings

🚦 5 Pipeline jobs failed

PR All | test / j06ca546 / SNMP   View in Datadog   GitHub Actions

🔄 Retry job. This looks flaky and may succeed on retry. Failed to resolve 'ddintegrations.blob.core.windows.net' during SNMP request due to NameResolutionError.

PR All | test / j46da136 / JBoss_WildFly   View in Datadog   GitHub Actions

🔄 Retry job. This looks flaky and may succeed on retry. Could not resolve: ddintegrations.blob.core.windows.net:443

PR All | test / j5a9585a / IBM ACE   View in Datadog   GitHub Actions

🔄 Retry job. This looks flaky and may succeed on retry. Could not resolve host: ddintegrations.blob.core.windows.net, leading to failure in retrieving necessary files.

View all 5 failed jobs.

🧪 20 Tests failed in 1 job

PR All | run   GitHub Actions

test_bulk_table from test_check.py   View in Datadog (Fix with Cursor)
HTTPSConnectionPool(host=&#39;ddintegrations.blob.core.windows.net&#39;, port=443): Max retries exceeded with url: /snmp/cisco-3850.snmprec (Caused by NameResolutionError(&#34;HTTPSConnection(host=&#39;ddintegrations.blob.core.windows.net&#39;, port=443): Failed to resolve &#39;ddintegrations.blob.core.windows.net&#39; ([Errno -2] Name or service not known)&#34;))
test_cast_metrics from test_check.py   View in Datadog (Fix with Cursor)
HTTPSConnectionPool(host=&#39;ddintegrations.blob.core.windows.net&#39;, port=443): Max retries exceeded with url: /snmp/cisco-3850.snmprec (Caused by NameResolutionError(&#34;HTTPSConnection(host=&#39;ddintegrations.blob.core.windows.net&#39;, port=443): Failed to resolve &#39;ddintegrations.blob.core.windows.net&#39; ([Errno -2] Name or service not known)&#34;))

View all 20 test failures

ℹ️ Info

No other issues found (see more)

❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 87.55% (+0.13%)

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 93c483d | Docs | Datadog PR Page | Give us feedback!

@eric-weaver eric-weaver added the qa/skip-qa Automatically skip this PR for the next QA label May 27, 2026
@eric-weaver eric-weaver changed the title [DBMON-6669] Fix resolve_db_host for local IPv6 and link-local addresses [DBMON-6669] Fix resolve_db_host for IPv6 loopback addresses May 27, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 27, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.95%. Comparing base (b5362c7) to head (9badf28).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

sethsamuel
sethsamuel previously approved these changes May 27, 2026
Kyle-Neale
Kyle-Neale previously approved these changes May 27, 2026
AAraKKe
AAraKKe previously approved these changes May 28, 2026
Copy link
Copy Markdown
Collaborator

@AAraKKe AAraKKe left a comment

Choose a reason for hiding this comment

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

Hi @eric-weaver, this review was generated with Claude Code and has been looked over and approved before posting. I believe the comments below are valid — feel free to discuss any you disagree with.

Comment legend

praise: no action needed, just celebrate!
note: informational, no action required.
question: seeking clarification or understanding your approach.
nit: minor, non-blocking (style, typo). Feel free to ignore.
suggestion: optional improvement, recommended but not required.
request: a change I believe is necessary before merging.

Comment thread datadog_checks_base/datadog_checks/base/utils/db/utils.py Outdated
host = db_host.strip()
if host.startswith('[') and host.endswith(']'):
host = host[1:-1]
if '%' in host:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Question: Is there a reason to route zone-scoped addresses through IPv6Interface(host).ip rather than ip_address(host) directly? In Python 3.9+, ip_address("fe80::1%eth0") returns an IPv6Address with scope_id set, and is_loopback works correctly on the result. IPv6Interface is semantically for CIDR network addresses and defaults to /128 when no prefix is given, so the detour works but is unusual. If this is a holdover from earlier Python compatibility, the pyproject.toml for datadog-checks-base targets 3.13 — ip_address for both branches would be a small simplification.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agree this simplifies it. Made the change

Comment thread datadog_checks_base/tests/base/utils/db/test_util.py Outdated
@temporal-github-worker-1 temporal-github-worker-1 Bot dismissed stale reviews from sethsamuel, Kyle-Neale, and AAraKKe May 29, 2026 16:38

Review from sethsamuel is dismissed. Related teams and files:

  • database-monitoring-agent
    • datadog_checks_base/datadog_checks/base/utils/db/utils.py
    • datadog_checks_base/tests/base/utils/db/test_util.py

Review from Kyle-Neale is dismissed. Related teams and files:

  • agent-integrations
    • datadog_checks_base/datadog_checks/base/utils/db/utils.py
    • datadog_checks_base/tests/base/utils/db/test_util.py

Review from AAraKKe is dismissed. Related teams and files:

  • agent-integrations
    • datadog_checks_base/datadog_checks/base/utils/db/utils.py
    • datadog_checks_base/tests/base/utils/db/test_util.py
@dd-octo-sts
Copy link
Copy Markdown
Contributor

dd-octo-sts Bot commented May 29, 2026

Validation Report

All 21 validations passed.

Show details
Validation Description Status
agent-reqs Verify check versions match the Agent requirements file
ci Validate CI configuration and code coverage settings
codeowners Validate every integration has a CODEOWNERS entry
config Validate default configuration files against spec.yaml
dep Verify dependency pins are consistent and Agent-compatible
http Validate integrations use the HTTP wrapper correctly
imports Validate check imports do not use deprecated modules
integration-style Validate check code style conventions
jmx-metrics Validate JMX metrics definition files and config
labeler Validate PR labeler config matches integration directories
legacy-signature Validate no integration uses the legacy Agent check signature
license-headers Validate Python files have proper license headers
licenses Validate third-party license attribution list
metadata Validate metadata.csv metric definitions
models Validate configuration data models match spec.yaml
openmetrics Validate OpenMetrics integrations disable the metric limit
package Validate Python package metadata and naming
qa-label Validate the pull request declares whether it needs QA for the next Agent release
readmes Validate README files have required sections
saved-views Validate saved view JSON file structure and fields
version Validate version consistency between package and changelog

View full run

@eric-weaver eric-weaver requested a review from AAraKKe May 29, 2026 19:36
@eric-weaver eric-weaver requested a review from sethsamuel May 29, 2026 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Missing tags from MySQL metrics

4 participants