Remember to send webhook stats to Influx post-refactor

This commit is contained in:
Ske 2018-07-24 22:55:41 +02:00
parent 8936029dc8
commit d81dadefa4

View File

@ -1,12 +1,13 @@
import ciso8601 import ciso8601
import logging import logging
import re import re
import time
from typing import List, Optional from typing import List, Optional
import aiohttp import aiohttp
import discord import discord
from pluralkit import db from pluralkit import db, stats
from pluralkit.bot import channel_logger, utils from pluralkit.bot import channel_logger, utils
logger = logging.getLogger("pluralkit.bot.proxy") logger = logging.getLogger("pluralkit.bot.proxy")
@ -162,12 +163,16 @@ class Proxy:
if member.avatar_url: if member.avatar_url:
form_data.add_field("avatar_url", member.avatar_url) form_data.add_field("avatar_url", member.avatar_url)
time_before = time.perf_counter()
async with self.session.post( async with self.session.post(
"https://discordapp.com/api/v6/webhooks/{}/{}?wait=true".format(hook_id, hook_token), "https://discordapp.com/api/v6/webhooks/{}/{}?wait=true".format(hook_id, hook_token),
data=form_data) as resp: data=form_data) as resp:
if resp.status == 200: if resp.status == 200:
message = await resp.json() message = await resp.json()
# Report webhook stats to Influx
stats.report_webhook(time.perf_counter() - time_before, True)
await db.add_message(conn, message["id"], message["channel_id"], member.id, original_message.author.id, await db.add_message(conn, message["id"], message["channel_id"], member.id, original_message.author.id,
text or "") text or "")
@ -205,6 +210,9 @@ class Proxy:
message["timestamp"]), message["timestamp"]),
message_id=message["id"]) message_id=message["id"])
elif resp.status == 404 and not has_already_retried: elif resp.status == 404 and not has_already_retried:
# Report webhook stats to Influx
stats.report_webhook(time.perf_counter() - time_before, False)
# Webhook doesn't exist. Delete it from the DB, create, and add a new one # Webhook doesn't exist. Delete it from the DB, create, and add a new one
self.logger.warning("Webhook registered in DB doesn't exist, deleting hook from DB, re-adding, and trying again (channel={}, hook={})".format(original_message.channel.id, hook_id)) self.logger.warning("Webhook registered in DB doesn't exist, deleting hook from DB, re-adding, and trying again (channel={}, hook={})".format(original_message.channel.id, hook_id))
await db.delete_webhook(conn, original_message.channel.id) await db.delete_webhook(conn, original_message.channel.id)
@ -213,6 +221,9 @@ class Proxy:
# Then try again all over, making sure to not retry again and go in a loop should it continually fail # Then try again all over, making sure to not retry again and go in a loop should it continually fail
return await self.do_proxy_message(conn, member, original_message, text, attachment_url, has_already_retried=True) return await self.do_proxy_message(conn, member, original_message, text, attachment_url, has_already_retried=True)
else: else:
# Report webhook stats to Influx
stats.report_webhook(time.perf_counter() - time_before, False)
raise discord.HTTPException(resp, await resp.text()) raise discord.HTTPException(resp, await resp.text())
async def try_proxy_message(self, conn, message: discord.Message): async def try_proxy_message(self, conn, message: discord.Message):