Skip to content

Commit

Permalink
Tighten things up
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchda committed Mar 1, 2024
1 parent cc71c35 commit cd95027
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 39 deletions.
2 changes: 1 addition & 1 deletion ddtrace/internal/datadog/profiling/ddup/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ set_target_properties(${EXTENSION_NAME} PROPERTIES SUFFIX "")

# RPATH is needed for sofile discovery at runtime, since Python packages are not
# installed in the system path. This is typical.
set_target_properties(${EXTENSION_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN")
set_target_properties(${EXTENSION_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/..")
target_include_directories(${EXTENSION_NAME} PRIVATE
../dd_wrapper/include
${Datadog_INCLUDE_DIRS}
Expand Down
16 changes: 14 additions & 2 deletions ddtrace/internal/datadog/profiling/ddup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@

try:
from ._ddup import * # noqa: F403, F401
except Exception:

def is_available():
# type: () -> bool
return True

except Exception as e:
from typing import Dict # noqa:F401
from typing import Optional # noqa:F401

from ddtrace._trace.span import Span # noqa:F401
from ddtrace.internal.logger import get_logger

LOG = get_logger(__name__)
LOG.error("Failed to import _ddup: %s", e)

def is_available():
# type: () -> bool
return False

# Decorator for not-implemented
def not_implemented(func):
Expand Down
4 changes: 2 additions & 2 deletions ddtrace/internal/datadog/profiling/stack_v2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.19)
# The exact name of this extension determines aspects of the installation and build paths,
# which need to be kept in sync with setup.py. Accordingly, take the name passed in by the
# caller, defaulting to "stack_v2" if needed.
set(EXTENSION_NAME "stack_v2" CACHE STRING "Name of the extension")
set(EXTENSION_NAME "_stack_v2" CACHE STRING "Name of the extension")
project(${EXTENSION_NAME})
message(STATUS "Building extension: ${EXTENSION_NAME}")

Expand Down Expand Up @@ -88,7 +88,7 @@ set_target_properties(${EXTENSION_NAME} PROPERTIES SUFFIX "")

# RPATH is needed for sofile discovery at runtime, since Python packages are not
# installed in the system path. This is typical.
set_target_properties(${EXTENSION_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN")
set_target_properties(${EXTENSION_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/..")
target_link_libraries(${EXTENSION_NAME} PRIVATE
dd_wrapper
)
Expand Down
33 changes: 33 additions & 0 deletions ddtrace/internal/datadog/profiling/stack_v2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
try:
from ._stack_v2 import * # noqa: F401, F403

def is_available():
# type: () -> bool
return True

except Exception as e:
from ddtrace.internal.logger import get_logger

LOG = get_logger(__name__)
LOG.error("Failed to import _stack_v2: %s", e)

def is_available():
# type: () -> bool
return False

# Decorator for not-implemented
def not_implemented(func):
def wrapper(*args, **kwargs):
raise NotImplementedError("{} is not implemented on this platform".format(func.__name__))

@not_implemented
def start(*args, **kwargs):
pass

@not_implemented
def stop(*args, **kwargs):
pass

@not_implemented
def set_interval(*args, **kwargs):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static PyMethodDef _stack_v2_methods[] = {
};

PyMODINIT_FUNC
PyInit_stack_v2(void)
PyInit__stack_v2(void)
{
PyObject* m;
static struct PyModuleDef moduledef = {
Expand Down
34 changes: 21 additions & 13 deletions ddtrace/profiling/collector/stack.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ from ddtrace import context
from ddtrace import span as ddspan
from ddtrace.internal import compat
from ddtrace.internal.datadog.profiling import ddup
from ddtrace.internal.datadog.profiling import stack_v2
from ddtrace.internal.utils import formats
from ddtrace.profiling import _threading
from ddtrace.profiling import collector
from ddtrace.profiling.collector import _task
from ddtrace.profiling.collector import _traceback
from ddtrace.profiling.collector import stack_event
from ddtrace.profiling.collector import stack_v2
from ddtrace.settings.profiling import config


Expand Down Expand Up @@ -486,11 +486,14 @@ class StackCollector(collector.PeriodicCollector):
self.tracer.context_provider._on_activate(self._thread_span_links.link_span)

# If stack_v2 is enabled, verify that it loaded properly. If not, fallback to the v1 stack collector
# and log a warning.
# and log an error.
if self._stack_collector_v2_enabled:
if not stack_v2.is_available():
self._stack_collector_v2_enabled = False
LOG.warning("Failed to load the v2 stack collector; falling back to the v1 stack collector")
LOG.error("Failed to load the v2 stack sampler; falling back to the v1 stack sampler")
if not ddup.is_available():
self._stack_collector_v2_enabled = False
LOG.error("Failed to load the libdd collector; falling back to the v1 stack sampler")

# Force-set use_libdd if the v2 stack collector is enabled
if self._stack_collector_v2_enabled:
Expand All @@ -501,6 +504,8 @@ class StackCollector(collector.PeriodicCollector):
# by the next sample.
stack_v2.start(min_interval=self.min_interval_time)
else:
if config.export.libdd_enabled and not ddup.is_available():
LOG.error("Failed to load the libdd collector; falling back to legacy collector")
set_use_libdd(config.export.libdd_enabled)

def _start_service(self):
Expand Down Expand Up @@ -532,16 +537,19 @@ class StackCollector(collector.PeriodicCollector):
now = compat.monotonic_ns()
wall_time = now - self._last_wall_time
self._last_wall_time = now

all_events = stack_collect(
self.ignore_profiler,
self._thread_time,
self.nframes,
self.interval,
wall_time,
self._thread_span_links,
self.endpoint_collection_enabled,
)
all_events = []

# If the stack v2 collector is enabled, then do not collect the stack samples here.
if not self._stack_collector_v2_enabled:
all_events = stack_collect(
self.ignore_profiler,
self._thread_time,
self.nframes,
self.interval,
wall_time,
self._thread_span_links,
self.endpoint_collection_enabled,
)

used_wall_time_ns = compat.monotonic_ns() - now
self.interval = self._compute_new_interval(used_wall_time_ns)
Expand Down
19 changes: 0 additions & 19 deletions ddtrace/profiling/collector/stack_v2.py

This file was deleted.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def get_exts_for(name):
if sys.version_info >= (3, 8):
ext_modules.append(
CMakeExtension(
"ddtrace.internal.datadog.profiling.stack_v2",
"ddtrace.internal.datadog.profiling.stack_v2._stack_v2",
source_dir=STACK_V2_DIR,
cmake_args=[
"-DPROFILING_ROOT={}".format(PROF_NATIVE_DIR),
Expand Down

0 comments on commit cd95027

Please sign in to comment.