GithubHelp home page GithubHelp logo

dkruchinin / sanic-prometheus Goto Github PK

View Code? Open in Web Editor NEW
80.0 80.0 29.0 46 KB

Prometheus metrics for Sanic, an async python web server

License: MIT License

Makefile 2.77% Python 96.33% Shell 0.90%
monitoring prometheus python sanic

sanic-prometheus's People

Contributors

arvind2222 avatar aviv-ebates avatar axyjo avatar banteg avatar dependabot[bot] avatar dkruchinin avatar dkruchinin-skyscanner avatar dnutiu avatar joar avatar mmniaziqb avatar optroodt avatar siredvin avatar skar404 avatar wallies avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

sanic-prometheus's Issues

Custom metrics

Nice work on this. I like the direction that you are headed. I'll be honest, I have not looked too deeply into the code base yet, but have you put any thought into providing some sort of an API to providing custom metrics?

For example, it would be nice if I had some sort of a decorator, or something that would allow me to report back a custom metric on a particular endpoint. In particular, I am thinking about a way to report to prometheus the number of live sessions and websocket connections are open.

404 page causes KeyError: '__START_TIME__'

When you reach a 404, it looks like the before_request_handler is not called and thus triggers the following error in the after_request_handler:

[2020-06-10 15:13:42 +0200] [17655] [ERROR] Exception occurred in one of response middleware handlers
Traceback (most recent call last):
  File "/Users/optroodt/venvs/<snip>/lib/python3.7/site-packages/sanic/app.py", line 1017, in handle_request
    request, response, request_name=name
  File "/Users/optroodt/venvs/<snip>/lib/python3.7/site-packages/sanic/app.py", line 1315, in _run_response_middleware
    _response = await _response
  File "/Users/optroodt/venvs/<snip>/lib/python3.7/site-packages/sanic_prometheus/__init__.py", line 125, in before_response
    metrics.after_request_handler(request, response, get_endpoint)
  File "/Users/optroodt/venvs/<snip>/lib/python3.7/site-packages/sanic_prometheus/metrics.py", line 56, in after_request_handler
    lat = time.time() - request['__START_TIME__']
  File "/Users/optroodt/venvs/<snip>/lib/python3.7/site-packages/sanic/request.py", line 143, in __getitem__
    return self.ctx.__dict__[key]
KeyError: '__START_TIME__'

Running:

  • MacOS 10.14.6 (Yes, I should upgrade :D)
  • Python 3.7.7,
  • sanic 19.12.2
  • sanic-prometheus 0.2.0

Broken with new prometheus-client 0.5.0

prometheus-client released 0.5.0 a few days ago, and it breaks the api.

Should probably limit the version in setup.py to prometheus-client ~= 0.4.2 to install the latest-known-good version.

Conflict with other before request middlewares

My error logs are getting filled with a

line 62, in after_request_handler
    lat = time.time() - request['__START_TIME__']
KeyError: '__START_TIME__'

and I suspect it is some kind of conflict with sanic-limiter by bohea (https://github.com/bohea/sanic-limiter). When I disable my limiter sanic-prometheus handles fine.

I can fork and try to patch this but I thought I'd bring it to your attention first to see if you've had any problem.

Sample code to run with sanic-limiter and sanic-prometheus installed. Latest version of both from git (sanic-limiter is a bit out of date via pip). Run this and go to localhost:8000/home then try localhost:8000/. After going to an endpoint that will throw a 404 you'll see the prometheus error.

from sanic_prometheus import monitor
from sanic import Sanic, response, Blueprint
from sanic_limiter import Limiter, get_remote_address


app = Sanic()
test_bp = Blueprint('test')

limiter = Limiter(
    app,
    global_limits=['10 per second', '500 per day'],
    key_func=get_remote_address)


@test_bp.route('/home', methods=['GET'])
async def home(request):
    return response.json({'success': 'you are home'})


if __name__ == '__main__':
    monitor(app).expose_endpoint()
    app.blueprint(test_bp)
    app.run(host='127.0.0.1', port=8000, access_log=True)

It's safer to use time.monotonic()

It's better to use time.monotonic() which is guaranteed to never decrease. time.time() can go backwards due to, e.g., malfunctioning ntp service.

Can prometheus-client be updated?

Is there a reason for prometheus-client~=0.5.0 in setup.py? I've been running with 0.6.0 for a while now without problems. The current version is 0.7.1 however.

New release and 0.1.4 access problem

Can you please prepare new release to pypi?

Also, this is very strange, but version 0.1.4 cannot be loaded from pypi

Collecting sanic-prometheus==0.1.4
  Could not find a version that satisfies the requirement sanic-prometheus==0.1.4 (from versions: 0.0.1, 0.1.0, 0.1.2, 0.1.3)
No matching distribution found for sanic-prometheus==0.1.4

Uncorrect monitoring task finish

Checked on sanic==0.6.0.
After app finished, monitoring coroutine was forced finished with message.

Task was destroyed but it is pending!
task: <Task pending coro=<make_periodic_memcollect_task.<locals>.collector() running at /home/siredvin/.pyenv/pastereport/lib/python3.6/site-packages/sanic_prometheus/metrics.py:51> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fcb0b71c6d8>()]>>

I guess, in monitoring method should be added something like:

@app.listener('after_server_stop')
def after_stop(app, loop):
    metric_collection_task.cancel()

@dkruchinin Will you support this repository? I can provide pull request for this issue.

Exception on OPTIONS

Hello,

I get a KeyError: '__START_TIME__' when I send an OPTIONS request, maybe the library should ignore the OPTIONS req. I'm also using it together with Sanic-Cors.

'Request' object does not support item assignment

Sanic no longer allows for item assignment in the request object.

I am using sanic==20.6.3 and sanic-prometheus==0.2.0

ERROR:sanic.root:Exception occurred while handling uri: 'http://localhost:8080/status/ok'
Traceback (most recent call last):
  File "/home/test/.local/lib/python3.8/site-packages/sanic/app.py", line 906, in handle_request
    response = await self._run_request_middleware(
  File "/home/test/.local/lib/python3.8/site-packages/sanic/app.py", line 1265, in _run_request_middleware
    response = await response
  File "/home/test/.local/lib/python3.8/site-packages/sanic_prometheus/__init__.py", line 120, in before_request
    metrics.before_request_handler(request)
  File "/home/test/.local/lib/python3.8/site-packages/sanic_prometheus/metrics.py", line 52, in before_request_handler
    request['__START_TIME__'] = time.time()
TypeError: 'Request' object does not support item assignment
Traceback (most recent call last):
  File "/home/test/.local/lib/python3.8/site-packages/sanic/app.py", line 906, in handle_request
    response = await self._run_request_middleware(
  File "/home/test/.local/lib/python3.8/site-packages/sanic/app.py", line 1265, in _run_request_middleware
    response = await response
  File "/home/test/.local/lib/python3.8/site-packages/sanic_prometheus/__init__.py", line 120, in before_request
    metrics.before_request_handler(request)
  File "/home/test/.local/lib/python3.8/site-packages/sanic_prometheus/metrics.py", line 52, in before_request_handler
    request['__START_TIME__'] = time.time()
TypeError: 'Request' object does not support item assignment

Nested paths

If I have an endpoint at /v2/ping, it appears that all the metrics come through as just /v2 and there's no way to know if the request was for /v2/ping or maybe /v2/pong

AttributeError: 'types.SimpleNamespace' object has no attribute '__START_TIME__'

I'm facing this issue with latest sanic-prometheus. It happens when I accessed a non-existed page..

[ERROR] Exception occurred in one of response middleware handlers
Traceback (most recent call last):
  File "/home/.../lib/python3.6/site-packages/sanic/app.py", line 977, in handle_request
    request, response, request_name=name
  File "/home/.../lib/python3.6/site-packages/sanic/app.py", line 1281, in _run_response_middleware
    _response = await _response
  File "/home/.../lib/python3.6/site-packages/sanic_prometheus/__init__.py", line 125, in before_response
    metrics.after_request_handler(request, response, get_endpoint)
  File "/home/.../lib/python3.6/site-packages/sanic_prometheus/metrics.py", line 56, in after_request_handler
    lat = time.time() - request.ctx.__START_TIME__
AttributeError: 'types.SimpleNamespace' object has no attribute '__START_TIME__'

Running:

Ubuntu 20.04
Python 3.6.9
sanic 20.6.3
sanic-prometheus 0.2.1

Multiprocess monitoring

If running Sanic in multi-worker mode (ie app.run(workers=...)), it appears that we run into an issue documented in the prometheus_client README titled "Multiprocess Mode".

If I repeatedly refresh the /metrics end point in my dev environment after hitting some endpoints a few times, the output will "rotate" between the correct metrics and blank or underreported metrics. I'm guessing the underreported/blank metrics are from worker processes that haven't served requests yet.

I'm guessing multiprocess_mode should be added to the various sanic-prometheus metrics.

monitor(app).start_server() runs server which must not be using in production - having memory leaks issues

I found that this way of prometheus metrics server starting, which code is

class _ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
    """Thread per request HTTP server."""
def start_http_server(port, addr='', registry=REGISTRY):
    """Starts an HTTP server for prometheus metrics as a daemon thread"""
    CustomMetricsHandler = MetricsHandler.factory(registry)
    httpd = _ThreadingSimpleServer((addr, port), CustomMetricsHandler)
    t = threading.Thread(target=httpd.serve_forever)
    t.daemon = True
    t.start()

is starting the server, which has memory leaks. I found it by profiling app with tracemalloc library. This server is infinitely opening new threads. I think this behavior must be specified in the docs.

Error on shutdown when adding the monitor

2017-12-27 13:35:38,132 asyncio ERROR Task was destroyed but it is pending!
task: <Task pending coro=<make_periodic_memcollect_task..collector() running at /usr/local/lib/python3.5/dist-packages/sanic_prometheus/metrics.py:42> wait_for=>

the error disappear when monitor(app).expose_endpoint() is not used

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.