Update _responses_instrumentor.py#47371
Open
howieleung wants to merge 1 commit into
Open
Conversation
This script fails to run, and this PR is the fix.
import os
import logging
# 1) MUST be set before Azure SDK imports/client construction
os.environ.setdefault("AZURE_EXPERIMENTAL_ENABLE_GENAI_TRACING", "true")
# Tracing setup (required order)
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.sampling import ALWAYS_OFF
from azure.core.settings import settings
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
from azure.ai.projects.telemetry import AIProjectInstrumentor
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
def main() -> None:
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("azure").setLevel(logging.DEBUG)
logging.getLogger("openai").setLevel(logging.DEBUG)
endpoint = os.environ.get("FOUNDRY_PROJECT_ENDPOINT")
model = os.environ.get("FOUNDRY_MODEL_NAME")
if not endpoint:
raise RuntimeError("FOUNDRY_PROJECT_ENDPOINT is not set")
if not model:
raise RuntimeError("FOUNDRY_MODEL_NAME is not set")
# 2) Global tracer provider with ALWAYS_OFF -> NonRecordingSpan
trace.set_tracer_provider(TracerProvider(sampler=ALWAYS_OFF))
# 3) Use OpenTelemetry span implementation for azure-core tracing
settings.tracing_implementation = OpenTelemetrySpan
# 4) Explicitly activate AI Projects instrumentor exactly once
AIProjectInstrumentor().instrument()
credential = DefaultAzureCredential()
client = AIProjectClient(endpoint=endpoint, credential=credential)
# 5) Responses API call through OpenAI client
openai_client = client.get_openai_client()
response = openai_client.responses.create(
model=model,
input=[{"role": "user", "content": "hello from repro for #46544"}],
)
output_text = getattr(response, "output_text", None) or ""
print(f"response_id={response.id}")
print(f"output_text={output_text}")
if __name__ == "__main__":
main()
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Responses API telemetry instrumentor to avoid touching span internals when OpenTelemetry is using a non-recording span, preventing crashes during attribute-based message accumulation (e.g., when ALWAYS_OFF / NonRecordingSpan is in effect).
Changes:
- Add an
is_recording()guard in_append_to_message_attributebefore readingspan.span_instance.attributes. - Early-return when the underlying OpenTelemetry span is not recording to prevent
AttributeErroronNonRecordingSpan.
Comment on lines
+561
to
+563
| if not span.span_instance.is_recording(): | ||
| return | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This script fails to run, and this PR is the fix.
import os
import logging
1) MUST be set before Azure SDK imports/client construction os.environ.setdefault("AZURE_EXPERIMENTAL_ENABLE_GENAI_TRACING", "true")
Tracing setup (required order)
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.sampling import ALWAYS_OFF
from azure.core.settings import settings
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan from azure.ai.projects.telemetry import AIProjectInstrumentor from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
def main() -> None:
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("azure").setLevel(logging.DEBUG)
logging.getLogger("openai").setLevel(logging.DEBUG)
if name == "main":
main()
Description
Please add an informative description that covers that changes made by the pull request and link all relevant issues.
If an SDK is being regenerated based on a new API spec, a link to the pull request containing these API spec changes should be included above.
All SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines