forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracing.py
More file actions
72 lines (54 loc) · 1.59 KB
/
tracing.py
File metadata and controls
72 lines (54 loc) · 1.59 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
import json
import flask
import os
import redis
import rq
import sentry_sdk
import time
import urllib3
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.rq import RqIntegration
app = flask.Flask(__name__)
redis_conn = redis.Redis()
http = urllib3.PoolManager()
queue = rq.Queue(connection=redis_conn)
def write_event(event):
with open("events", "a") as f:
f.write(json.dumps(event))
f.write("\n")
sentry_sdk.init(
integrations=[FlaskIntegration(), RqIntegration()],
traces_sample_rate=1.0,
debug=True,
transport=write_event,
)
def decode_base64(encoded, redis_key):
time.sleep(1)
r = http.request("GET", "http://httpbin.org/base64/{}".format(encoded))
redis_conn.set(redis_key, r.data)
@app.route("/")
def index():
return flask.render_template(
"index.html",
sentry_dsn=os.environ["SENTRY_DSN"],
traceparent=dict(sentry_sdk.Hub.current.iter_trace_propagation_headers()),
)
@app.route("/compute/<input>")
def compute(input):
redis_key = "sentry-python-tracing-example-result:{}".format(input)
redis_conn.delete(redis_key)
queue.enqueue(decode_base64, encoded=input, redis_key=redis_key)
return redis_key
@app.route("/wait/<redis_key>")
def wait(redis_key):
result = redis_conn.get(redis_key)
if result is None:
return "NONE"
else:
redis_conn.delete(redis_key)
return "RESULT: {}".format(result)
@app.cli.command("worker")
def run_worker():
print("WORKING")
worker = rq.Worker([queue], connection=queue.connection)
worker.work()