10000 Added uwsgi sharedarea support. · prometheus/client_python@a65767d · GitHub
[go: up one dir, main page]

Skip to content

Commit a65767d

Browse files
committed
Added uwsgi sharedarea support.
1 parent 67fe14a commit a65767d

File tree

13 files changed

+574
-1
lines changed

13 files changed

+574
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ dist
66
.coverage.*
77
.coverage
88
.tox
9+
examples/loadtest/logs/*
10+
examples/storage/*

MANIFEST.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include setup.py README.rst MANIFEST.in LICENSE *.txt
2+
recursive-include client_python/ *
3+
graft tests
4+
global-exclude *~
5+
recursive-exclude dist/* examples/*

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,22 @@ for family in text_string_to_metric_families("my_gauge 1.0\n"):
458458
for sample in family.samples:
459459
print("Name: {0} Labels: {1} Value: {2}".format(*sample))
460460
```
461+
462+
463+
## UWSGI sharedarea MODE
464+
465+
This mode enable uwsgi sharedarea memory to store all metrics and sync between processes.
466+
467+
To enable mode, setup `PROMETHEUS_UWSGI_SHAREDAREA` environment variable with sharedarea id value.
468+
469+
Example uwsgi config:
470+
471+
``` ini
472+
473+
[uwsgi]
474+
475+
#...
476+
477+
sharedarea=10
478+
479+
```

examples/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM python:2.7.8
2+
3+
ENV PYTHONUNBUFFERED=1
4+
5+
WORKDIR /usr/src/app
6+
7+
RUN apt-get update && apt-get install -y \
8+
libxml2-dev libxslt-dev python-dev \
9+
libyaml-dev \
10+
graphviz
11+
12+
COPY requirements.txt requirements.txt
13+
14+
RUN pip install -U plop gprof2dot ipython
15+
RUN pip install -r requirements.txt
16+
17+
RUN mkdir /app-entrypoint.d
18+
19+
#COPY app-entrypoint.sh /
20+
21+
#ENTRYPOINT ["/app-entrypoint.sh"]
22+
23+
ENTRYPOINT ["/bin/bash", "-c"]
24+
25+
#CMD ["echo 'hello'"]

examples/Makefile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
current_dir = $(shell pwd)
2+
3+
docker-clean: docker-clean-containers
4+
5+
docker-clean-containers:
6+
docker ps -q -f status=exited | xargs docker rm
7+
8+
docker-build:
9+
@echo "Run development services via docker"
10+
sudo docker-compose -f compose-uwsgi.yml build --force-rm #--no-cache
11+
12+
docker-stop:
13+
@echo "Stop docker services"
14+
sudo docker-compose -f compose-uwsgi.yml -f stop
15+
16+
docker-start:
17+
@echo "Run development services via docker"
18+
docker-compose -f compose-uwsgi.yml up --force-recreate
19+
20+
tank:
21+
@echo "Tank load"
22+
docker run --rm --net examples_default --link dev_flask_app:dev_flask_app -v $(current_dir)/loadtest/:/var/loadtest/ direvius/yandex-tank

examples/app-entrypoint.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
set -e
3+
4+
echo "Application entrypoint";
5+
6+
for f in /app-entrypoint.d/*; do
7+
echo "File: $f";
8+
case "$f" in
9+
*.sh) echo "$0: running $f"; . "$f" ;;
10+
*) echo "$0: ignoring $f" ;;
11+
esac
12+
echo
13+
done
14+
15+
exec "$@";

examples/compose-uwsgi.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: "2.0"
2+
3+
services:
4+
dev_flask_app:
5+
image: dev_flask_app:latest
6+
build:
7+
dockerfile: Dockerfile
8+
context: .
9+
environment:
10+
- PROMETEUS_STATUS=1
11+
- prometheus_multiproc_dir=/usr/src/app/storage/prometheus/
12+
#- PROMETHEUS_UWSGI_SHAREDAREA=1
13+
- PYTHONUNBUFFERED=1
14+
15+
command:
16+
- "rm -rf /usr/src/app/storage/prometheus/* && find /usr/src/app/ -name '*.pyc' -exec rm -f {} + && cd /prometheus_client && python /prometheus_client/setup.py install && cd /uwsgi_lib && python /uwsgi_lib/uwsgiconfig.py --build && cd /usr/src/app/ && /uwsgi_lib/uwsgi --ini=/usr/src/app/uwsgi.ini"
17+
volumes:
18+
- ./uwsgi.ini:/usr/src/app/uwsgi.ini
19+
- ./:/usr/src/app/
20+
- ../:/prometheus_client
21+
- /projects/libs/uwsgi:/uwsgi_lib
22+
23+
ports:
24+
- 8051:8051

examples/flask_app.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import os
4+
5+
import prometheus_client
6+
from flask import request, Response, Flask
7+
from prometheus_client import Counter, Gauge, Summary, Histogram
8+
from prometheus_client.core import CollectorRegistry
9+
from string import ascii_letters
10+
from random import choice
11+
12+
REGISTRY = CollectorRegistry(auto_describe=False)
13+
14+
15+
requests_total = Counter("app:requests_total", "Total count of requests", ["method", "url_rule", "env_role"], registry=REGISTRY)
16+
inprogress_total = Gauge("app:inprogress_total", "Total count of requests in progress", ["method", "url_rule", "env_role"], registry=REGISTRY)
17+
request_duration_summary_sec = Summary("app:request_duration_summary_sec", "Request duration in seconds", ["method", "url_rule", "env_role", "rnd"], registry=REGISTRY)
18+
request_duration_historam_sec = Histogram("app:request_duration_histogram_sec", "Request duration in seconds", ["method", "url_rule", "env_role", "rnd"], registry=REGISTRY)
19+
20+
random_counter = Counter("random_counter", "random counter", ["rnd"], registry=REGISTRY)
21+
22+
APP_ENV_ROLE = os.environ.get('APP_ROLE', 'unknown')
23+
24+
app = Flask(__name__)
25+
app.debug = True
26+
27+
28+
@app.route("/metrics")
29+
def metrics():
30+
text = "# Process in {0}\n".format(os.getpid())
31+
32+
return Response(text + prometheus_client.generate_latest(REGISTRY), mimetype="text/plain")
33+
34+
35+
@app.route('/<path:path>')
36+
@app.route('/')
37+
def index(path='/'):
38+
requests_total.labels(method=request.method, url_rule=path, env_role=APP_ENV_ROLE).inc()
39+
40+
#requests_total.labels(method=request.method, url_rule=path, env_role=APP_ENV_ROLE).inc()
41+
42+
rnd = ''.join([choice(ascii_letters) for x in xrange(10)])
43+
44+
text = "# Process in {0} rnd={1}\n".format(os.getpid(), rnd)
45+
46+
#random_counter.labels(rnd=rnd).inc()
47+
48+
#with request_duration_summary_sec.labels(method=request.method, url_rule=path, env_role=APP_ENV_ROLE, rnd=rnd).time():#, \
49+
#request_duration_historam_sec.labels(method=request.method, url_rule=path, env_role=APP_ENV_ROLE, rnd=rnd).time():
50+
51+
return Response(text, mimetype="text/plain")
52+
53+
application = app
54+
print("Debug app init")

examples/loadtest/load.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[phantom]
2+
address=dev_flask_app:8051 ;
3+
rps_schedule=const(100, 1m) ;
4+
connection_test=0 ;
5+
6+
ammofile=/var/loadtest/uris.txt
7+
ammo_type=uri
8+
writelog=1

examples/loadtest/uris.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[Connection: close]
2+
[Host: dev_flask_app]
3+
[Cookie: None]
4+
5+
/api/v
6+
/business/
7+
/auto/
8+
/starlife/
9+
/politics/
10+
/sport/
11+
/kids/
12+
/lifestyle/
13+
/incidents/
14+
/9may/
15+
/health/
16+
/newyear/
17+
/kinomusic/
18+
/games/
19+
/scitech/
20+
/

0 commit comments

Comments
 (0)
0