mirror of
				https://github.com/suyiiyii/nonebot-bison.git
				synced 2025-11-04 21:44:52 +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 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 = Counter(
 | 
			
		||||
@ -14,6 +16,24 @@ cookie_choose_counter = Counter(
 | 
			
		||||
    "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"])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -6,7 +6,7 @@ from nonebot_plugin_apscheduler import scheduler
 | 
			
		||||
from nonebot_plugin_saa.utils.exceptions import NoBotFound
 | 
			
		||||
 | 
			
		||||
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 ..send import send_msgs
 | 
			
		||||
@ -98,19 +98,22 @@ class Scheduler:
 | 
			
		||||
        success_flag = False
 | 
			
		||||
        platform_obj = platform_manager[schedulable.platform_name](context)
 | 
			
		||||
        try:
 | 
			
		||||
            if schedulable.use_batch:
 | 
			
		||||
                batch_targets = self.batch_api_target_cache[schedulable.platform_name][schedulable.target]
 | 
			
		||||
                sub_units = []
 | 
			
		||||
                for batch_target in batch_targets:
 | 
			
		||||
                    userinfo = await config.get_platform_target_subscribers(schedulable.platform_name, batch_target)
 | 
			
		||||
                    sub_units.append(SubUnit(batch_target, userinfo))
 | 
			
		||||
                to_send = await platform_obj.do_batch_fetch_new_post(sub_units)
 | 
			
		||||
            else:
 | 
			
		||||
                send_userinfo_list = await config.get_platform_target_subscribers(
 | 
			
		||||
                    schedulable.platform_name, schedulable.target
 | 
			
		||||
                )
 | 
			
		||||
                to_send = await platform_obj.do_fetch_new_post(SubUnit(schedulable.target, send_userinfo_list))
 | 
			
		||||
            success_flag = True
 | 
			
		||||
            with request_histogram.labels(
 | 
			
		||||
                platform_name=schedulable.platform_name, site_name=platform_obj.site.name
 | 
			
		||||
            ).time():
 | 
			
		||||
                if schedulable.use_batch:
 | 
			
		||||
                    batch_targets = self.batch_api_target_cache[schedulable.platform_name][schedulable.target]
 | 
			
		||||
                    sub_units = []
 | 
			
		||||
                    for batch_target in batch_targets:
 | 
			
		||||
                        userinfo = await config.get_platform_target_subscribers(schedulable.platform_name, batch_target)
 | 
			
		||||
                        sub_units.append(SubUnit(batch_target, userinfo))
 | 
			
		||||
                    to_send = await platform_obj.do_batch_fetch_new_post(sub_units)
 | 
			
		||||
                else:
 | 
			
		||||
                    send_userinfo_list = await config.get_platform_target_subscribers(
 | 
			
		||||
                        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:
 | 
			
		||||
            logger.debug(f"skip request: {err}")
 | 
			
		||||
        except Exception as err:
 | 
			
		||||
@ -131,16 +134,17 @@ class Scheduler:
 | 
			
		||||
        sent_counter.labels(
 | 
			
		||||
            platform_name=schedulable.platform_name, site_name=platform_obj.site.name, target=schedulable.target
 | 
			
		||||
        ).inc()
 | 
			
		||||
        for user, send_list in to_send:
 | 
			
		||||
            for send_post in send_list:
 | 
			
		||||
                logger.info(f"send to {user}: {send_post}")
 | 
			
		||||
                try:
 | 
			
		||||
                    await send_msgs(
 | 
			
		||||
                        user,
 | 
			
		||||
                        await send_post.generate_messages(),
 | 
			
		||||
                    )
 | 
			
		||||
                except NoBotFound:
 | 
			
		||||
                    logger.warning("no bot connected")
 | 
			
		||||
        with render_histogram.labels(platform_name=schedulable.platform_name, site_name=platform_obj.site.name).time():
 | 
			
		||||
            for user, send_list in to_send:
 | 
			
		||||
                for send_post in send_list:
 | 
			
		||||
                    logger.info(f"send to {user}: {send_post}")
 | 
			
		||||
                    try:
 | 
			
		||||
                        await send_msgs(
 | 
			
		||||
                            user,
 | 
			
		||||
                            await send_post.generate_messages(),
 | 
			
		||||
                        )
 | 
			
		||||
                    except NoBotFound:
 | 
			
		||||
                        logger.warning("no bot connected")
 | 
			
		||||
 | 
			
		||||
    def insert_new_schedulable(self, platform_name: str, target: Target):
 | 
			
		||||
        self.pre_weight_val += 1000
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user