mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-02 09:26:12 +08:00
✨ 添加 request_histogram 和 render_histogram
This commit is contained in:
parent
4c29cf10e4
commit
96573ec86e
@ -1,6 +1,8 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from starlette.responses import Response
|
from starlette.responses import Response
|
||||||
from prometheus_client import CONTENT_TYPE_LATEST, Counter, generate_latest
|
from prometheus_client import CONTENT_TYPE_LATEST, Gauge, Counter, Histogram, generate_latest
|
||||||
|
|
||||||
# Request counter
|
# Request counter
|
||||||
request_counter = Counter(
|
request_counter = Counter(
|
||||||
@ -14,6 +16,24 @@ cookie_choose_counter = Counter(
|
|||||||
"bison_cookie_choose_counter", "The number of cookie choose", ["site_name", "target", "cookie_id"]
|
"bison_cookie_choose_counter", "The number of cookie choose", ["site_name", "target", "cookie_id"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
request_histogram = Histogram(
|
||||||
|
"bison_request_histogram",
|
||||||
|
"The time of platform used to request the source",
|
||||||
|
["site_name", "platform_name"],
|
||||||
|
buckets=[0.1, 0.5, 1, 2, 5, 10, 30, 60],
|
||||||
|
)
|
||||||
|
|
||||||
|
render_histogram = Histogram(
|
||||||
|
"bison_render_histogram",
|
||||||
|
"The time of theme used to render",
|
||||||
|
["site_name", "platform_name"],
|
||||||
|
buckets=[0.1, 0.5, 1, 2, 5, 10, 30, 60],
|
||||||
|
)
|
||||||
|
|
||||||
|
start_time = Gauge("bison_start_time", "The start time of the program")
|
||||||
|
start_time.set(time.time())
|
||||||
|
|
||||||
|
|
||||||
metrics_router = APIRouter(prefix="/api/metrics", tags=["metrics"])
|
metrics_router = APIRouter(prefix="/api/metrics", tags=["metrics"])
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ from nonebot_plugin_apscheduler import scheduler
|
|||||||
from nonebot_plugin_saa.utils.exceptions import NoBotFound
|
from nonebot_plugin_saa.utils.exceptions import NoBotFound
|
||||||
|
|
||||||
from nonebot_bison.utils import ClientManager
|
from nonebot_bison.utils import ClientManager
|
||||||
from nonebot_bison.metrics import sent_counter, request_counter
|
from nonebot_bison.metrics import sent_counter, request_counter, render_histogram, request_histogram
|
||||||
|
|
||||||
from ..config import config
|
from ..config import config
|
||||||
from ..send import send_msgs
|
from ..send import send_msgs
|
||||||
@ -98,19 +98,22 @@ class Scheduler:
|
|||||||
success_flag = False
|
success_flag = False
|
||||||
platform_obj = platform_manager[schedulable.platform_name](context)
|
platform_obj = platform_manager[schedulable.platform_name](context)
|
||||||
try:
|
try:
|
||||||
if schedulable.use_batch:
|
with request_histogram.labels(
|
||||||
batch_targets = self.batch_api_target_cache[schedulable.platform_name][schedulable.target]
|
platform_name=schedulable.platform_name, site_name=platform_obj.site.name
|
||||||
sub_units = []
|
).time():
|
||||||
for batch_target in batch_targets:
|
if schedulable.use_batch:
|
||||||
userinfo = await config.get_platform_target_subscribers(schedulable.platform_name, batch_target)
|
batch_targets = self.batch_api_target_cache[schedulable.platform_name][schedulable.target]
|
||||||
sub_units.append(SubUnit(batch_target, userinfo))
|
sub_units = []
|
||||||
to_send = await platform_obj.do_batch_fetch_new_post(sub_units)
|
for batch_target in batch_targets:
|
||||||
else:
|
userinfo = await config.get_platform_target_subscribers(schedulable.platform_name, batch_target)
|
||||||
send_userinfo_list = await config.get_platform_target_subscribers(
|
sub_units.append(SubUnit(batch_target, userinfo))
|
||||||
schedulable.platform_name, schedulable.target
|
to_send = await platform_obj.do_batch_fetch_new_post(sub_units)
|
||||||
)
|
else:
|
||||||
to_send = await platform_obj.do_fetch_new_post(SubUnit(schedulable.target, send_userinfo_list))
|
send_userinfo_list = await config.get_platform_target_subscribers(
|
||||||
success_flag = True
|
schedulable.platform_name, schedulable.target
|
||||||
|
)
|
||||||
|
to_send = await platform_obj.do_fetch_new_post(SubUnit(schedulable.target, send_userinfo_list))
|
||||||
|
success_flag = True
|
||||||
except SkipRequestException as err:
|
except SkipRequestException as err:
|
||||||
logger.debug(f"skip request: {err}")
|
logger.debug(f"skip request: {err}")
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
@ -131,16 +134,17 @@ class Scheduler:
|
|||||||
sent_counter.labels(
|
sent_counter.labels(
|
||||||
platform_name=schedulable.platform_name, site_name=platform_obj.site.name, target=schedulable.target
|
platform_name=schedulable.platform_name, site_name=platform_obj.site.name, target=schedulable.target
|
||||||
).inc()
|
).inc()
|
||||||
for user, send_list in to_send:
|
with render_histogram.labels(platform_name=schedulable.platform_name, site_name=platform_obj.site.name).time():
|
||||||
for send_post in send_list:
|
for user, send_list in to_send:
|
||||||
logger.info(f"send to {user}: {send_post}")
|
for send_post in send_list:
|
||||||
try:
|
logger.info(f"send to {user}: {send_post}")
|
||||||
await send_msgs(
|
try:
|
||||||
user,
|
await send_msgs(
|
||||||
await send_post.generate_messages(),
|
user,
|
||||||
)
|
await send_post.generate_messages(),
|
||||||
except NoBotFound:
|
)
|
||||||
logger.warning("no bot connected")
|
except NoBotFound:
|
||||||
|
logger.warning("no bot connected")
|
||||||
|
|
||||||
def insert_new_schedulable(self, platform_name: str, target: Target):
|
def insert_new_schedulable(self, platform_name: str, target: Target):
|
||||||
self.pre_weight_val += 1000
|
self.pre_weight_val += 1000
|
||||||
|
Loading…
x
Reference in New Issue
Block a user