Skip to content

Commit

Permalink
ensure the fields are included in the log record as json
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazabbas committed May 9, 2024
1 parent 13c309f commit f17b665
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
12 changes: 6 additions & 6 deletions pyth_observer/check/price_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def state(self) -> PriceFeedState:
def run(self) -> bool:
...

def error_message(self) -> str:
def error_message(self) -> dict:
...


Expand Down Expand Up @@ -80,7 +80,7 @@ def run(self) -> bool:
# Fail
return False

def error_message(self) -> str:
def error_message(self) -> dict:
distance = self.__state.latest_block_slot - self.__state.latest_trading_slot
return {
"msg": f"{self.__state.symbol} is offline (either non-trading/stale). Last update {distance} slots ago.",
Expand Down Expand Up @@ -124,7 +124,7 @@ def run(self) -> bool:
# Fail
return False

def error_message(self) -> str:
def error_message(self) -> dict:
return {
"msg": f"{self.__state.symbol} is too far from Coingecko's price.",
"type": "PriceFeedCheck",
Expand Down Expand Up @@ -154,7 +154,7 @@ def run(self) -> bool:
# Fail
return False

def error_message(self) -> str:
def error_message(self) -> dict:
return {
"msg": f"{self.__state.symbol} confidence interval is too low.",
"type": "PriceFeedCheck",
Expand Down Expand Up @@ -204,7 +204,7 @@ def run(self) -> bool:
# Fail
return False

def error_message(self) -> str:
def error_message(self) -> dict:
if self.__state.crosschain_price:
publish_time = arrow.get(self.__state.crosschain_price["publish_time"])
else:
Expand Down Expand Up @@ -263,7 +263,7 @@ def run(self) -> bool:
# Fail
return False

def error_message(self) -> str:
def error_message(self) -> dict:
# It can never happen because of the check logic but linter could not understand it.
price = (
self.__state.crosschain_price["price"]
Expand Down
10 changes: 5 additions & 5 deletions pyth_observer/check/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def state(self) -> PublisherState:
def run(self) -> bool:
...

def error_message(self) -> str:
def error_message(self) -> dict:
...


Expand Down Expand Up @@ -78,7 +78,7 @@ def run(self) -> bool:
# Fail
return False

def error_message(self) -> str:
def error_message(self) -> dict:
diff = self.__state.price - self.__state.price_aggregate
intervals_away = abs(diff / self.__state.confidence_interval_aggregate)
return {
Expand Down Expand Up @@ -116,7 +116,7 @@ def run(self) -> bool:
# Fail
return False

def error_message(self) -> str:
def error_message(self) -> dict:
return {
"msg": f"{self.__state.publisher_name} confidence interval is too tight.",
"type": "PublisherCheck",
Expand Down Expand Up @@ -150,7 +150,7 @@ def run(self) -> bool:
# Fail
return False

def error_message(self) -> str:
def error_message(self) -> dict:
distance = self.__state.latest_block_slot - self.__state.slot
return {
"msg": f"{self.__state.publisher_name} hasn't published recently for {distance} slots.",
Expand Down Expand Up @@ -198,7 +198,7 @@ def run(self) -> bool:
# Fail
return False

def error_message(self) -> str:
def error_message(self) -> dict:
deviation = (self.ci_adjusted_price_diff() / self.__state.price_aggregate) * 100
return {
"msg": f"{self.__state.publisher_name} price is too far from aggregate price.",
Expand Down
12 changes: 4 additions & 8 deletions pyth_observer/event.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from typing import Dict, Literal, Protocol, TypedDict, cast

import aiohttp
Expand All @@ -16,7 +17,6 @@

load_dotenv()


class Context(TypedDict):
network: str
publishers: Dict[str, Publisher]
Expand Down Expand Up @@ -84,9 +84,6 @@ async def send(self):
)


LogEventLevel = Literal["DEBUG", "INFO", "WARNING", "ERROR"]


class LogEvent(Event):
def __init__(self, check: Check, context: Context):
self.check = check
Expand All @@ -95,10 +92,9 @@ def __init__(self, check: Check, context: Context):
async def send(self):
# Publisher checks expect the key -> name mapping of publishers when
# generating the error title/message.
text = self.check.error_message()

level = cast(LogEventLevel, os.environ.get("LOG_EVENT_LEVEL", "INFO"))
logger.log(level, text)
event = self.check.error_message()
with logger.contextualize(**event):
logger.info(event["msg"])


class TelegramEvent(Event):
Expand Down

0 comments on commit f17b665

Please sign in to comment.