Created
July 7, 2021 10:41
-
-
Save thanakijwanavit/729e42ea4746f88909a3dff4976d1130 to your computer and use it in GitHub Desktop.
sentry breadcrumbs example
This file contains hidden or 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
#export | |
from nicHelper.exception import errorString | |
import sentry_sdk | |
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration | |
from sentry_sdk import capture_message, capture_exception, add_breadcrumb, push_scope | |
sentry_sdk.init( | |
dsn="https://[email protected]/5852078", | |
integrations=[AwsLambdaIntegration()], | |
traces_sample_rate=0.2, | |
) | |
class SchemaValidationError(Exception): | |
pass | |
class PickingRequestError(Exception): | |
pass | |
class PickingFailed(Exception): | |
pass | |
def recordEvent(event): | |
add_breadcrumb( | |
category='event', | |
data=event, | |
level='info', | |
message='receivedEvent' | |
) | |
def recordMessage(message:str): | |
add_breadcrumb( | |
category='message', | |
message=message | |
) | |
def recordObject(item:dict, description:str): | |
add_breadcrumb( | |
category='object', | |
data=item, | |
level='info', | |
message=item | |
) | |
def sendToPicking(event, *args): | |
recordEvent(event) | |
try: | |
# extract input data | |
inputData:dict = Event.parseBody(event) | |
recordMessage(f'orderId is {inputData["orderId"]}\n') | |
## get order from database | |
order:dict = getOrder(inputData['orderId']) | |
recordObject(order,'order') | |
## validate data gathered | |
try: | |
validateUrl(schemaUrl,order, format_='yaml') | |
recordMessage("order schema validated") | |
except ValidationError as e: | |
capture_exception(SchemaValidationError(e)) | |
## send to picking | |
try: | |
r:requests.Response = requests.post(PICKINGENDPOINT, json = order, auth=AUTH) | |
recordObject(r.json(), 'picking result') | |
except Exception as e: | |
capture_exception(PickingRequestError(e)) | |
## check if picking is successful | |
try: | |
if r.status_code < 400: | |
recordMessage('picking success') | |
return Response.returnSuccess(order) | |
elif r.json().get('message') == 'the ref_order was duplicate.': | |
capture_exception(PickingFailed(f'picking is a duplicate raw response is{r.json()}, order is {order}')) | |
pickingOrder = requests.post(GETPICKINGENDPOINT, json = {'ref_order': order['orderId']}).json() | |
return Response.returnSuccess(statusCode = 202, body = pickingOrder) | |
else: | |
capture_exception(PickingFailed(f'unknown magento picking error raw response is {r.json()} order is {order}')) | |
return Response.returnError(f'{r.json()}') | |
except Exception as e: | |
capture_exception(e) | |
return Response.returnError(f'{errorString()}, {r.json()}') | |
except Exception as e: | |
capture_exception(e) | |
return Response.returnError(f'error {errorString()}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment