Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bootstrapping the xray sdk/middleware #1923

Merged
merged 11 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from urllib.parse import urljoin

import timeago
from aws_xray_sdk.core import xray_recorder
from flask import (
Markup,
current_app,
Expand Down Expand Up @@ -699,6 +700,9 @@ def useful_headers_after_request(response):

def register_errorhandlers(application): # noqa (C901 too complex)
def _error_response(error_code):

xray_recorder.begin_segment("error_response")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we need to call xray_recorder.end_segment somewhere? The basic usage doc mentions it might have to be called before begin_segment sometimes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder too if x-ray would detect the error segment automatically with the error response that is generated. Are we testing the segment feature of x-ray here or trying to fix an issue where x-ray fails to detect errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added as a failsafe for certain errors where the code path results in an empty/null segment. This code will verify that all errors are working as expected and that x-ray is still reporting traces.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stay tuned for code updates


resp = make_response(render_template("error/{0}.html".format(error_code)), error_code)
return useful_headers_after_request(resp)

Expand Down
5 changes: 5 additions & 0 deletions application.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os

from apig_wsgi import make_lambda_handler
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.ext.flask.middleware import XRayMiddleware
from dotenv import load_dotenv
from flask import Flask

Expand All @@ -15,6 +17,9 @@

create_app(application)

xray_recorder.configure(service='admin')
XRayMiddleware(application, xray_recorder)

apig_wsgi_handler = make_lambda_handler(application, binary_support=True)

if os.environ.get("USE_LOCAL_JINJA_TEMPLATES") == "True":
Expand Down
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ ignore_missing_imports = True
ignore_missing_imports = True

[mypy-newrelic.*]
ignore_missing_imports = True

[mypy-aws_xray_sdk.*]
ignore_missing_imports = True
98 changes: 96 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ xlrd = "1.2.0" # this is pinned so that we can continue to support .xslm files;
# Putting upgrade on hold due to v1.0.0 using sha512 instead of sha1 by default
itsdangerous = "2.2.0" # pyup: <1.0.0
newrelic = "8.10.0" # Pinned to 8.10.0 for now, 8.11.0 caused a performance regression: https://gcdigital.slack.com/archives/C012W5K734Y/p1709668046344929
aws-xray-sdk = "^2.14.0"

[tool.poetry.group.test.dependencies]
isort = "5.13.2"
Expand Down
Loading