From 3c8cd53094d44c900b7a80bdf18d2841aac5f72a Mon Sep 17 00:00:00 2001 From: wunder957 Date: Tue, 28 Nov 2023 15:14:58 +0800 Subject: [PATCH] Add prefix for inspector --- duetector/collectors/models.py | 5 ++++- duetector/injectors/inspector.py | 33 +++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/duetector/collectors/models.py b/duetector/collectors/models.py index c40a296..9592c3a 100644 --- a/duetector/collectors/models.py +++ b/duetector/collectors/models.py @@ -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 @@ -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__": diff --git a/duetector/injectors/inspector.py b/duetector/injectors/inspector.py index 0304d80..29703af 100644 --- a/duetector/injectors/inspector.py +++ b/duetector/injectors/inspector.py @@ -1,5 +1,6 @@ from __future__ import annotations +import itertools from typing import Any from duetector.utils import Singleton @@ -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 @@ -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() @@ -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()