Skip to content

Commit

Permalink
Migrate to sentry sdk (Netflix#176)
Browse files Browse the repository at this point in the history
* Move to Sentry SDK

* Tested and added more integrations
  • Loading branch information
castrapel authored Aug 5, 2020
1 parent 66311a5 commit 1c732f7
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[settings]
known_first_party=consoleme,consoleme_default_plugins
known_third_party = aiozipkin,asgiref,billiard,bleach,boto3,botocore,celery,click,click_log,cloudaux,cryptography,dateutil,deepdiff,ed25519,elasticsearch,fakeredis,google,googleapiclient,jsonschema,jwt,logmatic,marshmallow,mock,mockredis,moto,okta_jwt,onelogin,pandas,pip,pkg_resources,policy_sentry,policyuniverse,pydantic,pytest,pytz,raven,redis,requests,retrying,setuptools,simplejson,tenacity,tornado,ujson,uvloop,validate_email,yaml
known_third_party = aiozipkin,asgiref,billiard,bleach,boto3,botocore,celery,click,click_log,cloudaux,cryptography,dateutil,deepdiff,ed25519,elasticsearch,fakeredis,google,googleapiclient,jsonschema,jwt,logmatic,marshmallow,mock,mockredis,moto,okta_jwt,onelogin,pandas,pip,pkg_resources,policy_sentry,policyuniverse,pydantic,pytest,pytz,redis,requests,retrying,sentry_sdk,setuptools,simplejson,tenacity,tornado,ujson,uvloop,validate_email,yaml
multi_line_output=3
include_trailing_comma=True
balanced_wrapping=True
Expand Down
2 changes: 0 additions & 2 deletions consoleme/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ def main():
app = make_app(jwt_validator=lambda x: {})
else:
app = make_app()
if config.sentry:
app.sentry_client = config.sentry
return app


Expand Down
23 changes: 15 additions & 8 deletions consoleme/celery/celery_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import Dict, Tuple, Union

import celery
import raven
import sentry_sdk
import ujson
from asgiref.sync import async_to_sync
from billiard.exceptions import SoftTimeLimitExceeded
Expand All @@ -41,8 +41,11 @@
from cloudaux.aws.s3 import list_buckets
from cloudaux.aws.sns import list_topics
from cloudaux.aws.sts import boto3_cached_conn
from raven.contrib.celery import register_logger_signal, register_signal
from retrying import retry
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
from sentry_sdk.integrations.celery import CeleryIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from sentry_sdk.integrations.tornado import TornadoIntegration

from consoleme.config import config
from consoleme.lib.account_indexers import (
Expand Down Expand Up @@ -76,11 +79,15 @@ class Celery(celery.Celery):
def on_configure(self) -> None:
sentry_dsn = config.get("sentry.dsn")
if sentry_dsn:
client = raven.Client(sentry_dsn)
# register a custom filter to filter out duplicate logs
register_logger_signal(client)
# hook into the Celery error handler
register_signal(client)
sentry_sdk.init(
sentry_dsn,
integrations=[
TornadoIntegration(),
CeleryIntegration(),
AioHttpIntegration(),
RedisIntegration(),
],
)


app = Celery(
Expand Down Expand Up @@ -1336,7 +1343,7 @@ def _get_delivery_channels(**kwargs) -> list:
}
stats.count(f"{function}.error", tags={"account_id": account_id})
log.error(log_data, exc_info=True)
config.sentry.captureException()
sentry_sdk.capture_exception()
raise

log_data = {
Expand Down
3 changes: 0 additions & 3 deletions consoleme/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import yaml
from asgiref.sync import async_to_sync
from pytz import timezone
from raven.contrib.tornado import AsyncSentryClient

from consoleme.lib.plugins import get_plugin_by_name

Expand Down Expand Up @@ -217,5 +216,3 @@ def filter(self, record: LogRecord) -> bool:
hostname = socket.gethostname()
api_spec = {}
dir_ref = dir

sentry: AsyncSentryClient = AsyncSentryClient(get("sentry.dsn"))
5 changes: 2 additions & 3 deletions consoleme/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import tornado.web
import ujson as json
from asgiref.sync import sync_to_async
from raven.contrib.tornado import SentryMixin
from tornado import httputil

from consoleme.config import config
Expand Down Expand Up @@ -40,7 +39,7 @@
group_mapping = get_plugin_by_name(config.get("plugins.group_mapping"))()


class BaseJSONHandler(SentryMixin, tornado.web.RequestHandler):
class BaseJSONHandler(tornado.web.RequestHandler):
# These methods are returned in OPTIONS requests.
# Default methods can be overridden by setting this variable in child classes.
allowed_methods = ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"]
Expand Down Expand Up @@ -109,7 +108,7 @@ def get_current_user(self):
return tkn


class BaseHandler(SentryMixin, tornado.web.RequestHandler):
class BaseHandler(tornado.web.RequestHandler):
"""Default BaseHandler."""

def write_error(self, status_code: int, **kwargs: Any) -> None:
Expand Down
3 changes: 2 additions & 1 deletion consoleme/handlers/v1/credentials.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sentry_sdk
import tornado.escape
import tornado.web
import ujson as json
Expand Down Expand Up @@ -70,7 +71,7 @@ async def raise_if_certificate_too_old(self, role, log_data=None):
try:
max_cert_age = await group_mapping.get_max_cert_age_for_role(role)
except Exception as e:
config.sentry.captureException()
sentry_sdk.capture_exception()
log_data["error"] = e
log_data[
"message"
Expand Down
7 changes: 4 additions & 3 deletions consoleme/handlers/v1/policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Dict, List, Optional
from urllib.parse import quote_plus

import sentry_sdk
import tornado.escape
import ujson as json
from policyuniverse.expander_minimizer import _expand_wildcard_action
Expand Down Expand Up @@ -624,7 +625,7 @@ async def post(self):
arn, resource_actions, account_id
)
except Exception as e:
config.sentry.captureException()
sentry_sdk.capture_exception()
log_data["error"] = e
log.error(log_data, exc_info=True)
resource_actions = {}
Expand Down Expand Up @@ -652,7 +653,7 @@ async def post(self):
account_id, arn, request
)
except Exception as e:
config.sentry.captureException()
sentry_sdk.capture_exception()
await write_json_error(e, obj=self)
await self.finish()
return
Expand Down Expand Up @@ -932,7 +933,7 @@ async def post(self, request_id):
arn, resource_actions, account_id
)
except Exception as e:
config.sentry.captureException()
sentry_sdk.capture_exception()
log_data["error"] = e
log.error(log_data, exc_info=True)
resource_actions = {}
Expand Down
3 changes: 2 additions & 1 deletion consoleme/handlers/v2/generate_changes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys

import sentry_sdk
from pydantic import ValidationError

from consoleme.config import config
Expand Down Expand Up @@ -99,7 +100,7 @@ async def post(self):
log_data["message"] = "Unknown Exception occurred while generating changes"
log.error(log_data, exc_info=True)
stats.count(f"{log_data['function']}.exception", tags={"user": self.user})
config.sentry.captureException(tags={"user": self.user})
sentry_sdk.capture_exception(tags={"user": self.user})
self.write_error(500, message="Error generating changes: " + str(e))
return

Expand Down
7 changes: 3 additions & 4 deletions consoleme/handlers/v2/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time
import uuid

import sentry_sdk
import ujson as json
from pydantic import ValidationError

Expand Down Expand Up @@ -114,9 +115,7 @@ async def post(self):
self.write_error(403, message="This page is not yet available to all users")
return

tags = {
"user": self.user,
}
tags = {"user": self.user}
stats.count("RequestHandler.post", tags=tags)
log_data = {
"function": f"{__name__}.{self.__class__.__name__}.{sys._getframe().f_code.co_name}",
Expand Down Expand Up @@ -224,7 +223,7 @@ async def post(self):
log_data["message"] = "Unknown Exception occurred while parsing request"
log.error(log_data, exc_info=True)
stats.count(f"{log_data['function']}.exception", tags={"user": self.user})
config.sentry.captureException(tags={"user": self.user})
sentry_sdk.capture_exception(tags={"user": self.user})
self.write_error(500, message="Error parsing request: " + str(e))
return

Expand Down
9 changes: 5 additions & 4 deletions consoleme/handlers/v2/roles.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys

import sentry_sdk
import ujson as json
from pydantic import ValidationError

Expand Down Expand Up @@ -69,7 +70,7 @@ async def post(self):
stats.count(
f"{log_data['function']}.validation_exception", tags={"user": self.user}
)
config.sentry.captureException()
sentry_sdk.capture_exception()
self.write_error(400, message="Error validating input: " + str(e))
return

Expand All @@ -89,7 +90,7 @@ async def post(self):
"role_name": create_model.role_name,
},
)
config.sentry.captureException()
sentry_sdk.capture_exception()
self.write_error(500, message="Exception occurred cloning role: " + str(e))
return

Expand Down Expand Up @@ -363,7 +364,7 @@ async def post(self):
stats.count(
f"{log_data['function']}.validation_exception", tags={"user": self.user}
)
config.sentry.captureException()
sentry_sdk.capture_exception()
self.write_error(400, message="Error validating input: " + str(e))
return

Expand All @@ -383,7 +384,7 @@ async def post(self):
"role_name": clone_model.role_name,
},
)
config.sentry.captureException()
sentry_sdk.capture_exception()
self.write_error(500, message="Exception occurred cloning role: " + str(e))
return

Expand Down
13 changes: 7 additions & 6 deletions consoleme/lib/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any, Dict, List, Optional, Tuple

import pytz
import sentry_sdk
from asgiref.sync import sync_to_async
from botocore.exceptions import ClientError
from cloudaux import CloudAux
Expand Down Expand Up @@ -594,7 +595,7 @@ async def create_iam_role(create_model: RoleCreationRequestModel, username):
}
)
results["errors"] += 1
config.sentry.captureException()
sentry_sdk.capture_exception()
# Since we were unable to create the role, no point continuing, just return
return results

Expand Down Expand Up @@ -635,7 +636,7 @@ async def create_iam_role(create_model: RoleCreationRequestModel, username):
] = "Exception occurred creating/attaching instance profile"
log_data["error"] = str(e)
log.error(log_data, exc_info=True)
config.sentry.captureException()
sentry_sdk.capture_exception()
results["action_results"].append(
{
"status": "error",
Expand Down Expand Up @@ -755,7 +756,7 @@ async def clone_iam_role(clone_model: CloneRoleRequestModel, username):
}
)
results["errors"] += 1
config.sentry.captureException()
sentry_sdk.capture_exception()
# Since we were unable to create the role, no point continuing, just return
return results

Expand Down Expand Up @@ -823,7 +824,7 @@ async def clone_iam_role(clone_model: CloneRoleRequestModel, username):
] = "Exception occurred creating/attaching instance profile"
log_data["error"] = str(e)
log.error(log_data, exc_info=True)
config.sentry.captureException()
sentry_sdk.capture_exception()
results["action_results"].append(
{
"status": "error",
Expand Down Expand Up @@ -858,7 +859,7 @@ async def clone_iam_role(clone_model: CloneRoleRequestModel, username):
log_data["message"] = "Exception occurred copying inline policy"
log_data["error"] = str(e)
log.error(log_data, exc_info=True)
config.sentry.captureException()
sentry_sdk.capture_exception()
results["action_results"].append(
{
"status": "error",
Expand Down Expand Up @@ -889,7 +890,7 @@ async def clone_iam_role(clone_model: CloneRoleRequestModel, username):
log_data["message"] = "Exception occurred copying managed policy"
log_data["error"] = str(e)
log.error(log_data, exc_info=True)
config.sentry.captureException()
sentry_sdk.capture_exception()
results["action_results"].append(
{
"status": "error",
Expand Down
13 changes: 7 additions & 6 deletions consoleme/lib/v2/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import uuid
from hashlib import sha256

import sentry_sdk
import ujson as json
from asgiref.sync import sync_to_async
from cloudaux.aws.sts import boto3_cached_conn
Expand Down Expand Up @@ -431,7 +432,7 @@ async def apply_changes_to_role(
log.error(log_data)
response.errors += 1
response.action_results.append(
ActionResult(status="error", message=log_data["message"],)
ActionResult(status="error", message=log_data["message"])
)
return

Expand Down Expand Up @@ -476,7 +477,7 @@ async def apply_changes_to_role(
log_data["message"] = "Exception occurred applying inline policy"
log_data["error"] = str(e)
log.error(log_data, exc_info=True)
config.sentry.captureException()
sentry_sdk.capture_exception()
response.errors += 1
response.action_results.append(
ActionResult(
Expand All @@ -501,7 +502,7 @@ async def apply_changes_to_role(
log_data["message"] = "Exception occurred deleting inline policy"
log_data["error"] = str(e)
log.error(log_data, exc_info=True)
config.sentry.captureException()
sentry_sdk.capture_exception()
response.errors += 1
response.action_results.append(
ActionResult(
Expand All @@ -527,7 +528,7 @@ async def apply_changes_to_role(
log_data["message"] = "Exception occurred attaching managed policy"
log_data["error"] = str(e)
log.error(log_data, exc_info=True)
config.sentry.captureException()
sentry_sdk.capture_exception()
response.errors += 1
response.action_results.append(
ActionResult(
Expand All @@ -552,7 +553,7 @@ async def apply_changes_to_role(
log_data["message"] = "Exception occurred detaching managed policy"
log_data["error"] = str(e)
log.error(log_data, exc_info=True)
config.sentry.captureException()
sentry_sdk.capture_exception()
response.errors += 1
response.action_results.append(
ActionResult(
Expand Down Expand Up @@ -580,7 +581,7 @@ async def apply_changes_to_role(
] = "Exception occurred updating assume role policy policy"
log_data["error"] = str(e)
log.error(log_data, exc_info=True)
config.sentry.captureException()
sentry_sdk.capture_exception()
response.errors += 1
response.action_results.append(
ActionResult(
Expand Down
16 changes: 14 additions & 2 deletions consoleme/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

import pkg_resources
import requests
import sentry_sdk
import tornado.autoreload
import tornado.web
from raven.contrib.tornado import AsyncSentryClient
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
from sentry_sdk.integrations.celery import CeleryIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from sentry_sdk.integrations.tornado import TornadoIntegration

import consoleme
from consoleme.config import config
Expand Down Expand Up @@ -171,6 +175,14 @@ def make_app(jwt_validator=None):
sentry_dsn = config.get("sentry.dsn")

if sentry_dsn:
app.sentry_client = AsyncSentryClient(config.get("sentry.dsn"))
sentry_sdk.init(
dsn=sentry_dsn,
integrations=[
TornadoIntegration(),
CeleryIntegration(),
AioHttpIntegration(),
RedisIntegration(),
],
)

return app
Loading

0 comments on commit 1c732f7

Please sign in to comment.