-
Notifications
You must be signed in to change notification settings - Fork 24
feat(qwen-agent): add instrumentation for Qwen-Agent #154
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
Open
sipercai
wants to merge
2
commits into
alibaba:main
Choose a base branch
from
sipercai:liuyu/qwen-agent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
13 changes: 13 additions & 0 deletions
13
instrumentation-loongsuite/loongsuite-instrumentation-qwen-agent/CHANGELOG.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Changelog | ||
|
|
||
| ## Unreleased | ||
|
|
||
| ### Added | ||
|
|
||
| - Initial release of `loongsuite-instrumentation-qwen-agent` | ||
| - Instrumentation for `Agent.run()` (invoke_agent spans; `run_nonstream()` is covered via its internal `run()` call — no duplicate span) | ||
| - Instrumentation for `BaseChatModel.chat()` (LLM spans) | ||
| - Instrumentation for `Agent._call_tool()` (execute_tool spans) | ||
| - Support for streaming and non-streaming LLM responses | ||
| - Message conversion from qwen-agent types to GenAI semantic conventions | ||
| - Provider detection for DashScope, OpenAI, and Azure backends | ||
109 changes: 109 additions & 0 deletions
109
instrumentation-loongsuite/loongsuite-instrumentation-qwen-agent/README.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # OpenTelemetry Qwen-Agent Instrumentation | ||
|
|
||
| OpenTelemetry instrumentation for the [Qwen-Agent](https://github.com/QwenLM/Qwen-Agent) framework. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pip install opentelemetry-distro opentelemetry-exporter-otlp | ||
| opentelemetry-bootstrap -a install | ||
|
|
||
| pip install "qwen-agent >= 0.0.20" | ||
|
|
||
| # Install this instrumentation (from the LoongSuite repo) | ||
| pip install ./instrumentation-loongsuite/loongsuite-instrumentation-qwen-agent | ||
|
|
||
| # Required: GenAI utilities used by the instrumentation | ||
| pip install ./util/opentelemetry-util-genai | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be done before install |
||
| ``` | ||
|
|
||
| Published package name: | ||
|
|
||
| ```bash | ||
| pip install loongsuite-instrumentation-qwen-agent | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Auto-instrumentation | ||
|
|
||
| With `loongsuite-instrumentation-qwen-agent` installed, the `opentelemetry_instrumentor` entry point `qwen_agent` is registered for use with the OpenTelemetry distro. | ||
|
|
||
| ```bash | ||
| opentelemetry-instrument \ | ||
| --traces_exporter console \ | ||
| python your_qwen_agent_app.py | ||
| ``` | ||
|
|
||
| ### Manual instrumentation | ||
|
|
||
| ```python | ||
| from opentelemetry.instrumentation.qwen_agent import QwenAgentInstrumentor | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor | ||
| from qwen_agent.agents import Assistant | ||
|
|
||
| provider = TracerProvider() | ||
| provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter())) | ||
|
|
||
| QwenAgentInstrumentor().instrument(tracer_provider=provider) | ||
|
|
||
| bot = Assistant( | ||
| llm={"model": "qwen-max", "model_type": "qwen_dashscope"}, | ||
| name="my-assistant", | ||
| ) | ||
| for _ in bot.run([{"role": "user", "content": "Hello!"}]): | ||
| pass | ||
|
|
||
| QwenAgentInstrumentor().uninstrument() | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| ### Export to an OTLP backend | ||
|
|
||
| ```bash | ||
| export OTEL_SERVICE_NAME=my-qwen-agent-app | ||
| export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf | ||
| export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=<trace_endpoint> | ||
| # Optional: metrics / logs if you configure exporters globally | ||
| export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=<metrics_endpoint> | ||
|
|
||
| opentelemetry-instrument python your_app.py | ||
| ``` | ||
|
|
||
| ### GenAI semantic conventions and content capture | ||
|
|
||
| ```bash | ||
| # Enable experimental GenAI semantic conventions (recommended for this instrumentation) | ||
| export OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental | ||
|
|
||
| # Message content capture (same env vars as other GenAI instrumentations in this repo) | ||
| export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=SPAN_ONLY | ||
| # EVENT_ONLY | SPAN_AND_EVENT | NO_CONTENT | ||
| ``` | ||
|
|
||
| ## Supported components | ||
|
|
||
| | Area | Instrumented API | Span / operation | | ||
| |------|------------------|------------------| | ||
| | Agent | `Agent.run` | `invoke_agent` | | ||
| | LLM | `BaseChatModel.chat` | `chat` | | ||
| | ReAct | `Agent._call_llm` | `react step` (agents with tools) | | ||
| | Tools | `Agent._call_tool` | `execute_tool` | | ||
|
|
||
| `Agent.run_nonstream()` is not wrapped separately; it calls `run()` internally, so you still get a single `invoke_agent` span per run. | ||
|
|
||
| **Model backends** (inferred from `model_type` / class name): DashScope, OpenAI-compatible APIs, Azure OpenAI, and other Qwen-Agent–supported backends. | ||
|
|
||
| ## Visualization | ||
|
|
||
| Export telemetry to: | ||
|
|
||
| - [Alibaba Cloud ARMS / Managed Service for OpenTelemetry](https://www.aliyun.com/product/xtrace) | ||
| - [AgentScope Studio](https://github.com/agentscope-ai/agentscope-studio) or any OTLP-compatible collector | ||
| - Any OpenTelemetry-compatible backend (Jaeger, Zipkin, etc.) | ||
|
|
||
| ## License | ||
|
|
||
| Apache License 2.0 | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The format of the changelog file should follow the established conventions. Please refer to:
https://github.com/alibaba/loongsuite-python-agent/blob/main/instrumentation-loongsuite/loongsuite-instrumentation-dashscope/CHANGELOG.md