Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure AI Client: agent tracing prototype #38016

Draft
wants to merge 5 commits into
base: dargilco/azure-ai-client
Choose a base branch
from

Conversation

lmolkova
Copy link
Member

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 swagger spec, a link to the pull request containing these swagger spec changes has been included above.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

@github-actions github-actions bot added the AI label Oct 21, 2024
"""
...

def set_error(self, error_code: Optional[str] = None, exc: Optional[Exception] = None) -> None:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, undo - the __exit__(exc info) is enough for now

"""
Add attribute (key value pair) to the current span.

:param key: The key of the key value pair
:type key: str
:param value: The value of the key value pair
:type value: Union[str, int]
:type value: Union[str, int, float]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: need to add string[] for inference tracing


parsed_url = urllib.parse.urlparse(http_request.url)
authority = f"{parsed_url.hostname}:{parsed_url.port}"
return f"{http_request.method} {authority}"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: this is technically a breaking change, but also an important bugfix

also, can we cache parsed url and not re-parse it over and over again?

for token in self._context_tokens:
context.detach(token)
self._context_tokens.remove(token)

if self.previous_context:
context.attach(self.previous_context)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without this the suppression flag never disappears after making a call in that context.

@@ -228,7 +230,8 @@ def kind(self, value: SpanKind) -> None:
def __enter__(self) -> "OpenTelemetrySpan":
# Start the span.
if not isinstance(self.span_instance, NonRecordingSpan):
if self.kind == SpanKind.INTERNAL:
if self.kind == SpanKind.INTERNAL or self.kind == SpanKind.CLIENT:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gen ai spans are client, need to suppress stuff under them too


thread = ai_client.agents.create_thread()
print(f"Created thread, thread ID: {thread.id}")
with tracer.start_as_current_span(scenario):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: add tracing-specific examples - this is temporary, just to see how tracing works in different scenarios


_headers = case_insensitive_dict(kwargs.pop("headers", {}) or {})
_params = kwargs.pop("params", {}) or {}
with start_create_thread_span(self._config.project_name, messages=messages) as span:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is where monkey-patching comes really handy - helps to avoid customizing autogenerated code

Copy link
Member Author

@lmolkova lmolkova Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is needed to avoid circular import:

(operations/_patch.py -> models -> tracing helpers -> models)

maybe there is a better way and then the contents could be moved to _agent_instr.py. Or all the problem goes away with monkey-patching

self.ended = True
set_end_run(self.span, self.last_run)

if self.last_run.last_error:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if it's updated with local errors, need to test failure cases


from azure.ai.client._instrumentation._utils import * # pylint: disable=unused-wildcard-import
from azure.ai.client.models import _models
from azure.ai.client.models._enums import MessageRole, RunStepStatus
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to import private modules?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants