Periskop requires collecting and aggregating exceptions on the client side, as well as exposing them via an HTTP endpoint using a well defined format.
This library provides low level collection and rendering capabilities
pip install periskop-client
importjsonfromhttp.serverimportHTTPServerfromperiskop_client.collectorimportExceptionCollectorfromperiskop_client.exporterimportExceptionExporterfromperiskop_client.handlerimportexception_http_handlerfromperiskop_client.modelsimportHTTPContextdeffaulty_json():
returnjson.loads('{"id":')
if__name__=="__main__":
collector=ExceptionCollector()
try:
faulty_json()
exceptExceptionasexception:
# Report without contextcollector.report(exception)
# Report with HTTP context without request bodycollector.report_with_context(
exception,
HTTPContext("GET", "http://example.com", {"Cache-Control": "no-cache"}),
)
# Report with HTTP context with request bodycollector.report_with_context(
exception,
HTTPContext("GET", "http://example.com", {"Cache-Control": "no-cache"}, "request_body"),
)
# Expose collected exceptions in localhost:8081/-/exceptionsserver_address= ("", 8081)
handler=exception_http_handler(
path="/-/exceptions", exporter=ExceptionExporter(collector)
)
http_server=HTTPServer(server_address, handler)
http_server.serve_forever()You can also use pushgateway in case you want to push your metrics
instead of using pull method. Use only in case you really need it (e.g a batch job) as it would degrade the performance
of your application. In the following example, we assume that we deployed an instance of periskop-pushgateway
on http://localhost:6767:
exporter =ExceptionExporter(collector)
exporter.push_to_gateway("http://localhost:6767")For running tests pytest is needed. A recommended way to run all check is installing tox and then just type tox. This will run pytest tests, black formatter and flake8 and mypy static code analyzers.
Alternatively you can run pip install -r requirements-tests.txt and then run pytest.