GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — sentry-python/scripts/init_serverless_sdk.py at master · getsentry/sentry-python

Expand file tree

/

init_serverless_sdk.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
64 lines (51 loc) · 2.43 KB
/

init_serverless_sdk.py

Copy path

File metadata and controls

64 lines (51 loc) · 2.43 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
For manual instrumentation,
The Handler function string of an aws lambda function should be added as an
environment variable with a key of 'SENTRY_INITIAL_HANDLER' along with the 'DSN'
Then the Handler function sstring should be replaced with
'sentry_sdk.integrations.init_serverless_sdk.sentry_lambda_handler'
"""
import importlib
import os
from typing import TYPE_CHECKING
import sentry_sdk
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
if TYPE_CHECKING:
from typing import Any
# Configure Sentry SDK
sentry_sdk.init(
dsn=os.environ["SENTRY_DSN"],
integrations=[AwsLambdaIntegration(timeout_warning=True)],
traces_sample_rate=float(os.environ["SENTRY_TRACES_SAMPLE_RATE"]),
)
class AWSLambdaModuleLoader:
def __init__(self, sentry_initial_handler):
try:
module_path, self.handler_name = sentry_initial_handler.rsplit(".", 1)
except ValueError:
raise ValueError("Incorrect AWS Handler path (Not a path)")
self.extract_and_load_lambda_function_module(module_path)
def extract_and_load_lambda_function_module(self, module_path):
"""
Method that extracts and loads lambda function module from module_path
"""
# the handler used to be loaded from a file path, so keep accepting
# `./dir/file` and absolute `<task root>/dir/file` by making them relative
# to the task root, which is on `sys.path`.
task_root = os.environ.get("LAMBDA_TASK_ROOT", os.getcwd())
module_path = os.path.relpath(os.path.join(task_root, module_path), task_root)
# like the Lambda runtime: `dir/file` is the module `dir.file`, which `sys.modules`
# keeps, so its code only runs on the first invocation.
# https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/fa2462d93d11da632aad8f8bfb07a3e060438150/awslambdaric/bootstrap.py#L53
self.lambda_function_module = importlib.import_module(
module_path.replace("/", ".")
)
def get_lambda_handler(self):
return getattr(self.lambda_function_module, self.handler_name)
def sentry_lambda_handler(event: "Any", context: "Any") -> "Any":
"""
Handler function that invokes a lambda handler which path is defined in
environment variables as "SENTRY_INITIAL_HANDLER"
"""
module_loader = AWSLambdaModuleLoader(os.environ["SENTRY_INITIAL_HANDLER"])
return module_loader.get_lambda_handler()(event, context)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.