Skip to content

Commit 005986e

Browse files
chore(profiling): add stub files for config (#15573)
## Description This improves static typing in Profiling modules by providing a correct stub file. Previously, `pyright` would be unhappy with typing when using `envier` `EnvVariable`'s – now it's OK.
1 parent bbef8e7 commit 005986e

File tree

4 files changed

+108
-13
lines changed

4 files changed

+108
-13
lines changed

ddtrace/internal/settings/_core.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from collections import ChainMap
22
from enum import Enum
33
import os
4-
from typing import Dict # noqa:F401
5-
from typing import Optional # noqa:F401
6-
from typing import Union # noqa:F401
4+
from typing import Dict
5+
from typing import Optional
76

87
from envier import Env
98

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from enum import Enum
2+
from typing import Dict
3+
from typing import Optional
4+
from typing import cast
5+
6+
from envier import Env
7+
8+
FLEET_CONFIG: Dict[str, str]
9+
LOCAL_CONFIG: Dict[str, str]
10+
FLEET_CONFIG_IDS: Dict[str, str]
11+
12+
class ValueSource(str, Enum):
13+
FLEET_STABLE_CONFIG = cast(str, ...)
14+
ENV_VAR = cast(str, ...)
15+
LOCAL_STABLE_CONFIG = cast(str, ...)
16+
CODE = cast(str, ...)
17+
DEFAULT = cast(str, ...)
18+
UNKNOWN = cast(str, ...)
19+
OTEL_ENV_VAR = cast(str, ...)
20+
21+
class DDConfig(Env):
22+
fleet_source: Dict[str, str]
23+
local_source: Dict[str, str]
24+
env_source: Dict[str, str]
25+
_value_source: Dict[str, str]
26+
config_id: Optional[str]
27+
28+
def __init__(
29+
self,
30+
source: Optional[Dict[str, str]] = None,
31+
parent: Optional[Env] = None,
32+
dynamic: Optional[Dict[str, str]] = None,
33+
) -> None: ...
34+
def value_source(self, env_name: str) -> str: ...

ddtrace/internal/settings/profiling.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
from __future__ import annotations
2+
13
import itertools
24
import math
35
import os
46
import typing as t
57

8+
from envier import Env
9+
610
from ddtrace.ext.git import COMMIT_SHA
711
from ddtrace.ext.git import MAIN_PACKAGE
812
from ddtrace.ext.git import REPOSITORY_URL
@@ -19,11 +23,14 @@
1923
logger = get_logger(__name__)
2024

2125

22-
def _derive_default_heap_sample_size(heap_config, default_heap_sample_size=1024 * 1024):
23-
# type: (ProfilingConfigHeap, int) -> int
26+
def _derive_default_heap_sample_size(
27+
heap_config: Env,
28+
default_heap_sample_size: int = 1024 * 1024,
29+
) -> int:
30+
assert isinstance(heap_config, ProfilingConfigHeap) # nosec: assert is used for type checking
2431
heap_sample_size = heap_config._sample_size
2532
if heap_sample_size is not None:
26-
return heap_sample_size
33+
return t.cast(int, heap_sample_size)
2734

2835
if not heap_config.enabled:
2936
return 0
@@ -363,28 +370,28 @@ class ProfilingConfigPytorch(DDConfig):
363370
logger.warning("Failed to load ddup module (%s), disabling profiling", msg)
364371
telemetry_writer.add_log(
365372
TELEMETRY_LOG_LEVEL.ERROR,
366-
"Failed to load ddup module (%s), disabling profiling" % ddup_failure_msg,
373+
f"Failed to load ddup module ({ddup_failure_msg}), disabling profiling",
367374
)
368-
config.enabled = False
375+
config.enabled = False # pyright: ignore[reportAttributeAccessIssue]
369376

370377
# We also need to check if stack_v2 module is available, and turn if off
371378
# if it s not.
372379
stack_v2_failure_msg, stack_v2_is_available = _check_for_stack_v2_available()
373-
if config.stack.enabled and not stack_v2_is_available:
380+
if config.stack.enabled and not stack_v2_is_available: # pyright: ignore[reportAttributeAccessIssue]
374381
msg = stack_v2_failure_msg or "stack_v2 not available"
375382
logger.warning("Failed to load stack_v2 module (%s), falling back to v1 stack sampler", msg)
376383
telemetry_writer.add_log(
377384
TELEMETRY_LOG_LEVEL.ERROR,
378385
"Failed to load stack_v2 module (%s), disabling profiling" % msg,
379386
)
380-
config.stack.enabled = False
387+
config.stack.enabled = False # pyright: ignore[reportAttributeAccessIssue]
381388

382389
# Enrich tags with git metadata and DD_TAGS
383-
config.tags = _enrich_tags(config.tags)
390+
config.tags = _enrich_tags(config.tags) # pyright: ignore[reportAttributeAccessIssue]
384391

385392

386-
def config_str(config):
387-
configured_features = []
393+
def config_str(config) -> str:
394+
configured_features: list[str] = []
388395
if config.stack.enabled:
389396
configured_features.append("stack_v2")
390397
if config.lock.enabled:
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from typing import Dict
2+
from typing import Optional
3+
4+
from ddtrace.internal.settings._core import DDConfig
5+
6+
class ProfilingConfig(DDConfig):
7+
enabled: bool
8+
agentless: bool
9+
code_provenance: bool
10+
endpoint_collection: bool
11+
output_pprof: Optional[str]
12+
upload_interval: float
13+
capture_pct: float
14+
max_frames: int
15+
ignore_profiler: bool
16+
max_time_usage_pct: float
17+
api_timeout_ms: int
18+
timeline_enabled: bool
19+
tags: Dict[str, str]
20+
enable_asserts: bool
21+
sample_pool_capacity: int
22+
stack: ProfilingConfigStack
23+
lock: ProfilingConfigLock
24+
memory: ProfilingConfigMemory
25+
heap: ProfilingConfigHeap
26+
pytorch: ProfilingConfigPytorch
27+
28+
class ProfilingConfigStack(DDConfig):
29+
enabled: bool
30+
v2_adaptive_sampling: bool
31+
32+
class ProfilingConfigLock(DDConfig):
33+
enabled: bool
34+
name_inspect_dir: bool
35+
36+
class ProfilingConfigMemory(DDConfig):
37+
enabled: bool
38+
events_buffer: int
39+
40+
class ProfilingConfigHeap(DDConfig):
41+
enabled: bool
42+
_sample_size: Optional[int]
43+
sample_size: int
44+
45+
class ProfilingConfigPytorch(DDConfig):
46+
enabled: bool
47+
events_limit: int
48+
49+
config: ProfilingConfig
50+
ddup_failure_msg: Optional[str]
51+
ddup_is_available: bool
52+
stack_v2_failure_msg: Optional[str]
53+
stack_v2_is_available: bool
54+
55+
def config_str(config: ProfilingConfig) -> str: ...

0 commit comments

Comments
 (0)