Skip to content

Commit

Permalink
add monitoring examples and documents
Browse files Browse the repository at this point in the history
  • Loading branch information
harshanarayana committed Dec 28, 2018
1 parent 9c6b835 commit b7702bc
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/sanic/examples.rst
Expand Up @@ -107,6 +107,26 @@ Monitoring and Error Handling

.. literalinclude:: ../../examples/exception_monitoring.py

Monitoring using external Service Providers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* `LogDNA <https://logdna.com/>`_

.. literalinclude:: ../../examples/logdna_example.py

* `RayGun <https://raygun.com/>`_

.. literalinclude:: ../../examples/raygun_example.py

* `Rollbar <https://rollbar.com>`_

.. literalinclude:: ../../examples/rollbar_example.py

* `Sentry <http://sentry.io>`_

.. literalinclude:: ../../examples/sentry_example.py


Security
~~~~~~~~

Expand Down
61 changes: 61 additions & 0 deletions 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)
)
37 changes: 37 additions & 0 deletions 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)
)
30 changes: 30 additions & 0 deletions 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)
)
35 changes: 35 additions & 0 deletions 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)
)

0 comments on commit b7702bc

Please sign in to comment.