Skip to content

Commit c2b0c51

Browse files
Oleg Chaplashkinochaplashkin
authored andcommitted
Add prometheus metrics of application and VM
A new `GET /metrics` endpoint has been added with a basic metrics. Two custom metrics have also been added: - event types (X-GitHub-Event); - request latency from a third-party service (Github). Closes #27
1 parent 803ff39 commit c2b0c51

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

docbot/app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
from elasticapm.contrib.flask import ElasticAPM
2+
from prometheus_flask_exporter import PrometheusMetrics
23
from flask import Flask, request
34

45
from .handlers import webhook_handler, list_events_handler
56

7+
68
app = Flask(__name__)
79

810
app.config['ELASTIC_APM'] = {
911
'SERVICE_NAME': 'docbot',
1012
}
1113
apm = ElasticAPM(app)
1214

15+
metrics = PrometheusMetrics(app)
1316

1417
@app.route("/", methods=['GET'])
1518
def index() -> str:
1619
return list_events_handler()
1720

1821

1922
@app.route("/", methods=['POST'])
23+
@metrics.summary(
24+
name='events',
25+
description='Summary of event from the GitHub service',
26+
labels={'event': lambda: request.headers.get('X-GitHub-Event', None)}
27+
)
2028
def webhook() -> str:
2129
data: dict = request.json
2230
event: str = request.headers.get('X-GitHub-Event')

docbot/github.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import requests
22

3+
from .metrics import Metrics
34
from .utils import create_event
45

56

@@ -29,6 +30,7 @@ def create_issue(self, title, description, src_url, author, doc_repo_url):
2930
status_code = self._send_request(url, body)
3031
print('Created issue: {}'.format(status_code))
3132

33+
@Metrics.github_latency.time()
3234
def _send_request(self, url, body, method='post'):
3335
headers = {
3436
'Authorization': 'token {}'.format(self.token),

docbot/metrics.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from dataclasses import dataclass
2+
from prometheus_client import Histogram
3+
4+
5+
@dataclass
6+
class Metrics:
7+
github_latency = Histogram(
8+
name='github',
9+
documentation='Time spent processing a request from the GitHub service'
10+
)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ idna==3.3
99
itsdangerous==2.1.2
1010
Jinja2==3.1.2
1111
MarkupSafe==2.1.1
12+
prometheus-flask-exporter==0.22.3
1213
requests==2.28.0
1314
urllib3==1.26.9
1415
Werkzeug==2.1.2

0 commit comments

Comments
 (0)