Skip to content

Commit

Permalink
Add prefix for inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
Wh1isper committed Nov 28, 2023
1 parent 0df7adc commit 3c8cd53
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
5 changes: 4 additions & 1 deletion duetector/collectors/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pydantic

from duetector.injectors.inspector import Inspector
from duetector.log import logger
from duetector.utils import get_boot_time_duration_ns

Expand Down Expand Up @@ -125,10 +126,12 @@ def set_span(self, collector, span):
v = getattr(self, k)
if v is not None:
k, v = self.serialize_field(k, v)
k = k.replace(Inspector.sep, ".")
span.set_attribute(k, v)
for k, v in self.extended.items():
k = k.replace(Inspector.sep, ".")
span.set_attribute(k, v)
span.set_attribute("collector_id", collector.id)
span.set_attribute("collector.id", collector.id)


if __name__ == "__main__":
Expand Down
33 changes: 30 additions & 3 deletions duetector/injectors/inspector.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import itertools
from typing import Any

from duetector.utils import Singleton
Expand All @@ -10,23 +11,41 @@ def __init__(self) -> None:
pass


def with_prefix(sep: str, prefix, key: str | list[str]) -> str:
if isinstance(key, str):
return sep.join([prefix, key.lower()])
else:
return sep.join(itertools.chain([prefix], [k.lower() for k in key]))


class Inspector:
sep = "__"
prefix = "inspector"
name = None

@property
def id(self) -> str:
return f"duetector_inspector_{self.__class__.__name__}".lower()
return with_prefix(self.sep, self.prefix, self.name or self.__class__.__name__)

def with_id(self, key: str | list[str]) -> str:
return with_prefix(self.sep, self.id, key)

def is_inspected(self, model: dict[str, Any]):
return self.id in model and model[self.id]

def mark_inspected(self, model: dict[str, Any]) -> None:
def _mark_inspected(self, model: dict[str, Any]) -> None:
model[self.id] = True

def _ensure_with_id(self, model: dict[str, Any]) -> dict[str, Any]:
return {self.with_id(k) if not k.startswith(self.id) else k: v for k, v in model.items()}

def inspect(self, model: dict[str, Any]) -> dict[str, Any]:
if self.is_inspected(model):
return {}

inspect_info = self._inspect(model)
self.mark_inspected(inspect_info)
inspect_info = self._ensure_with_id(inspect_info)
self._mark_inspected(inspect_info)

return inspect_info

Expand All @@ -35,6 +54,10 @@ def _inspect(self, model: dict[str, Any]) -> dict[str, Any]:


class NamespaceInspector(Inspector):
@property
def name(self) -> str:
return self.sep.join(["proc", "namespace"])

def __init__(self) -> None:
self.proc_watcher = ProcWatcher()

Expand All @@ -43,6 +66,10 @@ def _inspect(self, model: dict[str, Any]) -> dict[str, Any]:


class CgroupInspector(Inspector):
@property
def name(self) -> str:
return self.sep.join(["proc", "cgroup"])

def __init__(self) -> None:
self.proc_watcher = ProcWatcher()

Expand Down

0 comments on commit 3c8cd53

Please sign in to comment.