-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
110 lines (91 loc) · 2.95 KB
/
Copy pathapp.py
File metadata and controls
110 lines (91 loc) · 2.95 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
import json
import math
import os
import random
import pika
from flask import Flask, jsonify
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
from opentelemetry.propagate import inject
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from sqlalchemy import create_engine, text
# OpenTelemetry
resource = Resource.create()
provider = TracerProvider(resource=resource)
otlp_exporter = OTLPSpanExporter()
processor = BatchSpanProcessor(otlp_exporter)
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)
tracer = trace.get_tracer(__name__)
# Database
DB_URL = os.environ["DATABASE_URL"]
engine = create_engine(DB_URL)
SQLAlchemyInstrumentor().instrument(engine=engine)
Psycopg2Instrumentor().instrument()
# RabbitMQ
RABBITMQ_HOST = "rabbitmq"
QUEUE_NAME = "my_queue"
connection = pika.BlockingConnection(
pika.ConnectionParameters(RABBITMQ_HOST, heartbeat=3600)
)
channel = connection.channel()
channel.queue_declare(queue=QUEUE_NAME, durable=True)
# Flask
app = Flask(__name__)
FlaskInstrumentor().instrument_app(app)
@app.route('/db-check')
def db_check():
with tracer.start_as_current_span("db-query-execution"):
with engine.connect() as connection:
sleep_time = random.random() / 50.0
result = connection.execute(
text("SELECT pg_sleep(:t);"), {"t": sleep_time}
).scalar()
return jsonify({
"status": "success",
"result": result,
})
def is_prime(n: int):
if n < 2:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
@app.route('/cpu-heavy')
def cpu_heavy():
limit = 20000
prime_count = 0
with tracer.start_as_current_span("cpu-intensive-task") as span:
for number in range(2, limit):
if is_prime(number):
prime_count += 1
span.set_attribute("prime_count", prime_count)
return jsonify({
"limit": limit,
"prime_count": prime_count,
})
@app.route('/send-message')
def send_message():
with tracer.start_as_current_span(
"publish_message", kind=trace.SpanKind.PRODUCER
):
headers = {}
inject(headers)
message = {"sleep": random.random() / 50.0}
properties = pika.BasicProperties(
headers=headers,
delivery_mode=pika.DeliveryMode.Persistent,
)
channel.basic_publish(
exchange="",
routing_key=QUEUE_NAME,
body=json.dumps(message),
properties=properties,
)
return jsonify({"status": "OK"})