Skip to content

Commit d40de8a

Browse files
chore(tracing): removes constants from the public api [3.0] [backport 2.20] (#12074)
Backport b90fa38 from #12010 to 2.20. ## Description Prefixes constants that are internal to the `ddtrace` library with an underscore. These constants are not publicly documented and should not be referenced by users. Using the deprecated constants will log a deprecation warning in 2.X and an error in 3.X ## Changes - Update constants in `ddtrace/contants.py`: https://github.com/DataDog/dd-trace-py/pull/12010/files#diff-9fdb2ad4ee57c87a82b368d46e86cbc4726001da7e23cff7404f694813644cc6 - Ensure deprecated constants are not used in ddtrace internals ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - 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) Co-authored-by: Munir Abdinur <[email protected]>
1 parent 333a9b5 commit d40de8a

File tree

88 files changed

+332
-295
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+332
-295
lines changed

ddtrace/_trace/context.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
from ddtrace._trace._span_link import SpanLink
1313
from ddtrace._trace.types import _MetaDictType
1414
from ddtrace._trace.types import _MetricDictType
15-
from ddtrace.constants import ORIGIN_KEY
16-
from ddtrace.constants import SAMPLING_PRIORITY_KEY
17-
from ddtrace.constants import USER_ID_KEY
15+
from ddtrace.constants import _ORIGIN_KEY
16+
from ddtrace.constants import _SAMPLING_PRIORITY_KEY
17+
from ddtrace.constants import _USER_ID_KEY
1818
from ddtrace.internal.compat import NumericType
1919
from ddtrace.internal.constants import MAX_UINT_64BITS as _MAX_UINT_64BITS
2020
from ddtrace.internal.constants import W3C_TRACEPARENT_KEY
@@ -72,9 +72,9 @@ def __init__(
7272
self._is_remote: bool = is_remote
7373

7474
if dd_origin is not None and _DD_ORIGIN_INVALID_CHARS_REGEX.search(dd_origin) is None:
75-
self._meta[ORIGIN_KEY] = dd_origin
75+
self._meta[_ORIGIN_KEY] = dd_origin
7676
if sampling_priority is not None:
77-
self._metrics[SAMPLING_PRIORITY_KEY] = sampling_priority
77+
self._metrics[_SAMPLING_PRIORITY_KEY] = sampling_priority
7878
if span_links is not None:
7979
self._span_links = span_links
8080
else:
@@ -127,16 +127,16 @@ def _update_tags(self, span: "Span") -> None:
127127
@property
128128
def sampling_priority(self) -> Optional[NumericType]:
129129
"""Return the context sampling priority for the trace."""
130-
return self._metrics.get(SAMPLING_PRIORITY_KEY)
130+
return self._metrics.get(_SAMPLING_PRIORITY_KEY)
131131

132132
@sampling_priority.setter
133133
def sampling_priority(self, value: Optional[NumericType]) -> None:
134134
with self._lock:
135135
if value is None:
136-
if SAMPLING_PRIORITY_KEY in self._metrics:
137-
del self._metrics[SAMPLING_PRIORITY_KEY]
136+
if _SAMPLING_PRIORITY_KEY in self._metrics:
137+
del self._metrics[_SAMPLING_PRIORITY_KEY]
138138
return
139-
self._metrics[SAMPLING_PRIORITY_KEY] = value
139+
self._metrics[_SAMPLING_PRIORITY_KEY] = value
140140

141141
@property
142142
def _traceparent(self) -> str:
@@ -180,22 +180,22 @@ def _tracestate(self) -> str:
180180
@property
181181
def dd_origin(self) -> Optional[Text]:
182182
"""Get the origin of the trace."""
183-
return self._meta.get(ORIGIN_KEY)
183+
return self._meta.get(_ORIGIN_KEY)
184184

185185
@dd_origin.setter
186186
def dd_origin(self, value: Optional[Text]) -> None:
187187
"""Set the origin of the trace."""
188188
with self._lock:
189189
if value is None:
190-
if ORIGIN_KEY in self._meta:
191-
del self._meta[ORIGIN_KEY]
190+
if _ORIGIN_KEY in self._meta:
191+
del self._meta[_ORIGIN_KEY]
192192
return
193-
self._meta[ORIGIN_KEY] = value
193+
self._meta[_ORIGIN_KEY] = value
194194

195195
@property
196196
def dd_user_id(self) -> Optional[Text]:
197197
"""Get the user ID of the trace."""
198-
user_id = self._meta.get(USER_ID_KEY)
198+
user_id = self._meta.get(_USER_ID_KEY)
199199
if user_id:
200200
return str(base64.b64decode(user_id), encoding="utf-8")
201201
return None
@@ -205,10 +205,10 @@ def dd_user_id(self, value: Optional[Text]) -> None:
205205
"""Set the user ID of the trace."""
206206
with self._lock:
207207
if value is None:
208-
if USER_ID_KEY in self._meta:
209-
del self._meta[USER_ID_KEY]
208+
if _USER_ID_KEY in self._meta:
209+
del self._meta[_USER_ID_KEY]
210210
return
211-
self._meta[USER_ID_KEY] = str(base64.b64encode(bytes(value, encoding="utf-8")), encoding="utf-8")
211+
self._meta[_USER_ID_KEY] = str(base64.b64encode(bytes(value, encoding="utf-8")), encoding="utf-8")
212212

213213
@property
214214
def _trace_id_64bits(self):

ddtrace/_trace/processor/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ddtrace._trace.span import _get_64_highest_order_bits_as_hex
1515
from ddtrace._trace.span import _is_top_level
1616
from ddtrace.constants import _APM_ENABLED_METRIC_KEY as MK_APM_ENABLED
17-
from ddtrace.constants import SAMPLING_PRIORITY_KEY
17+
from ddtrace.constants import _SAMPLING_PRIORITY_KEY
1818
from ddtrace.constants import USER_KEEP
1919
from ddtrace.internal import gitmetadata
2020
from ddtrace.internal import telemetry
@@ -165,7 +165,7 @@ def process_trace(self, trace: List[Span]) -> Optional[List[Span]]:
165165
# In order to ensure that the agent does not update priority sampling rates
166166
# due to single spans sampling, we set all of these spans to manual keep.
167167
if config._trace_compute_stats:
168-
span.set_metric(SAMPLING_PRIORITY_KEY, USER_KEEP)
168+
span.set_metric(_SAMPLING_PRIORITY_KEY, USER_KEEP)
169169
break
170170

171171
return trace

ddtrace/_trace/sampler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from typing import Tuple # noqa:F401
1313

1414
from ddtrace import config
15-
from ddtrace.constants import SAMPLING_LIMIT_DECISION
15+
from ddtrace.constants import _SAMPLING_LIMIT_DECISION
1616

1717
from ..constants import ENV_KEY
1818
from ..internal.constants import _PRIORITY_CATEGORY
@@ -342,7 +342,7 @@ def sample(self, span):
342342
# uses DatadogSampler._rate_limit_always_on to override this functionality.
343343
if sampled:
344344
sampled = self.limiter.is_allowed()
345-
span.set_metric(SAMPLING_LIMIT_DECISION, self.limiter.effective_rate)
345+
span.set_metric(_SAMPLING_LIMIT_DECISION, self.limiter.effective_rate)
346346
_set_sampling_tags(
347347
span,
348348
sampled,

ddtrace/_trace/span.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@
2424
from ddtrace._trace.types import _MetricDictType
2525
from ddtrace._trace.types import _TagNameType
2626
from ddtrace.constants import _ANALYTICS_SAMPLE_RATE_KEY
27+
from ddtrace.constants import _SAMPLING_AGENT_DECISION
28+
from ddtrace.constants import _SAMPLING_LIMIT_DECISION
29+
from ddtrace.constants import _SAMPLING_RULE_DECISION
30+
from ddtrace.constants import _SPAN_MEASURED_KEY
2731
from ddtrace.constants import ERROR_MSG
2832
from ddtrace.constants import ERROR_STACK
2933
from ddtrace.constants import ERROR_TYPE
3034
from ddtrace.constants import MANUAL_DROP_KEY
3135
from ddtrace.constants import MANUAL_KEEP_KEY
32-
from ddtrace.constants import SAMPLING_AGENT_DECISION
33-
from ddtrace.constants import SAMPLING_LIMIT_DECISION
34-
from ddtrace.constants import SAMPLING_RULE_DECISION
3536
from ddtrace.constants import SERVICE_KEY
3637
from ddtrace.constants import SERVICE_VERSION_KEY
37-
from ddtrace.constants import SPAN_MEASURED_KEY
3838
from ddtrace.constants import USER_KEEP
3939
from ddtrace.constants import USER_REJECT
4040
from ddtrace.constants import VERSION_KEY
@@ -327,7 +327,7 @@ def _override_sampling_decision(self, decision: Optional[NumericType]):
327327
self.context.sampling_priority = decision
328328
set_sampling_decision_maker(self.context, SamplingMechanism.MANUAL)
329329
if self._local_root:
330-
for key in (SAMPLING_RULE_DECISION, SAMPLING_AGENT_DECISION, SAMPLING_LIMIT_DECISION):
330+
for key in (_SAMPLING_RULE_DECISION, _SAMPLING_AGENT_DECISION, _SAMPLING_LIMIT_DECISION):
331331
if key in self._local_root._metrics:
332332
del self._local_root._metrics[key]
333333

@@ -401,7 +401,7 @@ def set_tag(self, key: _TagNameType, value: Any = None) -> None:
401401
# Also set the `version` tag to the same value
402402
# DEV: Note that we do no return, we want to set both
403403
self.set_tag(VERSION_KEY, value)
404-
elif key == SPAN_MEASURED_KEY:
404+
elif key == _SPAN_MEASURED_KEY:
405405
# Set `_dd.measured` tag as a metric
406406
# DEV: `set_metric` will ensure it is an integer 0 or 1
407407
if value is None:
@@ -458,7 +458,7 @@ def set_tags(self, tags: Dict[_TagNameType, Any]) -> None:
458458
def set_metric(self, key: _TagNameType, value: NumericType) -> None:
459459
"""This method sets a numeric tag value for the given key."""
460460
# Enforce a specific constant for `_dd.measured`
461-
if key == SPAN_MEASURED_KEY:
461+
if key == _SPAN_MEASURED_KEY:
462462
try:
463463
value = int(bool(value))
464464
except (ValueError, TypeError):

ddtrace/_trace/trace_handlers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
)
1919
from ddtrace._trace.utils_botocore.span_tags import set_botocore_response_metadata_tags
2020
from ddtrace.constants import _ANALYTICS_SAMPLE_RATE_KEY
21+
from ddtrace.constants import _SPAN_MEASURED_KEY
2122
from ddtrace.constants import SPAN_KIND
22-
from ddtrace.constants import SPAN_MEASURED_KEY
2323
from ddtrace.contrib import trace_utils
2424
from ddtrace.contrib.internal.botocore.constants import BOTOCORE_STEPFUNCTIONS_INPUT_KEY
2525
from ddtrace.contrib.internal.trace_utils import _set_url_tag
@@ -334,7 +334,7 @@ def _on_request_span_modifier(
334334
# RequestContext` and possibly a url rule
335335
span.resource = " ".join((request.method, request.path))
336336

337-
span.set_tag(SPAN_MEASURED_KEY)
337+
span.set_tag(_SPAN_MEASURED_KEY)
338338
# set analytics sample rate with global config enabled
339339
sample_rate = flask_config.get_analytics_sample_rate(use_global_config=True)
340340
if sample_rate is not None:
@@ -366,7 +366,7 @@ def _on_request_span_modifier_post(ctx, flask_config, request, req_body):
366366

367367
def _on_traced_get_response_pre(_, ctx: core.ExecutionContext, request, before_request_tags):
368368
before_request_tags(ctx["pin"], ctx.span, request)
369-
ctx.span._metrics[SPAN_MEASURED_KEY] = 1
369+
ctx.span._metrics[_SPAN_MEASURED_KEY] = 1
370370

371371

372372
def _on_django_finalize_response_pre(ctx, after_request_tags, request, response):

ddtrace/_trace/tracer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
from ddtrace._trace.sampler import DatadogSampler
3131
from ddtrace._trace.span import Span
3232
from ddtrace.appsec._constants import APPSEC
33+
from ddtrace.constants import _HOSTNAME_KEY
3334
from ddtrace.constants import ENV_KEY
34-
from ddtrace.constants import HOSTNAME_KEY
3535
from ddtrace.constants import PID
3636
from ddtrace.constants import VERSION_KEY
3737
from ddtrace.internal import agent
@@ -966,7 +966,7 @@ def _start_span(
966966
on_finish=[self._on_span_finish],
967967
)
968968
if config._report_hostname:
969-
span.set_tag_str(HOSTNAME_KEY, hostname.get_hostname())
969+
span.set_tag_str(_HOSTNAME_KEY, hostname.get_hostname())
970970

971971
if not span._parent:
972972
span.set_tag_str("runtime-id", get_runtime_id())

ddtrace/_trace/utils_botocore/span_tags.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from ddtrace import config
88
from ddtrace._trace.utils_botocore.aws_payload_tagging import AWSPayloadTagging
99
from ddtrace.constants import _ANALYTICS_SAMPLE_RATE_KEY
10+
from ddtrace.constants import _SPAN_MEASURED_KEY
1011
from ddtrace.constants import SPAN_KIND
11-
from ddtrace.constants import SPAN_MEASURED_KEY
1212
from ddtrace.ext import SpanKind
1313
from ddtrace.ext import aws
1414
from ddtrace.ext import http
@@ -23,7 +23,7 @@ def set_botocore_patched_api_call_span_tags(span: Span, instance, args, params,
2323
span.set_tag_str(COMPONENT, config.botocore.integration_name)
2424
# set span.kind to the type of request being performed
2525
span.set_tag_str(SPAN_KIND, SpanKind.CLIENT)
26-
span.set_tag(SPAN_MEASURED_KEY)
26+
span.set_tag(_SPAN_MEASURED_KEY)
2727

2828
if args:
2929
# DEV: join is the fastest way of concatenating strings that is compatible

ddtrace/_trace/utils_redis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from typing import Optional
88

99
from ddtrace.constants import _ANALYTICS_SAMPLE_RATE_KEY
10+
from ddtrace.constants import _SPAN_MEASURED_KEY
1011
from ddtrace.constants import SPAN_KIND
11-
from ddtrace.constants import SPAN_MEASURED_KEY
1212
from ddtrace.contrib import trace_utils
1313
from ddtrace.contrib.internal.redis_utils import _extract_conn_tags
1414
from ddtrace.ext import SpanKind
@@ -30,7 +30,7 @@ def _set_span_tags(
3030
span.set_tag_str(SPAN_KIND, SpanKind.CLIENT)
3131
span.set_tag_str(COMPONENT, config_integration.integration_name)
3232
span.set_tag_str(db.SYSTEM, redisx.APP)
33-
span.set_tag(SPAN_MEASURED_KEY)
33+
span.set_tag(_SPAN_MEASURED_KEY)
3434
if query is not None:
3535
span_name = schematize_cache_operation(redisx.RAWCMD, cache_provider=redisx.APP) # type: ignore[operator]
3636
span.set_tag_str(span_name, query)

ddtrace/appsec/_iast/_iast_request_context.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ddtrace.appsec._iast._taint_tracking._context import create_context as create_propagation_context
1515
from ddtrace.appsec._iast._taint_tracking._context import reset_context as reset_propagation_context
1616
from ddtrace.appsec._iast.reporter import IastSpanReporter
17-
from ddtrace.constants import ORIGIN_KEY
17+
from ddtrace.constants import _ORIGIN_KEY
1818
from ddtrace.internal import core
1919
from ddtrace.internal.logger import get_logger
2020
from ddtrace.internal.utils.formats import asbool
@@ -133,8 +133,8 @@ def _create_and_attach_iast_report_to_span(req_span: Span, existing_data: Option
133133
set_iast_request_enabled(False)
134134
end_iast_context(req_span)
135135

136-
if req_span.get_tag(ORIGIN_KEY) is None:
137-
req_span.set_tag_str(ORIGIN_KEY, APPSEC.ORIGIN_VALUE)
136+
if req_span.get_tag(_ORIGIN_KEY) is None:
137+
req_span.set_tag_str(_ORIGIN_KEY, APPSEC.ORIGIN_VALUE)
138138

139139
oce.release_request()
140140

ddtrace/appsec/_processor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
from ddtrace.appsec._exploit_prevention.stack_traces import report_stack
2828
from ddtrace.appsec._trace_utils import _asm_manual_keep
2929
from ddtrace.appsec._utils import has_triggers
30-
from ddtrace.constants import ORIGIN_KEY
31-
from ddtrace.constants import RUNTIME_FAMILY
30+
from ddtrace.constants import _ORIGIN_KEY
31+
from ddtrace.constants import _RUNTIME_FAMILY
3232
from ddtrace.ext import SpanTypes
3333
from ddtrace.internal import core
3434
from ddtrace.internal._unpatched import unpatched_open as open # noqa: A001
@@ -235,7 +235,7 @@ def on_span_start(self, span: Span) -> None:
235235
headers_case_sensitive = _asm_request_context.get_headers_case_sensitive()
236236

237237
span.set_metric(APPSEC.ENABLED, 1.0)
238-
span.set_tag_str(RUNTIME_FAMILY, "python")
238+
span.set_tag_str(_RUNTIME_FAMILY, "python")
239239

240240
def waf_callable(custom_data=None, **kwargs):
241241
return self._waf_action(span._local_root or span, ctx, custom_data, **kwargs)
@@ -391,8 +391,8 @@ def _waf_action(
391391
# Right now, we overwrite any value that could be already there. We need to reconsider when ASM/AppSec's
392392
# specs are updated.
393393
_asm_manual_keep(span)
394-
if span.get_tag(ORIGIN_KEY) is None:
395-
span.set_tag_str(ORIGIN_KEY, APPSEC.ORIGIN_VALUE)
394+
if span.get_tag(_ORIGIN_KEY) is None:
395+
span.set_tag_str(_ORIGIN_KEY, APPSEC.ORIGIN_VALUE)
396396
return waf_results
397397

398398
def _is_needed(self, address: str) -> bool:

0 commit comments

Comments
 (0)