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

Log input parameters of specific failing methods #40

Merged
merged 2 commits into from
Nov 27, 2023
Merged
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
8 changes: 6 additions & 2 deletions src/zinolib/controllers/zino1.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@
"""

from datetime import datetime, timezone
from typing import Iterable, List, TypedDict, Optional, Set
from typing import Dict, Iterable, List, TypedDict, Optional, Set
import logging

from .base import EventManager
from ..compat import StrEnum
from ..event_types import EventType, Event, HistoryEntry, LogEntry, AdmState
from ..event_types import PortStateEvent
from ..ritz import ZinoError, ritz, notifier
from ..utils import log_exception_with_params


__all__ = [
Expand Down Expand Up @@ -118,11 +119,11 @@

def __init__(self, manager, autoremove=False):
if not manager.is_authenticated:
msg = "Cannot initiate update handler, not authenticated"
raise self.UpdateError(msg)

Check warning on line 123 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L122-L123

Added lines #L122 - L123 were not covered by tests
self.manager = manager
if not self.manager.session.push:
self.manager.connect_push_channel()

Check warning on line 126 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L126

Added line #L126 was not covered by tests
self.events = manager.events
self.autoremove = autoremove

Expand Down Expand Up @@ -200,13 +201,13 @@
@classmethod
def connect_session(cls, session):
session.request.connect()
return cls.connect_push_channel(session)

Check warning on line 204 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L204

Added line #L204 was not covered by tests

@staticmethod
def connect_push_channel(session):
if session.request.connected and session.request.authenticated:
session.push = notifier(session.request)
session.push.connect() # ntie

Check warning on line 210 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L208-L210

Added lines #L208 - L210 were not covered by tests
return session

@staticmethod
Expand All @@ -220,8 +221,8 @@

@staticmethod
def close_session(session):
if hasattr(session.push, '_sock'):
session.push._sock.close()

Check warning on line 225 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L224-L225

Added lines #L224 - L225 were not covered by tests
session.request.close()
return None

Expand All @@ -248,7 +249,8 @@
return request.get_raw_attributes(event_id)

@classmethod
def attrlist_to_attrdict(cls, attrlist: Iterable[str]):
@log_exception_with_params(LOG)
def attrlist_to_attrdict(cls, attrlist: Iterable[str]) -> Dict[str, str]:
"""Translate a wire protocol dump of a single event

The dump is a list of lines of the format:
Expand Down Expand Up @@ -290,6 +292,7 @@
return request.get_raw_history(event_id).data

@classmethod
@log_exception_with_params(LOG)
def parse_response(cls, history_data: Iterable[str]) -> list[HistoryDict]:
"""
Input:
Expand Down Expand Up @@ -357,6 +360,7 @@
return request.get_raw_log(event_id).data

@staticmethod
@log_exception_with_params(LOG)
def parse_response(log_data: Iterable[str]) -> list[LogDict]:
"""
Input:
Expand Down Expand Up @@ -404,7 +408,7 @@
"Replace the original exception with our own"
try:
return function(*args)
except ZinoError as e:

Check warning on line 411 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L411

Added line #L411 was not covered by tests
raise self.ManagerException(e)

def _verify_session(self, quiet=False):
Expand All @@ -424,12 +428,12 @@
self.session = self._session_adapter.connect_session(self.session)

def connect_push_channel(self):
self.session = self._session_adapter.connect_push_channel(self.session)

Check warning on line 431 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L431

Added line #L431 was not covered by tests

def authenticate(self, username=None, password=None):
try:
self.session = self._session_adapter.authenticate(self.session, username, password)
except (ZinoError, ValueError) as e:

Check warning on line 436 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L435-L436

Added lines #L435 - L436 were not covered by tests
raise self.ManagerException(e)

def disconnect(self):
Expand All @@ -444,7 +448,7 @@
c.clear_flapping()
"""
if event.type == Event.Type.PortState:
return self.session.request.clear_flapping(event.router, event.if_index)

Check warning on line 451 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L451

Added line #L451 was not covered by tests
return None

def get_events(self):
Expand Down
25 changes: 25 additions & 0 deletions src/zinolib/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import functools
import hashlib
from typing import Any


__all__ = [
Expand Down Expand Up @@ -56,3 +58,26 @@
raw_token = "%s %s" % (challenge, password)
token = hashlib.sha1(raw_token.encode("UTF-8")).hexdigest()
return token


def log_exception_with_params(logger, reraise=True, return_value=None):
"""Log the params and exception if the wrapped function fails

If ``reraise`` is False, return ``return_value`` instead of reraising the
exception.
"""
def inner(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
try:
result = function(*args, **kwargs)
return result
except Exception as e:
params = f'args={args} kwargs={kwargs}'
funcname = function.__name__
logger.exception(f'"{funcname}" failed with: {params}\n{e}')
if reraise:
raise
return return_value

Check warning on line 81 in src/zinolib/utils.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/utils.py#L75-L81

Added lines #L75 - L81 were not covered by tests
return wrapper
return inner
Loading