Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Spark --app-id as container name #871

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
60 changes: 53 additions & 7 deletions gprofiler/containers_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,28 @@
logger = get_logger_adapter(__name__)


class ContainerNamesClient:
class ContainerNamesClientBase:
def __init__(self) -> None:
self._current_container_names: Set[str] = set()

def get_container_name(self, pid: int) -> str:
raise NotImplementedError

def reset_cache(self) -> None:
self._current_container_names.clear()

@property
def container_names(self) -> List[str]:
return list(self._current_container_names)


class ContainerNamesClient(ContainerNamesClientBase):
"""
Container names are from Docker & CRI containers.
"""

def __init__(self) -> None:
super().__init__()
try:
self._containers_client: Optional[ContainersClient] = ContainersClient()
logger.info(f"Discovered container runtimes: {self._containers_client.get_runtimes()}")
Expand All @@ -30,16 +50,11 @@ def __init__(self) -> None:
self._containers_client = None

self._pid_to_container_name_cache: Dict[int, str] = {}
self._current_container_names: Set[str] = set()
self._container_id_to_name_cache: Dict[str, Optional[str]] = {}

def reset_cache(self) -> None:
super().reset_cache()
self._pid_to_container_name_cache.clear()
self._current_container_names.clear()

@property
def container_names(self) -> List[str]:
return list(self._current_container_names)

def get_container_name(self, pid: int) -> str:
if self._containers_client is None:
Expand Down Expand Up @@ -94,3 +109,34 @@ def _refresh_container_names_cache(self) -> None:
self._container_id_to_name_cache.clear()
for container in self._containers_client.list_containers() if self._containers_client is not None else []:
self._container_id_to_name_cache[container.id] = container.name


class SparkContainerNamesClient(ContainerNamesClientBase):
"""
Container names are Spark application ids.
"""

def get_container_name(self, pid: int) -> str:
if not valid_perf_pid(pid):
return ""

try:
process = Process(pid)
args = process.cmdline()
except NoSuchProcess:
return ""

try:
it = iter(args)
for arg in it:
if arg == "--app-id":
name = f"spark: {next(it)}"
self._current_container_names.add(name)
return name
else:
return "" # not found
except StopIteration:
return ""
except Exception:
logger.exception("Unexpected error getting --app-id argument for pid", pid=pid, args=args)
return ""
5 changes: 3 additions & 2 deletions gprofiler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
ProfilerAPIClient,
)
from gprofiler.consts import CPU_PROFILING_MODE
from gprofiler.containers_client import ContainerNamesClient
from gprofiler.containers_client import SparkContainerNamesClient
from gprofiler.diagnostics import log_diagnostics, set_diagnostics
from gprofiler.exceptions import APIError, NoProfilersEnabledError
from gprofiler.gprofiler_types import ProcessToProfileData, UserArgs, integers_list, positive_integer
Expand Down Expand Up @@ -147,7 +147,8 @@ def __init__(
# 2. accessible only by us.
# the latter can be root only. the former can not. we should do this separation so we don't expose
# files unnecessarily.
container_names_client = ContainerNamesClient() if self._enrichment_options.container_names else None
# container_names_client = ContainerNamesClient() if self._enrichment_options.container_names else None
container_names_client = SparkContainerNamesClient() if self._enrichment_options.container_names else None
Comment on lines +150 to +151
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This needs to be made a CLI argument.

self._profiler_state = ProfilerState(
stop_event=Event(),
storage_dir=TEMPORARY_STORAGE_PATH,
Expand Down
8 changes: 4 additions & 4 deletions gprofiler/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from granulate_utils.metadata import Metadata

from gprofiler.containers_client import ContainerNamesClient
from gprofiler.containers_client import ContainerNamesClientBase
from gprofiler.gprofiler_types import ProcessToProfileData, ProfileData, ProfilingErrorStack, StackToSampleCount
from gprofiler.log import get_logger_adapter
from gprofiler.metadata.enrichment import EnrichmentOptions
Expand Down Expand Up @@ -39,7 +39,7 @@ def scale_sample_counts(stacks: StackToSampleCount, ratio: float) -> StackToSamp


def _make_profile_metadata(
container_names_client: Optional[ContainerNamesClient],
container_names_client: Optional[ContainerNamesClientBase],
add_container_names: bool,
metadata: Metadata,
metrics: Metrics,
Expand Down Expand Up @@ -175,7 +175,7 @@ def concatenate_from_external_file(

def concatenate_profiles(
process_profiles: ProcessToProfileData,
container_names_client: Optional[ContainerNamesClient],
container_names_client: Optional[ContainerNamesClientBase],
enrichment_options: EnrichmentOptions,
metadata: Metadata,
metrics: Metrics,
Expand Down Expand Up @@ -211,7 +211,7 @@ def concatenate_profiles(
def merge_profiles(
perf_pid_to_profiles: ProcessToProfileData,
process_profiles: ProcessToProfileData,
container_names_client: Optional[ContainerNamesClient],
container_names_client: Optional[ContainerNamesClientBase],
enrichment_options: EnrichmentOptions,
metadata: Metadata,
metrics: Metrics,
Expand Down
4 changes: 2 additions & 2 deletions gprofiler/profiler_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from psutil import Process

from gprofiler.containers_client import ContainerNamesClient
from gprofiler.containers_client import ContainerNamesClientBase
from gprofiler.utils import TemporaryDirectoryWithMode


Expand All @@ -19,7 +19,7 @@ class ProfilerState:
profile_spawned_processes: bool
insert_dso_name: bool
profiling_mode: str
container_names_client: Optional[ContainerNamesClient]
container_names_client: Optional[ContainerNamesClientBase]
processes_to_profile: Optional[List[Process]]

def __post_init__(self) -> None:
Expand Down
Loading