-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sentry.py
More file actions
executable file
·156 lines (127 loc) · 4.44 KB
/
test_sentry.py
File metadata and controls
executable file
·156 lines (127 loc) · 4.44 KB
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
#!/usr/bin/env python3
"""
Sentry Test Script
Sends errors, traces, profiling data, and logs to Sentry every second.
"""
import os
import sys
import time
import random
import logging
import sentry_sdk
from sentry_sdk import start_transaction
# Configure Python's built-in logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Get Sentry DSN from environment variable
SENTRY_DSN = os.environ.get("SENTRY_DSN")
if not SENTRY_DSN:
print("Error: SENTRY_DSN environment variable is not set!")
print("Please set SENTRY_DSN in your environment or docker-compose.yaml")
sys.exit(1)
# Initialize Sentry SDK
sentry_sdk.init(
dsn=SENTRY_DSN,
# Add data like request headers and IP for users
send_default_pii=True,
# Enable sending logs to Sentry
enable_logs=True,
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# Set profile_session_sample_rate to 1.0 to profile 100%
# of profile sessions.
profile_session_sample_rate=1.0,
)
def slow_function():
"""Simulates a slow operation for profiling"""
time.sleep(0.1)
return "slow_done"
def fast_function():
"""Simulates a fast operation for profiling"""
time.sleep(0.05)
return "fast_done"
def send_error_sample():
"""Send an error to Sentry"""
try:
# Intentionally cause different types of errors
error_type = random.choice([
lambda: 1 / 0, # ZeroDivisionError
lambda: int("not_a_number"), # ValueError
lambda: [][5], # IndexError
lambda: {"key": "value"}["missing_key"], # KeyError
])
error_type()
except Exception as e:
sentry_sdk.capture_exception(e)
print(f"✓ Error sent: {type(e).__name__}")
def send_trace_sample():
"""Send a transaction trace to Sentry"""
with start_transaction(op="test", name="test_transaction") as transaction:
# Create spans within the transaction
with transaction.start_child(op="db", description="Database query"):
time.sleep(0.05)
with transaction.start_child(op="http", description="API call"):
time.sleep(0.03)
with transaction.start_child(op="process", description="Data processing"):
time.sleep(0.02)
print("✓ Trace sent: test_transaction")
def send_profiling_sample():
"""Send profiling data to Sentry"""
# Start profiler
sentry_sdk.profiler.start_profiler()
# Execute functions to profile
for _ in range(3):
slow_function()
fast_function()
# Stop profiler
sentry_sdk.profiler.stop_profiler()
print("✓ Profiling data sent")
def send_logs_sample(iteration):
"""Send logs to Sentry"""
# Using Sentry's logger API - send different log levels
log_messages = [
("info", f"Info log sample {iteration}"),
("warning", f"Warning log sample {iteration}"),
("error", f"Error log sample {iteration}"),
]
message_type, message = random.choice(log_messages)
if message_type == "info":
sentry_sdk.logger.info(message)
logger.info(message) # Also send via Python logging
elif message_type == "warning":
sentry_sdk.logger.warning(message)
logger.warning(message)
elif message_type == "error":
sentry_sdk.logger.error(message)
logger.error(message)
print(f"✓ Log sent: [{message_type.upper()}] {message}")
def main():
"""Main loop that sends telemetry data every second"""
print("Starting Sentry test script...")
print(f"Sentry DSN: {SENTRY_DSN[:50]}...")
print("Sending errors, traces, profiling data, and logs every second...")
print("Press Ctrl+C to stop\n")
iteration = 0
try:
while True:
iteration += 1
print(f"\n--- Iteration {iteration} ---")
# Send error sample
send_error_sample()
# Send trace sample
send_trace_sample()
# Send profiling sample
send_profiling_sample()
# Send logs sample
send_logs_sample(iteration)
print(f"Waiting 1 second...\n")
time.sleep(1)
except KeyboardInterrupt:
print("\n\nStopping test script...")
print(f"Total iterations: {iteration}")
# Flush any remaining events
sentry_sdk.flush(timeout=2.0)
print("All events flushed to Sentry. Goodbye!")
if __name__ == "__main__":
main()