DavidLiu 4 months ago
parent
commit
5c1fdc9dcb
2 changed files with 28 additions and 10 deletions
  1. 9 9
      src/core/callcenter/registry.py
  2. 19 1
      src/core/callcenter/views.py

+ 9 - 9
src/core/callcenter/registry.py

@@ -4,21 +4,21 @@
 # import prometheus_client
 # import prometheus_client
 from . import app
 from . import app
 from typing import Iterable
 from typing import Iterable
-# from prometheus_client import Counter, Gauge, Histogram, Summary, start_http_server, generate_latest
+from prometheus_client import Counter, Gauge, Histogram, Summary
 # from prometheus_client.core import CollectorRegistry
 # from prometheus_client.core import CollectorRegistry
 from prometheus_flask_exporter import PrometheusMetrics
 from prometheus_flask_exporter import PrometheusMetrics
 
 
 metrics = PrometheusMetrics(app)
 metrics = PrometheusMetrics(app)
 
 
 
 
-def counter(name: str, documentation: str, labels=None):
-    return metrics.counter(name, documentation, labels)
+def counter(name, description, labels=None, initial_value_when_only_static_labels=True, **kwargs):
+    return metrics.counter(name, description, labels, initial_value_when_only_static_labels, **kwargs)
 
 
-def gauge(name: str, documentation: str, labels=None):
-    return metrics.gauge(name, documentation, labels)
+def gauge(name, description, labels=None, initial_value_when_only_static_labels=True, **kwargs):
+    return metrics.gauge(name, description, labels, initial_value_when_only_static_labels, **kwargs)
 
 
-def histogram(name: str, documentation: str, labels=None):
-    return metrics.histogram(name, documentation, labels)
+def histogram(name, description, labels=None, initial_value_when_only_static_labels=True, **kwargs):
+    return metrics.histogram(name, description, labels, initial_value_when_only_static_labels, **kwargs)
 
 
-def summary(name: str, documentation: str, labels=None):
-    return metrics.summary(name, documentation, labels)
+def summary(name, description, labels=None, initial_value_when_only_static_labels=True, **kwargs):
+    return metrics.summary(name, description, labels, initial_value_when_only_static_labels, **kwargs)

+ 19 - 1
src/core/callcenter/views.py

@@ -1,16 +1,18 @@
 #!/usr/bin/env python3
 #!/usr/bin/env python3
 # encoding:utf-8
 # encoding:utf-8
+import time
 from . import app
 from . import app
 from src.core.callcenter.agent import AgentService, AgentOperService
 from src.core.callcenter.agent import AgentService, AgentOperService
 from src.core.callcenter.constant import success_response
 from src.core.callcenter.constant import success_response
 from src.core.callcenter.enumeration import CallType
 from src.core.callcenter.enumeration import CallType
 from src.core.callcenter.esl.client import InboundClient, OutboundClient
 from src.core.callcenter.esl.client import InboundClient, OutboundClient
-from flask import request, render_template_string, Response
+from flask import request, render_template_string, g
 
 
 from src.core.callcenter.call import CallService
 from src.core.callcenter.call import CallService
 from src.core.callcenter.api import AgentCallRequest, AgentActionRequest, HangupCallRequest
 from src.core.callcenter.api import AgentCallRequest, AgentActionRequest, HangupCallRequest
 from src.core.voip.bot import BotAgent
 from src.core.voip.bot import BotAgent
 from .acd import AcdService
 from .acd import AcdService
+from src.core.callcenter.registry import Histogram
 
 
 agent = BotAgent(app)
 agent = BotAgent(app)
 inbound_client = InboundClient(agent,app)
 inbound_client = InboundClient(agent,app)
@@ -22,6 +24,22 @@ acd_service = AcdService(inbound_client, app)
 agent.acd_service = acd_service
 agent.acd_service = acd_service
 CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8')
 CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8')
 
 
+REQUEST_LATENCY = Histogram(
+    'request_latency_seconds',
+    'Request latency in seconds',
+    ['method', 'endpoint']
+)
+@app.before_request
+def start_timer():
+    """在请求开始时记录时间"""
+    g.start_time = time.time()
+
+@app.after_request
+def record_latency(response):
+    """请求结束时计算耗时,并将其上报到 Prometheus"""
+    latency = time.time() - g.start_time
+    REQUEST_LATENCY.labels(request.method, request.path).observe(latency)
+    return response
 
 
 
 
 @app.route('/')
 @app.route('/')