示例#1
0
def get_app():
    sentry_sdk.init(dsn=os.environ["SENTRY_DSN"],
                    integrations=[FalconIntegration()])
    context = create_context()

    return create_app(context, os.environ["AUTH0_ISSUER"],
                      os.environ["AUTH0_AUDIENCE"])
示例#2
0
def test_500(sentry_init, capture_events):
    sentry_init(integrations=[FalconIntegration()])

    app = falcon.API()

    class Resource:
        def on_get(self, req, resp):
            1 / 0

    app.add_route("/", Resource())

    def http500_handler(ex, req, resp, params):
        sentry_sdk.capture_exception(ex)
        resp.media = {
            "message": "Sentry error: %s" % sentry_sdk.last_event_id()
        }

    app.add_error_handler(Exception, http500_handler)

    events = capture_events()

    client = falcon.testing.TestClient(app)
    response = client.simulate_get("/")

    event, = events
    assert response.json == {"message": "Sentry error: %s" % event["event_id"]}
示例#3
0
def test_error_in_errorhandler(sentry_init, capture_events):
    sentry_init(integrations=[FalconIntegration()])

    app = falcon.API()

    class Resource:
        def on_get(self, req, resp):
            raise ValueError()

    app.add_route("/", Resource())

    def http500_handler(ex, req, resp, params):
        1 / 0

    app.add_error_handler(Exception, http500_handler)

    events = capture_events()

    client = falcon.testing.TestClient(app)

    with pytest.raises(ZeroDivisionError):
        client.simulate_get("/")

    event, = events

    last_ex_values = event["exception"]["values"][-1]
    assert last_ex_values["type"] == "ZeroDivisionError"
    assert last_ex_values["stacktrace"]["frames"][-1]["vars"][
        "ex"] == "ValueError()"
示例#4
0
def test_does_not_leak_scope(sentry_init, capture_events):
    sentry_init(integrations=[FalconIntegration()])
    events = capture_events()

    with sentry_sdk.configure_scope() as scope:
        scope.set_tag("request_data", False)

    app = falcon.API()

    class Resource:
        def on_get(self, req, resp):
            with sentry_sdk.configure_scope() as scope:
                scope.set_tag("request_data", True)

            def generator():
                for row in range(1000):
                    with sentry_sdk.configure_scope() as scope:
                        assert scope._tags["request_data"]

                    yield (str(row) + "\n").encode()

            resp.stream = generator()

    app.add_route("/", Resource())

    client = falcon.testing.TestClient(app)
    response = client.simulate_get("/")

    expected_response = "".join(str(row) + "\n" for row in range(1000))
    assert response.text == expected_response
    assert not events

    with sentry_sdk.configure_scope() as scope:
        assert not scope._tags["request_data"]
示例#5
0
def test_falcon_large_json_request(sentry_init, capture_events):
    sentry_init(integrations=[FalconIntegration()])

    data = {"foo": {"bar": "a" * 2000}}

    class Resource:
        def on_post(self, req, resp):
            assert req.media == data
            sentry_sdk.capture_message("hi")
            resp.media = "ok"

    app = falcon.API()
    app.add_route("/", Resource())

    events = capture_events()

    client = falcon.testing.TestClient(app)
    response = client.simulate_post("/", json=data)
    assert response.status == falcon.HTTP_200

    event, = events
    assert event["_meta"]["request"]["data"]["foo"]["bar"] == {
        "": {
            "len": 2000,
            "rem": [["!limit", "x", 509, 512]]
        }
    }
    assert len(event["request"]["data"]["foo"]["bar"]) == 512
示例#6
0
def test_errors(sentry_init, capture_exceptions, capture_events):
    sentry_init(integrations=[FalconIntegration()], debug=True)

    class ZeroDivisionErrorResource:
        def on_get(self, req, resp):
            1 / 0

    app = falcon.API()
    app.add_route("/", ZeroDivisionErrorResource())

    exceptions = capture_exceptions()
    events = capture_events()

    client = falcon.testing.TestClient(app)

    try:
        client.simulate_get("/")
    except ZeroDivisionError:
        pass

    exc, = exceptions
    assert isinstance(exc, ZeroDivisionError)

    event, = events
    assert event["exception"]["values"][0]["mechanism"]["type"] == "falcon"
示例#7
0
文件: app.py 项目: adswa/openneuro
def create_app(annex_path):
    # If running under gunicorn, use that logger
    gunicorn_logger = logging.getLogger('gunicorn.error')
    logging.basicConfig(handlers=gunicorn_logger.handlers,
                        level=gunicorn_logger.level)
    if datalad_service.config.SENTRY_DSN:
        sentry_sdk.init(
            dsn=datalad_service.config.SENTRY_DSN,
            integrations=[FalconIntegration()]
        )

    api = falcon.API(
        middleware=[AuthenticateMiddleware(), ElasticApmMiddleware(service_name='datalad-service', server_url=datalad_service.config.ELASTIC_APM_SERVER_URL)])
    api.router_options.converters['path'] = PathConverter

    store = DataladStore(annex_path)

    heartbeat = HeartbeatResource()
    datasets = DatasetResource(store)
    dataset_draft = DraftResource(store)
    dataset_description = DescriptionResource(store)
    dataset_files = FilesResource(store)
    dataset_publish = PublishResource(store)
    dataset_snapshots = SnapshotResource(store)
    dataset_upload = UploadResource(store)
    dataset_upload_file = UploadFileResource(store)

    api.add_route('/heartbeat', heartbeat)

    api.add_route('/datasets', datasets)
    api.add_route('/datasets/{dataset}', datasets)

    api.add_route('/datasets/{dataset}/draft', dataset_draft)
    api.add_route('/datasets/{dataset}/description', dataset_description)

    api.add_route('/datasets/{dataset}/files', dataset_files)
    api.add_route('/datasets/{dataset}/files/{filename:path}', dataset_files)

    api.add_route('/datasets/{dataset}/snapshots', dataset_snapshots)
    api.add_route(
        '/datasets/{dataset}/snapshots/{snapshot}', dataset_snapshots)
    api.add_route(
        '/datasets/{dataset}/snapshots/{snapshot}/files', dataset_files)
    api.add_route(
        '/datasets/{dataset}/snapshots/{snapshot}/files/{filename:path}', dataset_files)

    api.add_route(
        '/datasets/{dataset}/publish', dataset_publish
    )

    api.add_route(
        '/datasets/{dataset}/upload/{upload}', dataset_upload
    )
    api.add_route(
        '/uploads/{worker}/{dataset}/{upload}/{filename:path}', dataset_upload_file
    )

    return api
示例#8
0
def test_has_context(sentry_init, capture_events, make_client):
    sentry_init(integrations=[FalconIntegration()])
    events = capture_events()

    client = make_client()
    response = client.simulate_get("/message")
    assert response.status == falcon.HTTP_200

    event, = events
    assert event["transaction"] == "/message"  # Falcon URI template
    assert "data" not in event["request"]
    assert event["request"]["url"] == "http://falconframework.org/message"
示例#9
0
def test_transaction_style(sentry_init, make_client, capture_events,
                           transaction_style, expected_transaction):
    integration = FalconIntegration(transaction_style=transaction_style)
    sentry_init(integrations=[integration])
    events = capture_events()

    client = make_client()
    response = client.simulate_get("/message")
    assert response.status == falcon.HTTP_200

    event, = events
    assert event["transaction"] == expected_transaction
示例#10
0
def test_bad_request_not_captured(sentry_init, capture_events):
    sentry_init(integrations=[FalconIntegration()])
    events = capture_events()

    app = falcon.API()

    class Resource:
        def on_get(self, req, resp):
            raise falcon.HTTPBadRequest()

    app.add_route("/", Resource())

    client = falcon.testing.TestClient(app)

    client.simulate_get("/")

    assert not events
示例#11
0
def test_raised_4xx_errors(sentry_init, capture_exceptions, capture_events):
    sentry_init(integrations=[FalconIntegration()], debug=True)

    class Resource:
        def on_get(self, req, resp):
            raise falcon.HTTPError(falcon.HTTP_400)

    app = falcon.API()
    app.add_route("/", Resource())

    exceptions = capture_exceptions()
    events = capture_events()

    client = falcon.testing.TestClient(app)
    client.simulate_get("/")

    assert len(exceptions) == 0
    assert len(events) == 0
示例#12
0
def initialize_sentry(cfg: attrdict.AttrDict) -> None:
    """ Initializes the Sentry agent to capture exceptions which are then
        displayed under the sentry.io dashboard and the `fightfor-graphql`
        project.

    Args:
        cfg (attrdict.AttrDict): The service configuration dictionary.
    """

    # Initialization is skipped if the Sentry configuration has not been
    # defined.
    if not cfg.sentry.dsn:
        return None

    sentry_sdk.init(
        dsn=cfg.sentry.dsn,
        integrations=[FalconIntegration()],
        send_default_pii=False,
    )
示例#13
0
def test_falcon_empty_json_request(sentry_init, capture_events, data):
    sentry_init(integrations=[FalconIntegration()])

    class Resource:
        def on_post(self, req, resp):
            assert req.media == data
            sentry_sdk.capture_message("hi")
            resp.media = "ok"

    app = falcon.API()
    app.add_route("/", Resource())

    events = capture_events()

    client = falcon.testing.TestClient(app)
    response = client.simulate_post("/", json=data)
    assert response.status == falcon.HTTP_200

    event, = events
    assert event["request"]["data"] == data
示例#14
0
def test_falcon_raw_data_request(sentry_init, capture_events):
    sentry_init(integrations=[FalconIntegration()])

    class Resource:
        def on_post(self, req, resp):
            sentry_sdk.capture_message("hi")
            resp.media = "ok"

    app = falcon.API()
    app.add_route("/", Resource())

    events = capture_events()

    client = falcon.testing.TestClient(app)
    response = client.simulate_post("/", body="hi")
    assert response.status == falcon.HTTP_200

    event, = events
    assert event["request"]["headers"]["Content-Length"] == "2"
    assert event["request"]["data"] == ""
示例#15
0
def test_http_status(sentry_init, capture_exceptions, capture_events):
    """
    This just demonstrates, that if Falcon raises a HTTPStatus with code 500
    (instead of a HTTPError with code 500) Sentry will not capture it.
    """
    sentry_init(integrations=[FalconIntegration()], debug=True)

    class Resource:
        def on_get(self, req, resp):
            raise falcon.http_status.HTTPStatus(falcon.HTTP_508)

    app = falcon.API()
    app.add_route("/", Resource())

    exceptions = capture_exceptions()
    events = capture_events()

    client = falcon.testing.TestClient(app)
    client.simulate_get("/")

    assert len(exceptions) == 0
    assert len(events) == 0
示例#16
0
def test_raised_5xx_errors(sentry_init, capture_exceptions, capture_events):
    sentry_init(integrations=[FalconIntegration()], debug=True)

    class Resource:
        def on_get(self, req, resp):
            raise falcon.HTTPError(falcon.HTTP_502)

    app = falcon.API()
    app.add_route("/", Resource())

    exceptions = capture_exceptions()
    events = capture_events()

    client = falcon.testing.TestClient(app)
    client.simulate_get("/")

    (exc, ) = exceptions
    assert isinstance(exc, falcon.HTTPError)

    (event, ) = events
    assert event["exception"]["values"][0]["mechanism"]["type"] == "falcon"
    assert event["exception"]["values"][0]["type"] == "HTTPError"
示例#17
0
def test_logging(sentry_init, capture_events):
    sentry_init(
        integrations=[FalconIntegration(), LoggingIntegration(event_level="ERROR")]
    )

    logger = logging.getLogger()

    app = falcon.API()

    class Resource:
        def on_get(self, req, resp):
            logger.error("hi")
            resp.media = "ok"

    app.add_route("/", Resource())

    events = capture_events()

    client = falcon.testing.TestClient(app)
    client.simulate_get("/")

    event, = events
    assert event["level"] == "error"
示例#18
0
def create_app(annex_path):
    # If running under gunicorn, use that logger
    gunicorn_logger = logging.getLogger('gunicorn.error')
    logging.basicConfig(handlers=gunicorn_logger.handlers,
                        level=gunicorn_logger.level)
    if datalad_service.config.SENTRY_DSN:
        sentry_sdk.init(
            dsn=datalad_service.config.SENTRY_DSN,
            integrations=[FalconIntegration()]
        )

    middleware = [AuthenticateMiddleware()]
    if datalad_service.config.ELASTIC_APM_SERVER_URL:
        middleware.append(ElasticApmMiddleware(service_name='datalad-service',
                                               server_url=datalad_service.config.ELASTIC_APM_SERVER_URL))

    api = falcon.API(
        middleware=middleware)
    api.router_options.converters['path'] = PathConverter

    store = DataladStore(annex_path)

    heartbeat = HeartbeatResource()
    datasets = DatasetResource(store)
    dataset_draft = DraftResource(store)
    dataset_validation = ValidationResource(store)
    dataset_history = HistoryResource(store)
    dataset_description = DescriptionResource(store)
    dataset_files = FilesResource(store)
    dataset_annex_objects = AnnexObjectsResource(store)
    dataset_publish = PublishResource(store)
    dataset_snapshots = SnapshotResource(store)
    dataset_upload = UploadResource(store)
    dataset_upload_file = UploadFileResource(store)
    dataset_git_refs_resource = GitRefsResource(store)
    dataset_git_receive_resource = GitReceiveResource(store)
    dataset_git_upload_resource = GitUploadResource(store)
    dataset_git_annex_resource = GitAnnexResource(store)
    dataset_reexporter_resources = ReexporterResource(store)
    dataset_reset_resource = ResetResource(store)
    dataset_remote_import_resource = RemoteImportResource(store)

    api.add_route('/heartbeat', heartbeat)

    api.add_route('/datasets', datasets)
    api.add_route('/datasets/{dataset}', datasets)

    api.add_route('/datasets/{dataset}/draft', dataset_draft)
    api.add_route('/datasets/{dataset}/history', dataset_history)
    api.add_route('/datasets/{dataset}/description', dataset_description)
    api.add_route('/datasets/{dataset}/validate/{hexsha}', dataset_validation)
    api.add_route('/datasets/{dataset}/reset/{hexsha}', dataset_reset_resource)

    api.add_route('/datasets/{dataset}/files', dataset_files)
    api.add_route('/datasets/{dataset}/files/{filename:path}', dataset_files)

    api.add_route('/datasets/{dataset}/snapshots', dataset_snapshots)
    api.add_route(
        '/datasets/{dataset}/snapshots/{snapshot}', dataset_snapshots)
    api.add_route(
        '/datasets/{dataset}/snapshots/{snapshot}/files', dataset_files)
    api.add_route(
        '/datasets/{dataset}/snapshots/{snapshot}/files/{filename:path}', dataset_files)
    api.add_route(
        '/datasets/{dataset}/snapshots/{snapshot}/annex-key/{annex_key}', dataset_annex_objects)

    api.add_route(
        '/datasets/{dataset}/publish', dataset_publish
    )
    api.add_route('/datasets/{dataset}/reexport-remotes',
                  dataset_reexporter_resources)

    api.add_route(
        '/datasets/{dataset}/upload/{upload}', dataset_upload
    )
    api.add_route(
        '/uploads/{worker}/{dataset}/{upload}/{filename:path}', dataset_upload_file
    )

    api.add_route('/git/{worker}/{dataset}/info/refs',
                  dataset_git_refs_resource)
    api.add_route('/git/{worker}/{dataset}/git-receive-pack',
                  dataset_git_receive_resource)
    api.add_route('/git/{worker}/{dataset}/git-upload-pack',
                  dataset_git_upload_resource)
    api.add_route('/git/{worker}/{dataset}/annex/{key}',
                  dataset_git_annex_resource)

    api.add_route('/datasets/{dataset}/import/{import_id}',
                  dataset_remote_import_resource)

    return api
示例#19
0
文件: api.py 项目: Wauplin/robotoff
)
from robotoff.models import ProductInsight, UserAnnotation
from robotoff.off import http_session, OFFAuthentication
from robotoff.products import get_product_dataset_etag
from robotoff.utils import get_logger, get_image_from_url
from robotoff.utils.es import get_es_client
from robotoff.utils.i18n import TranslationStore
from robotoff.utils.types import JSONType
from robotoff.workers.client import send_ipc_event

import sentry_sdk
from sentry_sdk.integrations.falcon import FalconIntegration

logger = get_logger()

sentry_sdk.init(dsn=settings.SENTRY_DSN, integrations=[FalconIntegration()])

es_client = get_es_client()

TRANSLATION_STORE = TranslationStore()
TRANSLATION_STORE.load()


class ProductInsightResource:
    def on_get(self, req: falcon.Request, resp: falcon.Response, barcode: str):
        server_domain: Optional[str] = req.get_param("server_domain")
        response: JSONType = {}
        insights = [
            i.serialize() for i in get_insights(
                barcode=barcode, server_domain=server_domain, limit=None)
        ]
示例#20
0
import logging

import sentry_sdk
from envparse import env
from sentry_sdk.integrations.falcon import FalconIntegration

# загружаем конфиг
env.read_envfile()

# включаем логи
logging.basicConfig(format='[%(asctime)s][%(levelname)s] %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)

# включаем Sentry
if env('SENTRY_DSN', default=None) is not None:
    sentry_sdk.init(env('SENTRY_DSN'), integrations=[FalconIntegration()])
示例#21
0
import logging
import sentry_sdk
from envparse import env
from sentry_sdk.integrations.celery import CeleryIntegration
from sentry_sdk.integrations.falcon import FalconIntegration
from sentry_sdk.integrations.redis import RedisIntegration

# загружаем конфиг
env.read_envfile()

# включаем логи
logging.basicConfig(format='[%(asctime)s][%(levelname)s] %(message)s',
                    level=logging.INFO)

# formatter = json_log_formatter.JSONFormatter()

# json_handler = logging.FileHandler(filename='/var/log/wildsearch_app_log.json')
# json_handler.setFormatter(formatter)

logger = logging.getLogger(__name__)
# logger.addHandler(json_handler)


# включаем Sentry
if env('SENTRY_DSN', default=None) is not None:
    sentry_sdk.init(env('SENTRY_DSN'), integrations=[FalconIntegration(), CeleryIntegration(), RedisIntegration()])
示例#22
0
import os
import json
import falcon
import sentry_sdk
import logger

from sentry_sdk.integrations.falcon import FalconIntegration
from falcon_cors import CORS

from api_resources import QuestionsResource, AnswersResource, RandomQuestionResource

sentry_sdk.init(
    dsn=os.getenv("SENTRY_DSN"),
    environment=os.getenv("STAGE"),
    integrations=[FalconIntegration()],
)


def handle_404(req, resp):
    """Handle any request if there is no matching resource."""
    resp.status = falcon.HTTP_404
    resp.body = "Not found"


class HealthCheckResource:
    def on_get(self, request, response):
        body = {"data": {"status": "ok"}}
        response.body = json.dumps(body)


cors = CORS(allow_all_origins=True)
示例#23
0
from robotoff.prediction.object_detection import ObjectDetectionModelRegistry
from robotoff.prediction.ocr.dataclass import OCRParsingException
from robotoff.prediction.types import PredictionType
from robotoff.products import get_product_dataset_etag
from robotoff.spellcheck import SPELLCHECKERS, Spellchecker
from robotoff.taxonomy import TaxonomyType, get_taxonomy, match_unprefixed_value
from robotoff.utils import get_image_from_url, get_logger, http_session
from robotoff.utils.es import get_es_client
from robotoff.utils.i18n import TranslationStore
from robotoff.utils.text import get_tag
from robotoff.utils.types import JSONType
from robotoff.workers.client import send_ipc_event

logger = get_logger()

settings.init_sentry(integrations=[FalconIntegration()])

es_client = get_es_client()

TRANSLATION_STORE = TranslationStore()
TRANSLATION_STORE.load()


def _get_skip_voted_on(
    auth: Optional[OFFAuthentication], device_id: str
) -> SkipVotedOn:
    """Helper function for constructing SkipVotedOn objects based on request params."""
    if not auth:
        return SkipVotedOn(SkipVotedType.DEVICE_ID, device_id)

    username: Optional[str] = auth.get_username()