Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions ddtrace/_trace/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,17 @@ def _set_sampling_decision_maker(
self.context._meta[SAMPLING_DECISION_TRACE_TAG_KEY] = value
return value

def set_tag(self, key: str, value: Optional[str] = None) -> None:
"""Set a tag key/value pair on the span."""
def set_tag(self, key: str, value: Any = None) -> None:
"""Set a tag key/value pair on the span.

Boolean and bytes values are stored as their string representation.
``int`` and ``float`` values are stored as metrics (see ``set_metric``).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Qualify the numeric-to-metric guarantee

For integers outside the signed 64-bit range or non-finite floats, this new public documentation is incorrect: extract_attribute in src/native/span/span_data.rs stringifies overflowing integers and drops NaN/Infinity rather than storing them as metrics. Callers relying on this documented guarantee will therefore find a string tag or no value at all, so the sentence should state the supported numeric range and finite-value restriction. This docstring is customer-facing because Span is exposed through ddtrace.trace.

AGENTS.md reference: AGENTS.md:L25-L30

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Confirmed — thanks, this is a fair catch on a line I added. Fixed in a79c7c6.

I verified it against extract_attribute in src/native/span/span_data.rs (overflowing ints fall through to the str() fallback; NaN/Inf return None) and then measured it through the public set_tag path on 4.13.1:

s.set_tag("small_int", 2**53 + 1)
s.set_tag("i64_max", 2**63 - 1)
s.set_tag("overflow_int", 2**63)
s.set_tag("big_neg", -(2**63) - 1)
s.set_tag("nan", float("nan"))
s.set_tag("inf", float("inf"))
s.set_tag("plain_float", 1.5)
METRICS: {'plain_float': 1.5, 'i64_max': 9223372036854775807, 'small_int': 9007199254740993}
TAGS:    {'overflow_int': '9223372036854775808', 'big_neg': '-9223372036854775809'}

So integers beyond the signed 64-bit range become string tags, and NaN/infinite floats are dropped from both get_metrics() and get_tags() — my unqualified sentence was wrong on both counts.

One addition: there is a third exception the comment doesn't mention. http.status_code has a dedicated always-stringify path in extract_attribute, so an int there is also not stored as a metric:

s.set_tag("http.status_code", 200)
METRICS: {}
TAGS:    {'http.status_code': '200'}

The docstring now states the 64-bit range, the finite-value restriction, and the http.status_code case.

"""
# Explicitly try to convert expected integers to `int`
# DEV: Some integrations parse these values from strings, but don't call `int(value)` themselves
if key == net.TARGET_PORT:
try:
value = int(value) # type: ignore
value = int(value)
except (ValueError, TypeError):
pass

Expand All @@ -261,14 +265,14 @@ def set_tag(self, key: str, value: Optional[str] = None) -> None:
elif key == SERVICE_VERSION_KEY:
# Also set the `version` tag to the same value
# DEV: Note that we do no return, we want to set both
self._set_attribute(VERSION_KEY, value) # type: ignore[arg-type]
self._set_attribute(VERSION_KEY, value)
elif key == _SPAN_MEASURED_KEY:
# Set `_dd.measured` tag as a metric
# DEV: `set_metric` will ensure it is an integer 0 or 1
if value is None:
value = 1 # type: ignore
value = 1

self.set_metric(key, value) # type: ignore[arg-type] # ast-grep-ignore: span-set-metric
self.set_metric(key, value) # ast-grep-ignore: span-set-metric
return

if isinstance(key, bytes):
Expand All @@ -278,7 +282,7 @@ def set_tag(self, key: str, value: Optional[str] = None) -> None:
value = str(value)

try:
self._set_attribute(key, value) # type: ignore[arg-type]
self._set_attribute(key, value)
except Exception:
log.warning("error setting tag %s, ignoring it", key, exc_info=True)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
tracing: This fix resolves an issue where type checkers rejected valid application code passing
non-string values to ``Span.set_tag`` (for example ``span.set_tag("custom.tag", True)``). The
``value`` parameter was annotated as ``Optional[str]``, but the implementation accepts the
historic domain: booleans and bytes are stringified into tags, and numeric values are stored
as metrics.