Loongsuite Python Agent is a key component of LoongSuite, Alibaba’s unified observability data collection suite, providing instrumentation for Python applications.
LoongSuite includes the following key components:
LoongCollector: universal node agent, which prodivdes log collection, prometheus metric collection, and network and security collection capabilities based on eBPF.
Loongsuite Python Agent is also a customized distribution of upstream OTel Python Agent, with enhanced support for popular AI agent framework.
The implementation follows the latest GenAI semantic conventions.
Source tree: instrumentation/. General application and library instrumentations; PyPI project is always https://pypi.org/project/opentelemetry-instrumentation-<name>/. Each line below links to that URL and the package README in this repo.
This walkthrough uses AgentScope. The same exporter and loongsuite-instrument patterns apply to other stacks once their instrumentations are installed.
Prepare a demo (AgentScope ReAct example)
Use the upstream ReAct agent example as reference: you can clone AgentScope or align with that folder’s main.py.
To connect to a different model API instead of DashScope, see the AgentScope documentation: Model tutorial.
Step 3 - Create ReAct Agent
# -*- coding: utf-8 -*-
"""The main entry point of the ReAct agent example."""
import asyncio
import os
from agentscope.agent import ReActAgent, UserAgent
from agentscope.formatter import DashScopeChatFormatter
from agentscope.memory import InMemoryMemory
from agentscope.model import DashScopeChatModel
from agentscope.tool import (
Toolkit,
execute_shell_command,
execute_python_code,
view_text_file,
)
async def main() -> None:
"""The main entry point for the ReAct agent example."""
toolkit = Toolkit()
toolkit.register_tool_function(execute_shell_command)
toolkit.register_tool_function(execute_python_code)
toolkit.register_tool_function(view_text_file)
agent = ReActAgent(
name="Friday",
sys_prompt="You are a helpful assistant named Friday.",
model=DashScopeChatModel(
api_key=os.environ.get("DASHSCOPE_API_KEY"),
model_name="qwen-max",
enable_thinking=False,
stream=True,
),
formatter=DashScopeChatFormatter(),
toolkit=toolkit,
memory=InMemoryMemory(),
)
user = UserAgent("User")
msg = None
while True:
msg = await user(msg)
if msg.get_text_content() == "exit":
break
msg = await agent(msg)
asyncio.run(main())
Install and run loongsuite
Recommended integration approach: automatic instrumentation with loongsuite-instrument after installing loongsuite-distro and your instrumentations (via loongsuite-bootstrap or manual pip).
Use loongsuite-bootstrap (shipped with loongsuite-distro) to install LoongSuite wheels from a GitHub Release tarball and compatible opentelemetry-instrumentation-* versions from PyPI. Bootstrap performs a two-phase install: LoongSuite artifacts from the release, then pinned OpenTelemetry instrumentation packages (see docs/loongsuite-release.md).
Pick one of the following:
Option A — Install everything from a release:
loongsuite-bootstrap -a install --latest
# for specific version: loongsuite-bootstrap -a install --version X.Y.Z
Option B — Auto-detect (lean environments): install only instrumentations for libraries already present:
loongsuite-bootstrap -a install --latest --auto-detect
Note: If you need packages under instrumentation-genai/, use Option A or B together with loongsuite-distro / loongsuite-bootstrap. Relying only on manual pip can cause dependency resolution conflicts when loongsuite-util-genai and the community opentelemetry-util-genai are both pulled in or pinned differently.
Step 3 — Run under loongsuite-instrument
Configure where telemetry is exported (see Configure telemetry export below) using environment variables and/or loongsuite-instrument flags, then start your app:
Under the hood this aligns with ConsoleSpanExporter, ConsoleMetricExporter, and ConsoleLogRecordExporter.
Remote / production — OTLP
Before starting your application, install opentelemetry-exporter-otlp:
pip install opentelemetry-exporter-otlp
Point OpenTelemetry at a backend that accepts OTLP (gRPC or HTTP/protobuf), using OtlpSpanExporter, OtlpMetricExporter, OtlpLogExporter (or the equivalent env vars / loongsuite-instrument flags), for example:
Note: If you need packages under instrumentation-genai/, use Option A or B together with loongsuite-distro / loongsuite-bootstrap. Relying only on manual pip can cause dependency resolution conflicts when loongsuite-util-genai and the community opentelemetry-util-genai are both pulled in or pinned differently.
Step 2 — Initialize the OpenTelemetry SDK before anything emits telemetry. You are wiring the same exporters as in Configure telemetry export.
from opentelemetry import metrics, trace
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import ConsoleMetricExporter, PeriodicExportingMetricReader
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
resource = Resource.create({"service.name": "demo"})
tracer_provider = TracerProvider(resource=resource)
tracer_provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(tracer_provider)
metric_reader = PeriodicExportingMetricReader(ConsoleMetricExporter())
metrics.set_meter_provider(
MeterProvider(resource=resource, metric_readers=[metric_reader])
)
Step 3 — Call the framework instrumentor, then start your app without loongsuite-instrument.
from opentelemetry.instrumentation.agentscope import AgentScopeInstrumentor
AgentScopeInstrumentor().instrument()
# then import / start your agents (e.g. asyncio.run(main()))
Install from source (for development)
Step 1 — Clone this repository and checkout your branch.
Step 2 — Install upstream OpenTelemetry Python core and local LoongSuite components from a Git checkout of opentelemetry-python:
cd loongsuite-python-agent
GIT_ROOT="git+https://github.com/open-telemetry/opentelemetry-python.git"
# Use ONE pip install command so resolver sees all constraints together;
# split installs can downgrade/replace api+semconv when local editable deps are installed later.
pip install \
"${GIT_ROOT}#subdirectory=opentelemetry-api" \
"${GIT_ROOT}#subdirectory=opentelemetry-semantic-conventions" \
"${GIT_ROOT}#subdirectory=opentelemetry-sdk" \
-e ./util/opentelemetry-util-genai \
-e ./opentelemetry-instrumentation \
-e ./loongsuite-distro
Step 3 — Install the instrumentations you need, for example:
Configure where telemetry is exported (see Configure telemetry export below) using environment variables and/or loongsuite-instrument flags, then start your app:
Then run python demo.py. For console exporters, other backends, using loongsuite-instrument instead of plain python, or full precedence / edge cases, see loongsuite-site-bootstrap/README.md.
Beta: Site-bootstrap affects every Python process in the environment where it is enabled; read the package README before using it in production.
loongsuite-python-agent
English | 简体中文
Introduction
Loongsuite Python Agent is a key component of LoongSuite, Alibaba’s unified observability data collection suite, providing instrumentation for Python applications.
LoongSuite includes the following key components:
Loongsuite Python Agent is also a customized distribution of upstream OTel Python Agent, with enhanced support for popular AI agent framework. The implementation follows the latest GenAI semantic conventions.
Supported frameworks and components
LoongSuite instrumentation
Source tree:
instrumentation-loongsuite/.Distro and helpers:
loongsuite-instrument,loongsuite-bootstrap)OpenTelemetry instrumentation — generative workloads
Source tree:
instrumentation-genai/. These distributions follow OpenTelemetry generative semantic conventions (opentelemetry-instrumentation-*on PyPI).OpenTelemetry instrumentation
Source tree:
instrumentation/. General application and library instrumentations; PyPI project is alwayshttps://pypi.org/project/opentelemetry-instrumentation-<name>/. Each line below links to that URL and the package README in this repo.All
instrumentation/packages (click to expand)Quick start
This walkthrough uses AgentScope. The same exporter and
loongsuite-instrumentpatterns apply to other stacks once their instrumentations are installed.Prepare a demo (AgentScope ReAct example)
Use the upstream ReAct agent example as reference: you can clone AgentScope or align with that folder’s
main.py.Step 1 — Install AgentScope
Step 2 - Configure DashScope
Replace
{your_api_key}with a valid key from the DashScope console.To connect to a different model API instead of DashScope, see the AgentScope documentation: Model tutorial.
Step 3 - Create ReAct Agent
Install and run loongsuite
Recommended integration approach: automatic instrumentation with
loongsuite-instrumentafter installingloongsuite-distroand your instrumentations (vialoongsuite-bootstrapor manualpip).Step 1 — Install the distro
Optional:
pip install loongsuite-distro[otlp]for OTLP extras (loongsuite-distro README).Step 2 — Install instrumentations
Use
loongsuite-bootstrap(shipped withloongsuite-distro) to install LoongSuite wheels from a GitHub Release tarball and compatibleopentelemetry-instrumentation-*versions from PyPI. Bootstrap performs a two-phase install: LoongSuite artifacts from the release, then pinned OpenTelemetry instrumentation packages (see docs/loongsuite-release.md).Pick one of the following:
Option A — Install everything from a release:
Option B — Auto-detect (lean environments): install only instrumentations for libraries already present:
Option C — Manual
pip: install packages yourself from PyPI using the names in Supported frameworks and components.Step 3 — Run under
loongsuite-instrumentConfigure where telemetry is exported (see Configure telemetry export below) using environment variables and/or
loongsuite-instrumentflags, then start your app:For programmatic instrumentation, install from source, or site-bootstrap (
loongsuite-site-bootstrap), see Alternative installation methods.Configure telemetry export
Local debugging — console
Use the SDK’s console exporters so traces/metrics/logs print to the terminal, for example via
loongsuite-instrument:Under the hood this aligns with
ConsoleSpanExporter,ConsoleMetricExporter, andConsoleLogRecordExporter.Remote / production — OTLP
Before starting your application, install
opentelemetry-exporter-otlp:Point OpenTelemetry at a backend that accepts OTLP (gRPC or HTTP/protobuf), using
OtlpSpanExporter,OtlpMetricExporter,OtlpLogExporter(or the equivalent env vars /loongsuite-instrumentflags), for example:See also OpenTelemetry environment variables for
OTEL_EXPORTER_OTLP_*.Alternative installation methods
If you are not using the recommended
loongsuite-instrumentintegration approach, use one of the options below.Programmatic instrumentation
For applications where you can edit code and want explicit control over OpenTelemetry initialization.
Step 1 — Install instrumentations yourself from PyPI using the names in Supported frameworks and components.
Step 2 — Initialize the OpenTelemetry SDK before anything emits telemetry. You are wiring the same exporters as in Configure telemetry export.
Step 3 — Call the framework instrumentor, then start your app without
loongsuite-instrument.Install from source (for development)
Step 1 — Clone this repository and checkout your branch.
Step 2 — Install upstream OpenTelemetry Python core and local LoongSuite components from a Git checkout of opentelemetry-python:
Step 3 — Install the instrumentations you need, for example:
Step 4 — Run under
loongsuite-instrumentConfigure where telemetry is exported (see Configure telemetry export below) using environment variables and/or
loongsuite-instrumentflags, then start your app:Site-bootstrap (Beta)
Run without changing codes or bootstrap commands: a
.pthhook loads LoongSuite’s distro early (see loongsuite-site-bootstrap/README.md).Step 1 - Install LoongSuite Site Bootstrap
Step 2 — Install instrumentations
If you want a different installation approach, see Step 2 — Install instrumentations in Install and run loongsuite.
Step 3 — Enable the hook:
Step 4 — Create
~/.loongsuite/bootstrap-config.jsonwith the OpenTelemetry environments keys you need.Then run
python demo.py. For console exporters, other backends, usingloongsuite-instrumentinstead of plainpython, or full precedence / edge cases, see loongsuite-site-bootstrap/README.md.Optional: OTLP examples
AgentScope Studio
AgentScope Studio provides a web UI for traces and metrics.
Use the OTLP endpoint Studio prints (often
http://127.0.0.1:31415), for example:Or set
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT/OTEL_EXPORTER_OTLP_METRICS_ENDPOINTaccordingly. Details: AgentScope Studio.Forward OTLP to Jaeger via LoongCollector
Launch Jaeger
Launch LoongCollector
conf/continuous_pipeline_config/local/oltp.yaml:Run the demo against LoongCollector → Jaeger
Open the Jaeger UI and confirm traces arrive.
Community
We are looking forward to your feedback and suggestions. You can join our DingTalk group or scan the QR code below to engage with us.
Resources