Skip to content

openai: investigate metrics code relying on span recording #53

@xrmx

Description

@xrmx

It looks like our metrics code is relying on tracing being enabled, this is not the case if we run with OTEL_SDK_DISABLED=true.

Here's some tracebacks from running chatbot-rag-app with the sdk disabled:

api-frontend  | Traceback (most recent call last):
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/langchain_openai/chat_models/base.py", line 737, in _stream
api-frontend  |     for chunk in response:
api-frontend  |                  ^^^^^^^^^
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/openai/wrappers.py", line 122, in __next__
api-frontend  |     self.end(exc)
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/openai/wrappers.py", line 78, in end
api-frontend  |     _record_operation_duration_metric(self.operation_duration_metric, self.span, self.start_time)
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/openai/helpers.py", line 212, in _record_operation_duration_metric
api-frontend  |     operation_duration_metric_attrs = _get_attributes_if_set(
api-frontend  |                                       ^^^^^^^^^^^^^^^^^^^^^^^
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/openai/helpers.py", line 189, in _get_attributes_if_set
api-frontend  |     attributes = span.attributes
api-frontend  |                  ^^^^^^^^^^^^^^^
api-frontend  | AttributeError: 'NonRecordingSpan' object has no attribute 'attributes'. Did you mean: 'set_attributes'?
api-frontend  | 
api-frontend  | During handling of the above exception, another exception occurred:
api-frontend  | 
api-frontend  | Traceback (most recent call last):
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/werkzeug/serving.py", line 370, in run_wsgi
api-frontend  |     execute(self.server.app)
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/werkzeug/serving.py", line 333, in execute
api-frontend  |     for data in application_iter:
api-frontend  |                 ^^^^^^^^^^^^^^^^^
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/werkzeug/wsgi.py", line 256, in __next__
api-frontend  |     return self._next()
api-frontend  |            ^^^^^^^^^^^^
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/werkzeug/wrappers/response.py", line 32, in _iter_encoded
api-frontend  |     for item in iterable:
api-frontend  |                 ^^^^^^^^^
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/flask/helpers.py", line 125, in generator
api-frontend  |     yield from gen
api-frontend  |   File "/app/api/chat.py", line 78, in ask_question
api-frontend  |     for chunk in llm.stream(qa_prompt):
api-frontend  |                  ^^^^^^^^^^^^^^^^^^^^^^
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 415, in stream
api-frontend  |     for chunk in self._stream(messages, stop=stop, **kwargs):
api-frontend  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/langchain_openai/chat_models/base.py", line 735, in _stream
api-frontend  |     with context_manager as response:
api-frontend  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/openai/wrappers.py", line 108, in __exit__
api-frontend  |     self.end(exc_value)
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/openai/wrappers.py", line 70, in end
api-frontend  |     _record_operation_duration_metric(self.operation_duration_metric, self.span, self.start_time)
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/openai/helpers.py", line 212, in _record_operation_duration_metric
api-frontend  |     operation_duration_metric_attrs = _get_attributes_if_set(
api-frontend  |                                       ^^^^^^^^^^^^^^^^^^^^^^^
api-frontend  |   File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/openai/helpers.py", line 189, in _get_attributes_if_set
api-frontend  |     attributes = span.attributes
api-frontend  |                  ^^^^^^^^^^^^^^^
api-frontend  | AttributeError: 'NonRecordingSpan' object has no attribute 'attributes'. Did you mean: 'set_attributes'?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions