Coder Social home page Coder Social logo

Comments (5)

sebgoa avatar sebgoa commented on August 15, 2024

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.

drawks avatar drawks commented on August 15, 2024

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.

drawks avatar drawks commented on August 15, 2024

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.

sebgoa avatar sebgoa commented on August 15, 2024

thanks a lot for your answer, I will try it asap.

from client_python.

sebgoa avatar sebgoa commented on August 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.