Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
sentry-python/tests/integrations/aws_lambda/client.py at master · tinylambda/sentry-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
tinylambda
/
sentry-python
Public
forked from
getsentry/sentry-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
sentry-python
/
tests
/
integrations
/
aws_lambda
/
client.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
241 lines (201 loc) · 7.57 KB
master
Breadcrumbs
sentry-python
/
tests
/
integrations
/
aws_lambda
/
client.py
Copy path
Top
File metadata and controls
Code
Blame
241 lines (201 loc) · 7.57 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import
sys
import
os
import
shutil
import
tempfile
import
subprocess
import
boto3
import
uuid
import
base64
def
get_boto_client
():
return
boto3
.
client
(
"lambda"
,
aws_access_key_id
=
os
.
environ
[
"SENTRY_PYTHON_TEST_AWS_ACCESS_KEY_ID"
],
aws_secret_access_key
=
os
.
environ
[
"SENTRY_PYTHON_TEST_AWS_SECRET_ACCESS_KEY"
],
region_name
=
"us-east-1"
,
)
def
build_no_code_serverless_function_and_layer
(
client
,
tmpdir
,
fn_name
,
runtime
,
timeout
,
initial_handler
):
"""
Util function that auto instruments the no code implementation of the python
sdk by creating a layer containing the Python-sdk, and then creating a func
that uses that layer
"""
from
scripts
.
build_awslambda_layer
import
(
build_packaged_zip
,
)
build_packaged_zip
(
dest_abs_path
=
tmpdir
,
dest_zip_filename
=
"serverless-ball.zip"
)
with
open
(
os
.
path
.
join
(
tmpdir
,
"serverless-ball.zip"
),
"rb"
)
as
serverless_zip
:
response
=
client
.
publish_layer_version
(
LayerName
=
"python-serverless-sdk-test"
,
Description
=
"Created as part of testsuite for getsentry/sentry-python"
,
Content
=
{
"ZipFile"
:
serverless_zip
.
read
()},
)
with
open
(
os
.
path
.
join
(
tmpdir
,
"ball.zip"
),
"rb"
)
as
zip
:
client
.
create_function
(
FunctionName
=
fn_name
,
Runtime
=
runtime
,
Timeout
=
timeout
,
Environment
=
{
"Variables"
: {
"SENTRY_INITIAL_HANDLER"
:
initial_handler
,
"SENTRY_DSN"
:
"https://123abc@example.com/123"
,
"SENTRY_TRACES_SAMPLE_RATE"
:
"1.0"
,
}
},
Role
=
os
.
environ
[
"SENTRY_PYTHON_TEST_AWS_IAM_ROLE"
],
Handler
=
"sentry_sdk.integrations.init_serverless_sdk.sentry_lambda_handler"
,
Layers
=
[
response
[
"LayerVersionArn"
]],
Code
=
{
"ZipFile"
:
zip
.
read
()},
Description
=
"Created as part of testsuite for getsentry/sentry-python"
,
)
def
run_lambda_function
(
client
,
runtime
,
code
,
payload
,
add_finalizer
,
syntax_check
=
True
,
timeout
=
30
,
layer
=
None
,
initial_handler
=
None
,
subprocess_kwargs
=
(),
):
subprocess_kwargs
=
dict
(
subprocess_kwargs
)
with
tempfile
.
TemporaryDirectory
()
as
tmpdir
:
if
initial_handler
:
# If Initial handler value is provided i.e. it is not the default
# `test_lambda.test_handler`, then create another dir level so that our path is
# test_dir.test_lambda.test_handler
test_dir_path
=
os
.
path
.
join
(
tmpdir
,
"test_dir"
)
python_init_file
=
os
.
path
.
join
(
test_dir_path
,
"__init__.py"
)
os
.
makedirs
(
test_dir_path
)
with
open
(
python_init_file
,
"w"
):
# Create __init__ file to make it a python package
pass
test_lambda_py
=
os
.
path
.
join
(
tmpdir
,
"test_dir"
,
"test_lambda.py"
)
else
:
test_lambda_py
=
os
.
path
.
join
(
tmpdir
,
"test_lambda.py"
)
with
open
(
test_lambda_py
,
"w"
)
as
f
:
f
.
write
(
code
)
if
syntax_check
:
# Check file for valid syntax first, and that the integration does not
# crash when not running in Lambda (but rather a local deployment tool
# such as chalice's)
subprocess
.
check_call
([
sys
.
executable
,
test_lambda_py
])
fn_name
=
"test_function_{}"
.
format
(
uuid
.
uuid4
())
if
layer
is
None
:
setup_cfg
=
os
.
path
.
join
(
tmpdir
,
"setup.cfg"
)
with
open
(
setup_cfg
,
"w"
)
as
f
:
f
.
write
(
"[install]
\n
prefix="
)
subprocess
.
check_call
(
[
sys
.
executable
,
"setup.py"
,
"sdist"
,
"-d"
,
os
.
path
.
join
(
tmpdir
,
".."
)],
**
subprocess_kwargs
)
subprocess
.
check_call
(
"pip install mock==3.0.0 funcsigs -t ."
,
cwd
=
tmpdir
,
shell
=
True
,
**
subprocess_kwargs
)
# https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html
subprocess
.
check_call
(
"pip install ../*.tar.gz -t ."
,
cwd
=
tmpdir
,
shell
=
True
,
**
subprocess_kwargs
)
shutil
.
make_archive
(
os
.
path
.
join
(
tmpdir
,
"ball"
),
"zip"
,
tmpdir
)
with
open
(
os
.
path
.
join
(
tmpdir
,
"ball.zip"
),
"rb"
)
as
zip
:
client
.
create_function
(
FunctionName
=
fn_name
,
Runtime
=
runtime
,
Timeout
=
timeout
,
Role
=
os
.
environ
[
"SENTRY_PYTHON_TEST_AWS_IAM_ROLE"
],
Handler
=
"test_lambda.test_handler"
,
Code
=
{
"ZipFile"
:
zip
.
read
()},
Description
=
"Created as part of testsuite for getsentry/sentry-python"
,
)
else
:
subprocess
.
run
(
[
"zip"
,
"-q"
,
"-x"
,
"**/__pycache__/*"
,
"-r"
,
"ball.zip"
,
"./"
],
cwd
=
tmpdir
,
check
=
True
,
)
# Default initial handler
if
not
initial_handler
:
initial_handler
=
"test_lambda.test_handler"
build_no_code_serverless_function_and_layer
(
client
,
tmpdir
,
fn_name
,
runtime
,
timeout
,
initial_handler
)
@
add_finalizer
def
clean_up
():
client
.
delete_function
(
FunctionName
=
fn_name
)
# this closes the web socket so we don't get a
# ResourceWarning: unclosed <ssl.SSLSocket ... >
# warning on every test
# based on https://github.com/boto/botocore/pull/1810
# (if that's ever merged, this can just become client.close())
session
=
client
.
_endpoint
.
http_session
managers
=
[
session
.
_manager
]
+
list
(
session
.
_proxy_managers
.
values
())
for
manager
in
managers
:
manager
.
clear
()
response
=
client
.
invoke
(
FunctionName
=
fn_name
,
InvocationType
=
"RequestResponse"
,
LogType
=
"Tail"
,
Payload
=
payload
,
)
assert
200
<=
response
[
"StatusCode"
]
<
300
,
response
return
response
_REPL_CODE
=
"""
import os
def test_handler(event, context):
line = {line!r}
if line.startswith(">>> "):
exec(line[4:])
elif line.startswith("$ "):
os.system(line[2:])
else:
print("Start a line with $ or >>>")
return b""
"""
try
:
import
click
except
ImportError
:
pass
else
:
@
click
.
command
()
@
click
.
option
(
"--runtime"
,
required
=
True
,
help
=
"name of the runtime to use, eg python3.8"
)
@
click
.
option
(
"--verbose"
,
is_flag
=
True
,
default
=
False
)
def
repl
(
runtime
,
verbose
):
"""
Launch a "REPL" against AWS Lambda to inspect their runtime.
"""
cleanup
=
[]
client
=
get_boto_client
()
print
(
"Start a line with `$ ` to run shell commands, or `>>> ` to run Python"
)
while
True
:
line
=
input
()
response
=
run_lambda_function
(
client
,
runtime
,
_REPL_CODE
.
format
(
line
=
line
),
b""
,
cleanup
.
append
,
subprocess_kwargs
=
{
"stdout"
:
subprocess
.
DEVNULL
,
"stderr"
:
subprocess
.
DEVNULL
,
}
if
not
verbose
else
{},
)
for
line
in
base64
.
b64decode
(
response
[
"LogResult"
]).
splitlines
():
print
(
line
.
decode
(
"utf8"
))
for
f
in
cleanup
:
f
()
cleanup
=
[]
if
__name__
==
"__main__"
:
repl
()
You can’t perform that action at this time.