Skip to content

Instantly share code, notes, and snippets.

@Slach
Last active January 20, 2017 06:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Slach/0cfdcc99711869d1764e to your computer and use it in GitHub Desktop.
Save Slach/0cfdcc99711869d1764e to your computer and use it in GitHub Desktop.
Flask + Pynba monitoring example
import pynba
import pynba.util
import flask
import logging
import datetime
# see https://gist.github.com/Slach/00c86b1341f738bc9dd5
from monitor import CoffeePynbaMonitor
logger = logging.getLogger('pynba')
# if your PINBA instance data empty uncomment
# logger.setLevel(logging.DEBUG)
app = flask.Flask('test_pynba')
app.config.update(service.config)
app.pynba = None
@app.before_first_request
def before_first_request():
# replace to CoffeePynbaMonitor if you need status and other request params
app.pynba = pynba.util.ScriptMonitor(
address=(app.config['pynba']['server'], app.config['pynba']['port']),
hostname=flask.request.host,
scriptname=flask.request.path,
)
@app.before_request
def before_request():
# replace to CoffeePynbaMonitor if you need status and other request params
app.pynba = pynba.util.ScriptMonitor(
address=(app.config['pynba']['server'], app.config['pynba']['port']),
hostname=flask.request.host,
scriptname=flask.request.path,
)
@app.after_request
def after_request(response):
app.pynba.collector.document_size = response.content_length
app.pynba.status = response.status_code
app.pynba.send()
app.pynba = None
return response
@app.teardown_request
def teardown_request(error):
if app.pynba is not None:
app.pynba.collector.document_size = 0
app.pynba.status = response.status_code
app.pynba.send()
app.pynba = None
@app.route('/test_action/', methods=['POST'])
def set_profile(game_service_id):
with app.pynba.timer(state='flask_timer_global'):
try:
# do something with database
data = {'var': 'All OK'}
status = 200
except:
data = {'var': 'Error'}
status = 500
with app.pynba.timer(state ='flask_generate_response'):
resp_string = app.render(data, 'template.html')
response = flask.Response(response=resp_string, status=status, mimetype='text/html')
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment