Skip to content

Upgrade Datadog plugin to ddtrace 4.x public API#1563

Open
Dev-iL wants to merge 2 commits intoapache:mainfrom
SummitSG-LLC:2604/upgrade_ddtrace
Open

Upgrade Datadog plugin to ddtrace 4.x public API#1563
Dev-iL wants to merge 2 commits intoapache:mainfrom
SummitSG-LLC:2604/upgrade_ddtrace

Conversation

@Dev-iL
Copy link
Copy Markdown
Collaborator

@Dev-iL Dev-iL commented Apr 26, 2026

related: #1288
fixes: #1562

ddtrace 3.0 removed Span and context from the top-level package. Migrate to the canonical ddtrace.trace module, bump the version constraint to >=4.0, fix stale sf-hamilton package references, and add smoke tests for the plugin.

Summary

  • Migrate h_ddog.py imports from removed top-level ddtrace.Span/ddtrace.context to the canonical ddtrace.trace module (ddtrace.trace.Span, ddtrace.trace.Context)
  • Bump version constraint from ddtrace<3.0 to ddtrace>=4.0 in both the datadog optional dependency and the docs dependency group
  • Fix stale sf-hamilton package references to apache-hamilton in the ImportError message and DDOGTracer docstring
  • Remove the outdated TODO comment about ddtrace 3.0 deprecation
  • Add smoke tests for the plugin (tests/plugins/test_h_ddog.py)

Motivation

ddtrace 3.0 removed Span and context from the top-level ddtrace package (DataDog/dd-trace-py#12186). The Hamilton plugin was pinned to ddtrace<3.0 as a temporary workaround. This PR migrates to the stable public API at ddtrace.trace, which is the canonical location in ddtrace 4.x.

Changes

File What changed
hamilton/plugins/h_ddog.py from ddtrace import Span, context, tracer split into from ddtrace import tracer + from ddtrace.trace import Context, Span; context.Context replaced with Context in type annotations and constructor calls; TODO comment removed; sf-hamilton references updated to apache-hamilton
pyproject.toml ddtrace<3.0 changed to ddtrace>=4.0 in [project.optional-dependencies].datadog and docs
tests/plugins/test_h_ddog.py New file with 7 tests covering span lifecycle (happy path, error path, task-based), AsyncDDOGTracer instantiation, and serialization/deserialization round-trip

No breaking changes

The public API surface of DDOGTracer and AsyncDDOGTracer is unchanged: same constructor signatures, same methods, same base class hierarchy. Only internal import paths changed.

Test plan

  • DDOGTracer span lifecycle: before_graph -> before_node -> after_node -> after_graph
  • Error propagation through span __exit__
  • Task-based span lifecycle with before_task/after_task
  • AsyncDDOGTracer instantiation with all parameter combinations
  • __getstate__/__setstate__ serialization round-trip
  • _serialize_span_dict/_deserialize_span_dict round-trip
  • Module imports successfully with ddtrace 4.4.0
  • No stale ddtrace<3.0 pins or sf-hamilton references remain

Checklist

  • PR has an informative and human-readable title (this will be pulled into the release notes)
  • Changes are limited to a single goal (no scope creep)
  • Code passed the pre-commit check & code is left cleaner/nicer than when first encountered.
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output)
  • Placeholder code is flagged / future TODOs are captured in comments
  • Project documentation has been updated if adding/changing functionality.

@Dev-iL Dev-iL force-pushed the 2604/upgrade_ddtrace branch from 64b35ca to 1dcfe4a Compare April 26, 2026 12:45
Copy link
Copy Markdown
Contributor

@skrawcz skrawcz left a comment

Choose a reason for hiding this comment

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

Great work upgrading the Datadog plugin to the ddtrace 4.x API! The migration from ddtrace.Span/ddtrace.context to ddtrace.trace.Span/ddtrace.trace.Context is clean and correct. The tests are well-structured and provide good coverage of the core lifecycle.

One blocking issue: the uv.lock file at the repository root appears to be accidentally included — it's 9,425 lines and doesn't exist on main. The root of the repository doesn't use uv for dependency management (only ui/backend/ does). Please remove it from the PR (git rm uv.lock and amend or add a new commit). It accounts for 9,425 of the 9,585 additions.

Suggestions (non-blocking):

  1. Consider pinning to ddtrace>=4.0,<5.0 instead of unbounded >=4.0 — the jump from 2.x→4.x already demonstrated that ddtrace major versions can have breaking import changes. An upper bound would prevent future breakage without requiring another PR.

  2. In test_serialize_deserialize_span_dict, you could additionally assert the type: assert isinstance(deserialized["key1"], Context) to verify the deserialized values are actually Context instances, not just that the key exists.

Also a nice bonus: the sf-hamiltonapache-hamilton references and the ddogdatadog extra name fix in the docstring. 👍

Local test results: 7/7 tests pass with ddtrace 4.8.0, ruff check and format both pass clean.

ddtrace 3.0 removed `Span` and `context` from the top-level package.
Migrate to the canonical `ddtrace.trace` module, bump the version constraint to `>=4.0,<5`, fix stale `sf-hamilton` package references, and add smoke tests for the plugin.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Dev-iL Dev-iL force-pushed the 2604/upgrade_ddtrace branch from 1dcfe4a to a53314e Compare May 3, 2026 19:25
@Dev-iL Dev-iL requested a review from skrawcz May 3, 2026 19:26
@Dev-iL
Copy link
Copy Markdown
Collaborator Author

Dev-iL commented May 3, 2026

@skrawcz Feedback addressed + fixed some random CI issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

uv sync fails because of ddtrace

2 participants