Skip to content

Commit

Permalink
Improve Monitoring Log
Browse files Browse the repository at this point in the history
  • Loading branch information
AliRn76 committed Nov 1, 2024
1 parent 3b69526 commit d0e9a1f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
Binary file added docs/docs/images/monitoring_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 14 additions & 4 deletions docs/docs/monitoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ Then you can watch them live with: `panther monitor`

Make sure it is `False` on production for better performance

It's using `monitoring` logger

#### Log Example:

#### Reference
- [panther.monitoring.Monitoring](https://github.com/AliRn76/panther/blob/master/panther/monitoring.py)

#### Log Example

```python
date time | method | path | ip:port | response_time [ms, s] | status
date time | method | path | ip:port | response_time(seconds) | status

2023-12-11 18:23:42 | GET | /login | 127.0.0.1:55710 | 0.001117052001063712 | 200
```


#### Monitoring Example

2023-12-11 18:23:42 | GET | /login | 127.0.0.1:55710 | 2.8021 ms | 200
```
![monitoring_example.png](images/monitoring_example.png)
23 changes: 20 additions & 3 deletions panther/cli/monitor_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
with contextlib.suppress(ImportError):
from watchfiles import watch

loggerr = logging.getLogger('panther')
logger = logging.getLogger('panther')


class Monitoring:
Expand All @@ -30,7 +30,7 @@ def __init__(self):
def monitor(self) -> None:
if error := self.initialize():
# Don't continue if initialize() has error
loggerr.error(error)
logger.error(error)
return

with (
Expand All @@ -51,7 +51,10 @@ def monitor(self) -> None:

for _ in watching:
for line in f.readlines():
self.rows.append(line.split('|'))
# line = date_time | method | path | ip:port | response_time(seconds) | status
columns = line.split('|')
columns[4] = self._clean_response_time(float(columns[4]))
self.rows.append(columns)
live.update(self.generate_table())

def initialize(self) -> str:
Expand Down Expand Up @@ -100,5 +103,19 @@ def update_rows(self, *args, **kwargs):
lines = (os.get_terminal_size()[1] - 6) // 2
self.rows = deque(self.rows, maxlen=lines)

@classmethod
def _clean_response_time(cls, response_time: int) -> str:
time_unit = ' s'

if response_time < 0.01:
response_time = response_time * 1_000
time_unit = 'ms'

elif response_time >= 10:
response_time = response_time / 60
time_unit = ' m'

return f'{round(response_time, 4)} {time_unit}'


monitor = Monitoring().monitor
16 changes: 3 additions & 13 deletions panther/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class Monitoring:
"""
Create Log Message Like Below:
date time | method | path | ip:port | response_time [ms, s] | status
date_time | method | path | ip:port | response_time(seconds) | status
"""
def __init__(self, is_ws: bool = False):
self.is_ws = is_ws
Expand All @@ -30,15 +30,5 @@ async def before(self, request: BaseRequest):

async def after(self, status: int | Literal['Accepted', 'Rejected', 'Closed'], /):
if config.MONITORING:
response_time = perf_counter() - self.start_time
time_unit = ' s'

if response_time < 0.01:
response_time = response_time * 1_000
time_unit = 'ms'

elif response_time >= 10:
response_time = response_time / 60
time_unit = ' m'

logger.info(f'{self.log} | {round(response_time, 4)} {time_unit} | {status}')
response_time = perf_counter() - self.start_time # Seconds
logger.info(f'{self.log} | {response_time} | {status}')

0 comments on commit d0e9a1f

Please sign in to comment.