Skip to content

Commit

Permalink
instrument mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaqq committed Jan 31, 2025
1 parent 1ac5fb9 commit 397a3eb
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions ops/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ class _GenericLazyMapping(Mapping[str, _LazyValueType], ABC):
_lazy_data: Optional[Dict[str, _LazyValueType]] = None

@abstractmethod
# FIXME instrument here, or individual access?
def _load(self) -> Dict[str, _LazyValueType]:
raise NotImplementedError()

Expand All @@ -898,20 +899,27 @@ def _data(self) -> Dict[str, _LazyValueType]:
def _invalidate(self):
self._lazy_data = None

# FIXME isntrument all these?
def __contains__(self, key: str) -> bool:
return key in self._data
with tracer.start_as_current_span(f"x in ops.{self.__class__.__name__}"): # type: ignore
return key in self._data

def __len__(self) -> int:
return len(self._data)
with tracer.start_as_current_span(f"len(ops.{self.__class__.__name__})"): # type: ignore
return len(self._data)

def __iter__(self):
return iter(self._data)
with tracer.start_as_current_span(f"for x in ops.{self.__class__.__name__}"): # type: ignore
return iter(self._data)

def __getitem__(self, key: str) -> _LazyValueType:
return self._data[key]
# FIXME code path also covers .get()
with tracer.start_as_current_span(f"ops.{self.__class__.__name__}[x]"): # type: ignore
return self._data[key]

def __repr__(self) -> str:
return repr(self._data)
with tracer.start_as_current_span(f"repr(ops.{self.__class__.__name__})"): # type: ignore
return repr(self._data)


class LazyMapping(_GenericLazyMapping[str]):
Expand Down Expand Up @@ -952,6 +960,8 @@ def __len__(self):
def __iter__(self) -> Iterable[str]:
return iter(self._data)

# FIXME: called by bass class for .get() too
@tracer.start_as_current_span("ops.RelationMapping[x]") # type: ignore
def __getitem__(self, relation_name: str) -> List['Relation']:
is_peer = relation_name in self._peers
relation_list: Optional[List[Relation]] = self._data[relation_name]
Expand Down Expand Up @@ -1020,6 +1030,7 @@ def __init__(self, backend: '_ModelBackend'):
self._backend = backend
self._data: _BindingDictType = {}

# FIXME check
def get(self, binding_key: Union[str, 'Relation']) -> 'Binding':
"""Get a specific Binding for an endpoint/relation.
Expand Down Expand Up @@ -1068,6 +1079,7 @@ def _network_get(self, name: str, relation_id: Optional[int] = None) -> 'Network
return Network(self._backend.network_get(name, relation_id))

@property
# FIXME check
def network(self) -> 'Network':
"""The network information for this binding."""
if self._network is None:
Expand Down Expand Up @@ -1782,6 +1794,7 @@ class RelationData(Mapping[Union['Unit', 'Application'], 'RelationDataContent'])
:attr:`Relation.data`
"""

# FIXME check
def __init__(self, relation: Relation, our_unit: Unit, backend: '_ModelBackend'):
self.relation = weakref.proxy(relation)
self._data: Dict[Union[Unit, Application], RelationDataContent] = {
Expand Down Expand Up @@ -1833,6 +1846,7 @@ def _hook_is_running(self) -> bool:
# unrestricted, allowing test code to read/write databags at will.
return bool(self._backend._hook_is_running)

# FIXME instrument this or LazyMapping?
def _load(self) -> '_RelationDataContent_Raw':
"""Load the data from the current entity / relation."""
try:
Expand Down Expand Up @@ -2187,6 +2201,7 @@ class Pod:
def __init__(self, backend: '_ModelBackend'):
self._backend = backend

@tracer.start_as_current_span("ops.Pod.set_spec") # type: ignore
def set_spec(self, spec: 'K8sSpec', k8s_resources: Optional['K8sSpec'] = None):
"""Set the specification for pods that Juju should start in kubernetes.
Expand Down Expand Up @@ -2219,6 +2234,8 @@ def __len__(self):
def __iter__(self):
return iter(self._storage_map)

# FIXME: called by bass class for .get() too
@tracer.start_as_current_span("ops.StorageMapping[]") # type: ignore
def __getitem__(self, storage_name: str) -> List['Storage']:
if storage_name not in self._storage_map:
meant = ', or '.join(repr(k) for k in self._storage_map)
Expand All @@ -2231,6 +2248,8 @@ def __getitem__(self, storage_name: str) -> List['Storage']:
storage_list.append(storage)
return storage_list

# FIXME doesn't seem used by any charm?
@tracer.start_as_current_span("ops.StorageMapping.request") # type: ignore
def request(self, storage_name: str, count: int = 1):
"""Requests new storage instances of a given name.
Expand Down

0 comments on commit 397a3eb

Please sign in to comment.