Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add monitoring examples and documents
- Loading branch information
1 parent
9c6b835
commit b7702bc
Showing
5 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
) |