Skip to content

Commit

Permalink
Merge pull request #17 from datamade/hcg/upgrade-sentry
Browse files Browse the repository at this point in the history
Upgrade Sentry SDK
  • Loading branch information
hancush authored Jul 13, 2020
2 parents 406b234 + b52e96e commit 69950af
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
49 changes: 39 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from flask import Flask, request, make_response, abort
from io import StringIO, BytesIO
import json
import requests
import urllib.parse

from boto.s3.connection import S3Connection
from boto.s3.key import Key
from boto.exception import S3ResponseError
from app_config import AWS_KEY, AWS_SECRET, SENTRY_DSN, IMAGE_SECRET
from io import StringIO, BytesIO
from flask import Flask, request, make_response, abort
from flask_cors import cross_origin
import requests

import sentry_sdk
from sentry_sdk import capture_message
from sentry_sdk.integrations.flask import FlaskIntegration

from app_config import AWS_KEY, AWS_SECRET, SENTRY_DSN, IMAGE_SECRET
from sentry_handlers import before_send

import urllib.parse

app = Flask(__name__)

Expand All @@ -20,8 +27,15 @@

WHITELIST = ['ord.legistar.com', 'chicago.legistar.com', 'metro.legistar1.com', 'metro.legistar.com']

from raven.contrib.flask import Sentry
sentry = Sentry(app, dsn=SENTRY_DSN)
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=[FlaskIntegration()],
before_send=before_send
)

MISSING_PIN_ERROR = 'Could not find image for PIN'
MISSING_DOCUMENT_ERROR = 'Could not find document at URL'
MISSING_IMAGE_ERROR = 'Image does not exist at'

@app.route('/<pin>.jpg')
def index(pin):
Expand All @@ -47,7 +61,7 @@ def index(pin):
s3_key.set_acl('public-read')

else:
sentry.captureMessage('Could not find image for PIN %s' % pin)
capture_message('{0} {1}'.format(MISSING_PIN_ERROR, pin))
abort(404)

output.seek(0)
Expand Down Expand Up @@ -106,7 +120,7 @@ def document(city):
s3_key.set_acl('public-read')

else:
sentry.captureMessage('Could not find document at URL %s' % document_url)
capture_message('{0} {1}'.format(MISSING_DOCUMENT_ERROR, document_url))
abort(doc.status_code)

response = make_response(output.getvalue())
Expand Down Expand Up @@ -161,14 +175,29 @@ def image():
s3_key.set_acl('public-read')

else:
sentry.captureMessage('Image does not exist at %s' % image_url)
capture_message('{0} {1}'.format(MISSING_IMAGE_ERROR, image_url))
abort(404)

output.seek(0)
response = make_response(output.read())
response.headers['Content-Type'] = 'image/jpg'
return response

@app.route('/test-logging/')
def test_logging():
from uuid import uuid4

base_messages = (
MISSING_PIN_ERROR,
MISSING_DOCUMENT_ERROR,
MISSING_IMAGE_ERROR,
)

for message in base_messages:
capture_message('{0} {1}'.format(message, uuid4()))

raise Exception('Exception raised')


if __name__ == "__main__":
import os
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Flask==0.10.1
Flask==0.11
requests==2.7.0
boto==2.38.0
flask_cors==2.1.2
raven[flask]
sentry-sdk[flask]==0.16.1
gunicorn
17 changes: 17 additions & 0 deletions sentry_handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def before_send(event, hint):
'''
Group like messages under the same fingerprint.
'''
from app import MISSING_PIN_ERROR, MISSING_DOCUMENT_ERROR, MISSING_IMAGE_ERROR

event_message = event.get('message')

if event_message:
if event_message.startswith(MISSING_PIN_ERROR):
event['fingerprint'] = ['no-image-for-pin']
elif event_message.startswith(MISSING_DOCUMENT_ERROR):
event['fingerprint'] = ['no-document-for-url']
elif event_message.startswith(MISSING_IMAGE_ERROR):
event['fingerprint'] = ['no-image-for-url']

return event

0 comments on commit 69950af

Please sign in to comment.