open-telemetry / open-telemetry/opentelemetry-python-contrib
[Bug] for sse Server-Sent Events, opentelemetry-instrumentation-flask server span time is less than real response end
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 1.1k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 16
Description
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 ?
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the provided Flask SSE example, focusing on FlaskInstrumentor().instrument_app(app) and the /stream entry point. Trace when the server span ends relative to the event_stream response, and consider the issue complete when the span duration includes the full streamed response.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- flask, python
- Domain
- backend, observability
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100