Skip to content

Commit

Permalink
Increase logs (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeandemeusy authored Feb 28, 2025
1 parent 56a989c commit 9d34c65
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 315 deletions.
105 changes: 45 additions & 60 deletions ct-app/core/api/hoprd_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,9 @@

from core.components.logs import configure_logging

from . import request_objects as request
from . import response_objects as response
from .http_method import HTTPMethod
from .request_objects import (
ApiRequestObject,
FundChannelBody,
GetChannelsBody,
GetPeersBody,
OpenChannelBody,
PopMessagesBody,
SendMessageBody,
)
from .response_objects import (
Addresses,
Balances,
Channels,
Configuration,
ConnectedPeer,
Infos,
Message,
OpenedChannel,
SendMessageAck,
TicketPrice,
)

MESSAGE_TAG = 0x1245

Expand All @@ -50,8 +31,12 @@ async def __call(
self,
method: HTTPMethod,
endpoint: str,
data: ApiRequestObject = None,
data: request.ApiRequestObject = None,
):
if endpoint != "messages":
logger.debug("Hitting API", {"host": self.host,
"method": method.value, "endpoint": endpoint,
"data": getattr(data, "as_dict", {})})
try:
headers = {"Content-Type": "application/json"}
async with aiohttp.ClientSession(headers=self.headers) as s:
Expand All @@ -67,11 +52,11 @@ async def __call(

return (res.status // 200) == 1, data
except OSError as err:
logger.exception("OSError while doing an API call",
logger.error("OSError while doing an API call",
{"error": str(err), "method": method.value, "endpoint": endpoint})

except Exception as err:
logger.exception("Exception while doing an API call",
logger.error("Exception while doing an API call",
{"error": str(err), "method": method.value, "endpoint": endpoint})

return (False, None)
Expand All @@ -80,7 +65,7 @@ async def __call_api(
self,
method: HTTPMethod,
endpoint: str,
data: ApiRequestObject = None,
data: request.ApiRequestObject = None,
timeout: int = 60,
) -> tuple[bool, Optional[object]]:
backoff = 0.5
Expand All @@ -105,27 +90,27 @@ async def __call_api(
else:
return result

async def balances(self) -> Optional[Balances]:
async def balances(self) -> Optional[response.Balances]:
"""
Returns the balance of the node.
:return: balances: Balances | undefined
"""
is_ok, response = await self.__call_api(HTTPMethod.GET, "account/balances")
return Balances(response) if is_ok else None
is_ok, resp = await self.__call_api(HTTPMethod.GET, "account/balances")
return response.Balances(resp) if is_ok else None

async def open_channel(
self, peer_address: str, amount: str
) -> Optional[OpenedChannel]:
) -> Optional[response.OpenedChannel]:
"""
Opens a channel with the given peer_address and amount.
:param: peer_address: str
:param: amount: str
:return: channel id: str | undefined
"""
data = OpenChannelBody(amount, peer_address)
data = request.OpenChannelBody(amount, peer_address)

is_ok, response = await self.__call_api(HTTPMethod.POST, "channels", data, timeout=90)
return OpenedChannel(response) if is_ok else None
is_ok, resp = await self.__call_api(HTTPMethod.POST, "channels", data, timeout=90)
return response.OpenedChannel(resp) if is_ok else None

async def fund_channel(self, channel_id: str, amount: float) -> bool:
"""
Expand All @@ -134,7 +119,7 @@ async def fund_channel(self, channel_id: str, amount: float) -> bool:
:param: amount: float
:return: bool
"""
data = FundChannelBody(amount)
data = request.FundChannelBody(amount)

is_ok, _ = await self.__call_api(
HTTPMethod.POST, f"channels/{channel_id}/fund", data, timeout=90
Expand All @@ -150,54 +135,54 @@ async def close_channel(self, channel_id: str) -> bool:
is_ok, _ = await self.__call_api(HTTPMethod.DELETE, f"channels/{channel_id}", timeout=90)
return is_ok

async def channels(self) -> Channels:
async def channels(self) -> response.Channels:
"""
Returns all channels.
:return: channels: list
"""
params = GetChannelsBody("true", "false")
params = request.GetChannelsBody("true", "false")

is_ok, response = await self.__call_api(
is_ok, resp = await self.__call_api(
HTTPMethod.GET, f"channels?{params.as_header_string}"
)
return Channels(response) if is_ok else None
return response.Channels(resp) if is_ok else None

async def peers(
self,
quality: float = 0.5,
) -> list[ConnectedPeer]:
) -> list[response.ConnectedPeer]:
"""
Returns a list of peers.
:param: param: list or str = "peerId"
:param: status: str = "connected"
:param: quality: int = 0..1
:return: peers: list
"""
params = GetPeersBody(quality)
params = request.GetPeersBody(quality)

is_ok, response = await self.__call_api(
is_ok, resp = await self.__call_api(
HTTPMethod.GET, f"node/peers?{params.as_header_string}"
)

if not is_ok:
return []

if "connected" not in response:
if "connected" not in resp:
return []

return [ConnectedPeer(peer) for peer in response["connected"]]
return [response.ConnectedPeer(peer) for peer in resp["connected"]]

async def get_address(self) -> Optional[Addresses]:
async def get_address(self) -> Optional[response.Addresses]:
"""
Returns the address of the node.
:return: address: str | undefined
"""
is_ok, response = await self.__call_api(HTTPMethod.GET, "account/addresses")
return Addresses(response) if is_ok else None
is_ok, resp = await self.__call_api(HTTPMethod.GET, "account/addresses")
return response.Addresses(resp) if is_ok else None

async def send_message(
self, destination: str, message: str, hops: list[str], tag: int = MESSAGE_TAG
) -> Optional[SendMessageAck]:
) -> Optional[response.SendMessageAck]:
"""
Sends a message to the given destination.
:param: destination: str
Expand All @@ -206,36 +191,36 @@ async def send_message(
:param: tag: int = 0x0320
:return: bool
"""
data = SendMessageBody(message, hops, destination, tag)
is_ok, response = await self.__call_api(HTTPMethod.POST, "messages", data=data)
return SendMessageAck(response) if is_ok else None
data = request.SendMessageBody(message, hops, destination, tag)
is_ok, resp = await self.__call_api(HTTPMethod.POST, "messages", data=data)
return response.SendMessageAck(resp) if is_ok else None

async def node_info(self) -> Optional[Infos]:
async def node_info(self) -> Optional[response.Infos]:
"""
Gets informations about the HOPRd node.
:return: Infos
"""
is_ok, response = await self.__call_api(HTTPMethod.GET, "node/info")
return Infos(response) if is_ok else None
is_ok, resp = await self.__call_api(HTTPMethod.GET, "node/info")
return response.Infos(resp) if is_ok else None

async def ticket_price(self) -> Optional[TicketPrice]:
async def ticket_price(self) -> Optional[response.TicketPrice]:
"""
Gets the ticket price set in the configuration file.
:return: TicketPrice
"""
is_ok, response = await self.__call_api(HTTPMethod.GET, "node/configuration")
return TicketPrice(Configuration(json.loads(response)).as_dict) if is_ok else None
is_ok, resp = await self.__call_api(HTTPMethod.GET, "node/configuration")
return response.TicketPrice(response.Configuration(json.loads(resp)).as_dict) if is_ok else None

async def messages_pop_all(self, tag: int = MESSAGE_TAG) -> list:
"""
Pop all messages from the inbox
:param: tag = 0x0320
:return: list
"""
is_ok, response = await self.__call_api(
HTTPMethod.POST, "messages/pop-all", data=PopMessagesBody(tag)
is_ok, resp = await self.__call_api(
HTTPMethod.POST, "messages/pop-all", data=request.PopMessagesBody(tag)
)
return [Message(item) for item in response.get("messages", [])] if is_ok else []
return [response.Message(item) for item in resp.get("messages", [])] if is_ok else []

async def healthyz(self, timeout: int = 20) -> bool:
"""
Expand All @@ -258,10 +243,10 @@ async def _check_url(url: str):
await asyncio.sleep(0.25)

try:
response = await asyncio.wait_for(_check_url(url), timeout=timeout)
resp = await asyncio.wait_for(_check_url(url), timeout=timeout)
except asyncio.TimeoutError:
return False
except Exception:
return False
else:
return response.status == target
return resp.status == target
5 changes: 4 additions & 1 deletion ct-app/core/api/request_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class GetChannelsBody(ApiRequestObject):
def __init__(self, full_topology: str, including_closed: str):
super().__init__(vars())


class SendMessageBody(ApiRequestObject):
keys = {
"body": "body",
Expand All @@ -73,14 +74,16 @@ class SendMessageBody(ApiRequestObject):
def __init__(self, body: str, path: list[str], destination: str, tag: int):
super().__init__(vars())


class PopMessagesBody(ApiRequestObject):
keys = {"tag": "tag"}

def __init__(self, tag: int):
super().__init__(vars())


class GetPeersBody(ApiRequestObject):
keys = {"quality": "quality"}

def __init__(self, quality: float):
super().__init__(vars())
super().__init__(vars())
5 changes: 3 additions & 2 deletions ct-app/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from prometheus_client import Gauge

from core.components.logs import configure_logging
from core.subgraph.providers import GraphQLProvider
from core.subgraph import GraphQLProvider

from .api import HoprdAPI
from .components import Address, AsyncLoop, LockedVar, Parameters, Peer, Utils
Expand Down Expand Up @@ -85,6 +85,7 @@ async def rotate_subgraphs(self):
"""
Checks the subgraph URLs and sets the subgraph mode in use (default, backup or none).
"""
logger.info("Rotating subgraphs")
for provider in self.providers.values():
await provider.test(self.params.subgraph.type)

Expand Down Expand Up @@ -160,7 +161,7 @@ async def registered_nodes(self):
STAKE.labels(node.safe.address, "additional_balance").set(node.safe.additional_balance)

self.registered_nodes_data = results
logger.debug("Fetched registered nodes in the safe registry",{"count": len(results)})
logger.debug("Fetched registered nodes in the safe registry", {"count": len(results)})
SUBGRAPH_SIZE.set(len(results))

@master(flagguard, formalin)
Expand Down
Loading

0 comments on commit 9d34c65

Please sign in to comment.