You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I use opentelemetry-instrumentation-flask to get flask sse span, the server span end before the sse response end, that makes flask sse server span time is incorrect. Did anyone solve this problem ?
from flask import Flask, Response
import time
import os
import flask
from opentelemetry.instrumentation.flask import FlaskInstrumentor, _ENVIRON_SPAN_KEY
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, BatchSpanProcessor
from werkzeug.local import LocalProxy
from opentelemetry.trace import get_current_span
app = Flask(__name__)
# 初始化追踪提供者和导出器
provider = TracerProvider()
console_exporter = ConsoleSpanExporter()
provider.add_span_processor(BatchSpanProcessor(console_exporter))
trace.set_tracer_provider(provider)
# 初始化 FlaskInstrumentor 并应用到 Flask 应用
FlaskInstrumentor().instrument_app(app)
# 定义一个生成器,用于流数据
def event_stream():
for i in range(1, 5):
# 每次发送消息(遵循 SSE 数据格式)
yield f"data: Server Time {time.ctime()}\n\n"
time.sleep(1) # 每秒发送一次
@app.route('/stream')
def stream():
# 返回流式响应
response = Response(event_stream(), content_type='text/event-stream')
response.headers["test"] = "test"
return response
if __name__ == '__main__':
app.run(debug=True)
Steps to Reproduce
code
Expected Result
code
Actual Result
code
Additional context
code
Would you like to implement a fix?
None
The text was updated successfully, but these errors were encountered:
The issue seems to be related to how opentelemetry-instrumentation-flask handles the lifecycle of spans for streaming responses.
The Response object for SSE streams is returned immediately, but the actual streaming continues asynchronously, which might not be accounted for in the span lifecycle
Describe your environment
OS: (e.g, Ubuntu)
Python version: (e.g., Python 3.8.10)
Package version: (e.g., 0.46.0)
What happened?
When I use opentelemetry-instrumentation-flask to get flask sse span, the server span end before the sse response end, that makes flask sse server span time is incorrect. Did anyone solve this problem ?
Steps to Reproduce
code
Expected Result
code
Actual Result
code
Additional context
code
Would you like to implement a fix?
None
The text was updated successfully, but these errors were encountered: