from random import randint from flask import Flask, request import logging from opentelemetry import trace tracer = trace.get_tracer("diceroller.tracer") app = Flask(__name__) logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) @app.route("/rolldice") def roll_dice(): player = request.args.get('player', default = None, type = str) result = str(roll3()) if player: logger.warning("%s is rolling the dice: %s", player, result) else: logger.warning("Anonymous player is rolling the dice: %s", result) return result @tracer.start_as_current_span("roll") def roll(): return randint(1, 6) def roll2(): with tracer.start_as_current_span("roll") as rollspan: try: res = randint(1, 6) rollspan.set_status(trace.StatusCode.OK) rollspan.set_attribute("roll.value", res) rollspan.add_event("事件A") return res except Exception as ex: rollspan.set_status(trace.StatusCode.ERROR) rollspan.record_exception(ex) raise ex @tracer.start_as_current_span("roll") def roll3(): # 其它业务代码... with tracer.start_as_current_span("randint") as randspan: try: res = randint(1, 6) randspan.set_status(trace.StatusCode.OK) randspan.set_attribute("roll.value", res) randspan.add_event("事件A") return res except Exception as ex: randspan.set_status(trace.StatusCode.ERROR) randspan.record_exception(ex) raise ex # 其它业务代码...