-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
104 lines (76 loc) · 2.97 KB
/
server.py
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
import base64
import os
import gzip
import time
import random
from cryptography.fernet import Fernet
from dotenv import load_dotenv
from flask import Flask, request, jsonify
from opentelemetry import trace
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.sdk.resources import Resource
### Environment variables
load_dotenv()
PORT = int( os.getenv("PORT") )
FORMAT = os.getenv("FORMAT")
### Encryption data
key = Fernet.generate_key()
cipherSuite = Fernet(key)
#### Set up Jaeger exporter and tracing
jaeger_exporter = JaegerExporter(
agent_host_name="localhost",
agent_port=6831,
)
tracer_provider = TracerProvider(resource=Resource.create({"service.name": "server"}))
trace.set_tracer_provider(tracer_provider)
tracer_provider.add_span_processor(SimpleSpanProcessor(jaeger_exporter))
tracer = trace.get_tracer(__name__)
### Flask App
app = Flask(__name__)
FlaskInstrumentor().instrument_app(app)
### Methods
def generate_bug(errorRate): # Creating deliberate delay on the client side
if random.random() < errorRate:
time.sleep(5) # introduce delay of Five seconds
def createFile(folder, name, content):
with tracer.start_as_current_span("Create file") as span:
if random.random() < 0.5: # Predicate P
generate_bug(0.3) # Introduce deliberate delay
with open(os.path.join(folder, name), "wb") as file:
file.write(content)
span.add_event("File creation", {"name": name})
### Endpoints
@app.route("/", methods=["GET"])
def serveKey():
with tracer.start_as_current_span("Serve key") as span:
span.set_attribute("Method", request.method)
span.set_attribute("Client", request.remote_addr)
span.add_event("Sent key", {"key": key.decode(FORMAT)})
return jsonify({
"key": key.decode(FORMAT)
})
@app.route("/", methods=["POST"])
def takeFiles():
with tracer.start_as_current_span("TakeFiles") as span:
req = request.get_json()
name = req.get("name", "DEFAULT")
files = req.get("files", {})
span.set_attribute("Method", request.method)
span.set_attribute("Client", request.remote_addr)
span.set_attribute("File Count", len(files.keys()))
span.set_attribute("Client name", name)
os.makedirs(name, exist_ok=True)
for fileName, fileData in files.items():
fileContent = base64.b64decode(fileData.encode(FORMAT))
fileContent = gzip.decompress(fileContent)
fileContent = cipherSuite.decrypt(fileContent)
createFile(name, fileName, fileContent)
return jsonify(message="success")
### Driver
if __name__ == "__main__":
with tracer.start_as_current_span("main") as span:
span.set_attribute("Type", "server")
app.run(host="0.0.0.0", port=PORT)