Added more spans

This commit is contained in:
Pünkösd Marcell 2021-08-10 14:40:57 +02:00
parent 98234f0e8a
commit 00e9d02478

View File

@ -12,6 +12,8 @@ from db import db
from influxus import influx_db from influxus import influx_db
from models import SampleMetadata from models import SampleMetadata
from schemas import SampleSchema, SampleMetadataSchema from schemas import SampleSchema, SampleMetadataSchema
from requests_opentracing import SessionTracing
import opentracing
""" """
Flask Restful endpoints Flask Restful endpoints
@ -37,6 +39,7 @@ class SampleResource(Resource):
Post request send to the endpoint Post request send to the endpoint
:return: :return:
""" """
with opentracing.tracer.start_active_span('parseAndValidate'):
if 'file' not in request.files: if 'file' not in request.files:
return abort(400, "no file found") return abort(400, "no file found")
else: else:
@ -56,6 +59,7 @@ class SampleResource(Resource):
current_app.logger.exception(e) current_app.logger.exception(e)
return abort(417, 'Input JSON schema invalid') return abort(417, 'Input JSON schema invalid')
with opentracing.tracer.start_active_span('generateTag'):
xeger = Xeger(limit=30) xeger = Xeger(limit=30)
while True: while True:
generated_tag = xeger.xeger(r'^[a-zA-Z]+[0-9a-zA-Z_]*$')[:32] generated_tag = xeger.xeger(r'^[a-zA-Z]+[0-9a-zA-Z_]*$')[:32]
@ -65,6 +69,8 @@ class SampleResource(Resource):
# Handle mega-autismo-cliento # Handle mega-autismo-cliento
soundfile_content_length = soundfile.content_length soundfile_content_length = soundfile.content_length
if soundfile_content_length <= 0: # BRUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUH if soundfile_content_length <= 0: # BRUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUH
with opentracing.tracer.start_active_span(
'calculateContentLength'): # In an ideal scenario this span is missing
current_app.logger.debug( current_app.logger.debug(
"The uploader did not provide content-length for the sound file... Calculating manually..." "The uploader did not provide content-length for the sound file... Calculating manually..."
) )
@ -78,6 +84,7 @@ class SampleResource(Resource):
# It's insane, that you can not set this field in curl # It's insane, that you can not set this field in curl
with opentracing.tracer.start_active_span('sqlalchemy.create'):
record = SampleMetadata( record = SampleMetadata(
device_id=desc['device_id'], device_id=desc['device_id'],
device_date=desc['date'], device_date=desc['date'],
@ -85,6 +92,7 @@ class SampleResource(Resource):
) )
db.session.add(record) db.session.add(record)
with opentracing.tracer.start_active_span('uploadToStorageService'):
files = { files = {
'description': (None, json.dumps({'tag': generated_tag}), 'application/json'), 'description': (None, json.dumps({'tag': generated_tag}), 'application/json'),
'soundFile': ( 'soundFile': (
@ -94,7 +102,7 @@ class SampleResource(Resource):
{'Content-Length': soundfile_content_length})} {'Content-Length': soundfile_content_length})}
upload_started = time.time() upload_started = time.time()
r = requests.post( r = SessionTracing(propagate=True).post(
f"http://{current_app.config.get('STORAGE_HOSTNAME')}/object", f"http://{current_app.config.get('STORAGE_HOSTNAME')}/object",
files=files files=files
) )
@ -104,11 +112,14 @@ class SampleResource(Resource):
current_app.logger.warning(f"Uploading to storage-service took {upload_time:5} sec") current_app.logger.warning(f"Uploading to storage-service took {upload_time:5} sec")
if r.status_code not in [200, 201]: if r.status_code not in [200, 201]:
return abort(500, f"Failed to upload sample to storage service. Upstream status: {r.status_code}: {r.text}") return abort(500,
f"Failed to upload sample to storage service. Upstream status: {r.status_code}: {r.text}")
with opentracing.tracer.start_active_span('sqlalchemy.commit'):
db.session.commit() db.session.commit()
# Announce only after the data is successfully committed # Announce only after the data is successfully committed
with opentracing.tracer.start_active_span('amqp.publish'):
try: try:
magic_amqp.publish({'tag': generated_tag}) magic_amqp.publish({'tag': generated_tag})
except Exception as e: except Exception as e:
@ -117,6 +128,7 @@ class SampleResource(Resource):
# metrics # metrics
if current_app.config['ENABLE_INFLUXDB']: if current_app.config['ENABLE_INFLUXDB']:
with opentracing.tracer.start_active_span('influxdb.write_points'):
influx_db.write_points( influx_db.write_points(
[ [
{ {
@ -139,7 +151,9 @@ class SampleResource(Resource):
Get all stored items Get all stored items
:return: :return:
""" """
with opentracing.tracer.start_active_span('sqlalchemy.select'):
samples = SampleMetadata.query.all() samples = SampleMetadata.query.all()
return self.samplemetadataschema.dump(list(samples)), 200 return self.samplemetadataschema.dump(list(samples)), 200
@ -156,5 +170,7 @@ class SampleParameterResource(Resource):
:param tag: :param tag:
:return: :return:
""" """
with opentracing.tracer.start_active_span('sqlalchemy.select', tags={"tag": tag}):
sample = SampleMetadata.query.filter_by(tag=tag).first_or_404() sample = SampleMetadata.query.filter_by(tag=tag).first_or_404()
return self.samplemetadataschema.dump(sample), 200 return self.samplemetadataschema.dump(sample), 200