Skip to content

monitoring.counter_outgoing added into BaseHttpMainLoop #201

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

Merged
merged 1 commit into from
Jul 10, 2024
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: 7 additions & 1 deletion smart_kit/start_points/main_loop_async_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,26 @@ async def handle_message(self, message: SmartAppFromMessage) -> typing.Tuple[int
code = 204
log(f"OUTGOING DATA: {answer.masked_value} with code: {code}",
params={log_const.KEY_NAME: "outgoing_policy_message"}, user=user)
monitoring.counter_outgoing(self.app_name, answer.command.name, answer.command, user)
return code, "NO CONTENT", answer

answer_message = SmartAppToMessage(
answer, message, request=None, validators=self.to_msg_validators, masking_fields=self.masking_fields)
answer, message, request=None, validators=self.to_msg_validators, masking_fields=self.masking_fields
)
if answer_message.validate():
code = 200
log_answer = str(answer_message.masked_value).replace("%", "%%")
log(f"OUTGOING DATA: {log_answer} with code: {code}",
params={log_const.KEY_NAME: "outgoing_policy_message"}, user=user)
monitoring.counter_outgoing(self.app_name, answer.command.name, answer.command, user)
return code, "OK", answer_message
else:
code = 500
answer = SmartAppToMessage(
self.BAD_ANSWER_COMMAND, message=message, request=None, masking_fields=self.masking_fields)
log(f"OUTGOING DATA: {answer.masked_value} with code: {code}",
params={log_const.KEY_NAME: "outgoing_policy_message"}, user=user)
monitoring.counter_outgoing(self.app_name, answer.command.name, answer.command, user)
return code, "BAD ANSWER", answer

async def process_message(self, message: SmartAppFromMessage, *args, **kwargs):
Expand All @@ -153,6 +157,7 @@ async def process_message(self, message: SmartAppFromMessage, *args, **kwargs):

with StatsTimer() as load_timer:
user = await self.load_user(db_uid, message)
monitoring.sampling_load_time(self.app_name, load_timer.secs)
stats += "Loading time: {} msecs\n".format(load_timer.msecs)
with StatsTimer() as script_timer:
commands = await self.model.answer(message, user)
Expand All @@ -164,6 +169,7 @@ async def process_message(self, message: SmartAppFromMessage, *args, **kwargs):
stats += "Script time: {} msecs\n".format(script_timer.msecs)
with StatsTimer() as save_timer:
await self.save_user(db_uid, user, message)
monitoring.sampling_save_time(self.app_name, save_timer.secs)
stats += "Saving time: {} msecs\n".format(save_timer.msecs)
log(stats, params={log_const.KEY_NAME: "timings"})
await self.postprocessor.postprocess(user, message)
Expand Down
3 changes: 3 additions & 0 deletions smart_kit/start_points/main_loop_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from core.configs.global_constants import CALLBACK_ID_HEADER
from core.logging.logger_utils import log
from core.message.from_message import SmartAppFromMessage, basic_error_message
from core.monitoring.monitoring import monitoring
from core.utils.stats_timer import StatsTimer
from smart_kit.compatibility.commands import combine_commands
from smart_kit.message.smartapp_to_message import SmartAppToMessage
Expand Down Expand Up @@ -70,6 +71,7 @@ def process_message(self, message: SmartAppFromMessage, *args, **kwargs):
db_uid = message.db_uid
with StatsTimer() as load_timer:
user = self.loop.run_until_complete(self.load_user(db_uid, message))
monitoring.sampling_load_time(self.app_name, load_timer.secs)
stats += "Loading time: {} msecs\n".format(load_timer.msecs)
with StatsTimer() as script_timer:
commands = asyncio.get_event_loop().run_until_complete(self.model.answer(message, user))
Expand All @@ -81,6 +83,7 @@ def process_message(self, message: SmartAppFromMessage, *args, **kwargs):
stats += "Script time: {} msecs\n".format(script_timer.msecs)
with StatsTimer() as save_timer:
self.loop.run_until_complete(self.save_user(db_uid, user, message))
monitoring.sampling_save_time(self.app_name, save_timer.secs)
stats += "Saving time: {} msecs\n".format(save_timer.msecs)
log(stats, user=user, params={log_const.KEY_NAME: "timings"})
self.loop.run_until_complete(self.postprocessor.postprocess(user, message))
Expand Down
3 changes: 1 addition & 2 deletions smart_kit/start_points/main_loop_kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,6 @@ def _generate_answers(self, user, commands, message, **kwargs):
else:
answers.append(SmartAppToMessage(self.BAD_ANSWER_COMMAND, message=message, request=request))

monitoring.counter_outgoing(self.app_name, command.name, answer, user)

return answers

def _get_timeout_from_message(self, orig_message_raw: Dict, callback_id, headers):
Expand Down Expand Up @@ -603,6 +601,7 @@ async def _send_request(self, user: BaseUser, answer: SmartAppToMessage, mq_mess
request_params["mq_message"] = mq_message
request_params["payload"] = answer.value
request_params["masked_value"] = answer.masked_value
monitoring.counter_outgoing(self.app_name, answer.command.name, answer.command, user)
await request.run(answer.value.encode(), request_params)
self._log_request(user, request, answer, mq_message)

Expand Down
Loading