Skip to content

Commit

Permalink
Hash AWS Lambda test functions based on current revision (#2557)
Browse files Browse the repository at this point in the history
We were using the current SDK version for determining whether an AWS Lambda function should be reused, so e.g. on PRs, this would reuse the existing functions instead of creating new ones with any changes from the PR. Changing this to use the current commit instead.

Also, use a 6 character hash instead of 5 characters, just to lower the chance for collisions a bit.
  • Loading branch information
sentrivana committed Dec 1, 2023
1 parent 916ed04 commit f9ffe96
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
26 changes: 16 additions & 10 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,11 @@ def _get_debug_hub():
pass


def get_default_release():
def get_git_revision():
# type: () -> Optional[str]
"""Try to guess a default release."""
release = os.environ.get("SENTRY_RELEASE")
if release:
return release

with open(os.path.devnull, "w+") as null:
try:
release = (
revision = (
subprocess.Popen(
["git", "rev-parse", "HEAD"],
stdout=subprocess.PIPE,
Expand All @@ -116,10 +111,21 @@ def get_default_release():
.decode("utf-8")
)
except (OSError, IOError):
pass
return None

if release:
return release
return revision


def get_default_release():
# type: () -> Optional[str]
"""Try to guess a default release."""
release = os.environ.get("SENTRY_RELEASE")
if release:
return release

release = get_git_revision()
if release is not None:
return release

for var in (
"HEROKU_SLUG_COMMIT",
Expand Down
4 changes: 3 additions & 1 deletion tests/integrations/aws_lambda/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import tempfile

from sentry_sdk.consts import VERSION as SDK_VERSION
from sentry_sdk.utils import get_git_revision

AWS_REGION_NAME = "us-east-1"
AWS_CREDENTIALS = {
Expand Down Expand Up @@ -226,7 +227,8 @@ def run_lambda_function(
# Making a unique function name depending on all the code that is run in it (function code plus SDK version)
# The name needs to be short so the generated event/envelope json blobs are small enough to be output
# in the log result of the Lambda function.
function_hash = hashlib.shake_256((code + SDK_VERSION).encode("utf-8")).hexdigest(5)
rev = get_git_revision() or SDK_VERSION
function_hash = hashlib.shake_256((code + rev).encode("utf-8")).hexdigest(6)
fn_name = "test_{}".format(function_hash)
full_fn_name = "{}_{}".format(
fn_name, runtime.replace(".", "").replace("python", "py")
Expand Down

0 comments on commit f9ffe96

Please sign in to comment.