Code Monkey home page Code Monkey logo

periskop-python's Introduction

periskop-python

pypi PyPI pyversions Build Status

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

Install

pip install periskop-client

Usage example

import json
from http.server import HTTPServer

from periskop_client.collector import ExceptionCollector
from periskop_client.exporter import ExceptionExporter
from periskop_client.handler import exception_http_handler
from periskop_client.models import HTTPContext


def faulty_json():
    return json.loads('{"id":')


if __name__ == "__main__":
    collector = ExceptionCollector()
    try:
        faulty_json()
    except Exception as exception:
        # Report without context
        collector.report(exception)
        # Report with HTTP context without request body
        collector.report_with_context(
            exception,
            HTTPContext("GET", "http://example.com", {"Cache-Control": "no-cache"}),
        )
        # Report with HTTP context with request body
        collector.report_with_context(
            exception,
            HTTPContext("GET", "http://example.com", {"Cache-Control": "no-cache"}, "request_body"),
        )

    # Expose collected exceptions in localhost:8081/-/exceptions
    server_address = ("", 8081)
    handler = exception_http_handler(
        path="/-/exceptions", exporter=ExceptionExporter(collector)
    )
    http_server = HTTPServer(server_address, handler)
    http_server.serve_forever()

Using push gateway

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")

Run tests

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.

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.