Comments (5)
I managed to do it with tornado, but is there a best practice without tornado:
from tornado.wsgi import WSGIContainer
from tornado.ioloop import IOLoop
from tornado.web import FallbackHandler, RequestHandler, Application
from prometheus_client import start_http_server, Summary
from flask import Flask
from prometheus_client import Counter
app = Flask(__name__)
REQUEST_TIME = Summary('request_processing_seconds','Time Spent processing request')
@app.route('/')
@REQUEST_TIME.time()
def handler():
return "hello world"
class MainHandler(RequestHandler):
def get(self):
self.write("This message comes from Tornado")
tr = WSGIContainer(app)
application = Application([
(r"/tornado", MainHandler),
(r".*", FallbackHandler, dict(fallback=tr)),
])
if __name__ == "__main__":
application.listen(5000)
start_http_server(9999)
IOLoop.instance().start()
from client_python.
Here is a working example of instrumenting a few routes in a basic bottle application and making the metrics route available an attached to the application. This also uses the MultiProcessCollector
since it would be fairly atypical (at all but the smallest scale) to run a wsgi application as a single process. This requires that the prometheus_multiproc_dir
variable be set. This can typically be accomplished in your wsgi runner, for example in uwsgi you could add the following to your config
env = prometheus_multiproc_dir=/tmp/helloworld.stats
from bottle import route, run, template, Bottle
from prometheus_client import multiprocess
from prometheus_client import generate_latest, CollectorRegistry, Gauge, Counter
application = Bottle()
registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry)
IN_PROGRESS = Gauge("inprogress_requests", "help", multiprocess_mode='livesum')
REQUESTS = Counter('http_requests_total', 'Description of counter', ['method', 'endpoint'])
@IN_PROGRESS.track_inprogress()
@application.route('/hello/<name>')
def index(name):
REQUESTS.labels(method='GET', endpoint="hello").inc()
return template('<b>Hello {{name}}</b>!', name=name)
@IN_PROGRESS.track_inprogress()
@application.route('/metrics')
def metrics():
data = generate_latest(registry)
return data
from client_python.
For sake of completeness, here is the equivalent example with none of the multiprocessing stuff:
from bottle import route, run, template, Bottle
from prometheus_client import multiprocess
from prometheus_client import generate_latest, REGISTRY, Gauge, Counter
application = Bottle()
IN_PROGRESS = Gauge("inprogress_requests", "help")
REQUESTS = Counter('http_requests_total', 'Description of counter', ['method', 'endpoint'])
@IN_PROGRESS.track_inprogress()
@application.route('/hello/<name>')
def index(name):
REQUESTS.labels(method='GET', endpoint="hello").inc()
return template('<b>Hello {{name}}</b>!', name=name)
@IN_PROGRESS.track_inprogress()
@application.route('/metrics')
def metrics():
return generate_latest(REGISTRY)
application.run()
from client_python.
thanks a lot for your answer, I will try it asap.
from client_python.
Ok this is working fine. I just need to figure out how to export the metrics into my prometheus server which is running on a different node...
from client_python.
Related Issues (20)
- https://github.com/prometheus/client_python.git HOT 1
- Use of `MmapedDict` results in fd leaks (resource leaks) HOT 1
- installed package name doesnt match
- Using None as a label value breaks exposure of all metrics
- UTF-8: Implement support in Python client library
- Empty /metrics body using multiprocessing on FastAPI HOT 1
- ASGI mounted to FastAPI mounts metrics to `/metrics/` instead of `/metrics` HOT 6
- Gauge 'track_inprogress' function not working HOT 2
- Network scanners report issues because all HTTP methods are supported HOT 1
- tests/test_asgi.py failures with python 3.12 HOT 3
- real reference for API HOT 3
- Histogram without label creates histogram_*.db even without data HOT 3
- _count Histogram metrics in multirprocess mode HOT 1
- Exclude PID labels in multiprocess mode HOT 2
- Metric for label set has incorrect name exposed in multiprocess mode
- Timestamp comparison is wrong
- Why is the restriction on name in class MetricWrapperBase to comply with the METRIC-NAME-RE rule?
- Failed to push using push_to_gateway and basic_auth_handler. HOT 2
- Metrics declared at toplevel of program interfere with autoreload
- Multiprocess registry metrics cause "Error on ingesting samples with different value but same timestamp" after Prometheus upgrade >2.52.0 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from client_python.