From b7702bc3e8dd21b2babc77df17e029903b1b8975 Mon Sep 17 00:00:00 2001 From: Harsha Narayana Date: Mon, 10 Dec 2018 20:35:02 +0530 Subject: [PATCH] add monitoring examples and documents --- docs/sanic/examples.rst | 20 ++++++++++++ examples/logdna_example.py | 61 +++++++++++++++++++++++++++++++++++++ examples/raygun_example.py | 37 ++++++++++++++++++++++ examples/rollbar_example.py | 30 ++++++++++++++++++ examples/sentry_example.py | 35 +++++++++++++++++++++ 5 files changed, 183 insertions(+) create mode 100644 examples/logdna_example.py create mode 100644 examples/raygun_example.py create mode 100644 examples/rollbar_example.py create mode 100644 examples/sentry_example.py diff --git a/docs/sanic/examples.rst b/docs/sanic/examples.rst index 68c2fccd..d6e99d1f 100644 --- a/docs/sanic/examples.rst +++ b/docs/sanic/examples.rst @@ -107,6 +107,26 @@ Monitoring and Error Handling .. literalinclude:: ../../examples/exception_monitoring.py +Monitoring using external Service Providers +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +* `LogDNA `_ + +.. literalinclude:: ../../examples/logdna_example.py + +* `RayGun `_ + +.. literalinclude:: ../../examples/raygun_example.py + +* `Rollbar `_ + +.. literalinclude:: ../../examples/rollbar_example.py + +* `Sentry `_ + +.. literalinclude:: ../../examples/sentry_example.py + + Security ~~~~~~~~ diff --git a/examples/logdna_example.py b/examples/logdna_example.py new file mode 100644 index 00000000..da38f404 --- /dev/null +++ b/examples/logdna_example.py @@ -0,0 +1,61 @@ +import logging +import socket +from os import getenv +from platform import node +from uuid import getnode as get_mac + +from logdna import LogDNAHandler + +from sanic import Sanic +from sanic.response import json +from sanic.request import Request + +log = logging.getLogger('logdna') +log.setLevel(logging.INFO) + + +def get_my_ip_address(remote_server="google.com"): + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s.connect((remote_server, 80)) + return s.getsockname()[0] + + +def get_mac_address(): + h = iter(hex(get_mac())[2:].zfill(12)) + return ":".join(i + next(h) for i in h) + + +logdna_options = { + "app": __name__, + "index_meta": True, + "hostname": node(), + "ip": get_my_ip_address(), + "mac": get_mac_address() +} + +logdna_handler = LogDNAHandler(getenv("LOGDNA_API_KEY"), options=logdna_options) + +logdna = logging.getLogger(__name__) +logdna.setLevel(logging.INFO) +logdna.addHandler(logdna_handler) + +app = Sanic(__name__) + + +@app.middleware +def log_request(request: Request): + logdna.info("I was Here with a new Request to URL: {}".format(request.url)) + + +@app.route("/") +def default(request): + return json({ + "response": "I was here" + }) + + +if __name__ == "__main__": + app.run( + host="0.0.0.0", + port=getenv("PORT", 8080) + ) diff --git a/examples/raygun_example.py b/examples/raygun_example.py new file mode 100644 index 00000000..fcb4a5eb --- /dev/null +++ b/examples/raygun_example.py @@ -0,0 +1,37 @@ +from os import getenv + +from raygun4py.raygunprovider import RaygunSender + +from sanic import Sanic +from sanic.exceptions import SanicException +from sanic.handlers import ErrorHandler + + +class RaygunExceptionReporter(ErrorHandler): + + def __init__(self, raygun_api_key=None): + super().__init__() + if raygun_api_key is None: + raygun_api_key = getenv("RAYGUN_API_KEY") + + self.sender = RaygunSender(raygun_api_key) + + def default(self, request, exception): + self.sender.send_exception(exception=exception) + return super().default(request, exception) + + +raygun_error_reporter = RaygunExceptionReporter() +app = Sanic(__name__, error_handler=raygun_error_reporter) + + +@app.route("/raise") +async def test(request): + raise SanicException('You Broke It!') + + +if __name__ == '__main__': + app.run( + host="0.0.0.0", + port=getenv("PORT", 8080) + ) diff --git a/examples/rollbar_example.py b/examples/rollbar_example.py new file mode 100644 index 00000000..cf37244f --- /dev/null +++ b/examples/rollbar_example.py @@ -0,0 +1,30 @@ +import rollbar + +from sanic.handlers import ErrorHandler +from sanic import Sanic +from sanic.exceptions import SanicException +from os import getenv + +rollbar.init(getenv("ROLLBAR_API_KEY")) + + +class RollbarExceptionHandler(ErrorHandler): + + def default(self, request, exception): + rollbar.report_message(str(exception)) + return super().default(request, exception) + + +app = Sanic(__name__, error_handler=RollbarExceptionHandler()) + + +@app.route("/raise") +def create_error(request): + raise SanicException("I was here and I don't like where I am") + + +if __name__ == "__main__": + app.run( + host="0.0.0.0", + port=getenv("PORT", 8080) + ) diff --git a/examples/sentry_example.py b/examples/sentry_example.py new file mode 100644 index 00000000..1ea75a0f --- /dev/null +++ b/examples/sentry_example.py @@ -0,0 +1,35 @@ +from os import getenv + +from sentry_sdk import init as sentry_init +from sentry_sdk.integrations.sanic import SanicIntegration + +from sanic import Sanic +from sanic.response import json + +sentry_init( + dsn=getenv("SENTRY_DSN"), + integrations=[SanicIntegration()], +) + +app = Sanic(__name__) + + +# noinspection PyUnusedLocal +@app.route("/working") +async def working_path(request): + return json({ + "response": "Working API Response" + }) + + +# noinspection PyUnusedLocal +@app.route("/raise-error") +async def raise_error(request): + raise Exception("Testing Sentry Integration") + + +if __name__ == '__main__': + app.run( + host="0.0.0.0", + port=getenv("PORT", 8080) + )