Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/agentscope/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def init(
logging_level: str = "INFO",
studio_url: str | None = None,
tracing_url: str | None = None,
tracing_protocol: str = "http/protobuf",
) -> None:
"""Initialize the agentscope library.

Expand All @@ -97,6 +98,11 @@ def init(
OpenTelemetry tracing platforms like Arize-Phoenix and Langfuse.
If not provided and `studio_url` is provided, it will send traces
to the AgentScope Studio's tracing endpoint.
tracing_protocol (`str`, optional):
The protocol to use for the trace exporter. Supported values are:
- "grpc": Use gRPC protocol
- "http/protobuf": Use HTTP protocol with protobuf encoding
Defaults to "http/protobuf".
"""

if project:
Expand Down Expand Up @@ -148,7 +154,7 @@ def init(
if endpoint:
from .tracing import setup_tracing

setup_tracing(endpoint=endpoint)
setup_tracing(endpoint=endpoint, protocol=tracing_protocol)
_config.trace_enabled = True


Expand Down
21 changes: 17 additions & 4 deletions src/agentscope/tracing/_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@
"""The tracing interface class in agentscope."""


def setup_tracing(endpoint: str) -> None:
def setup_tracing(
endpoint: str,
protocol: str = "http/protobuf",
) -> None:
"""Set up the AgentScope tracing by configuring the endpoint URL.

Args:
endpoint (`str`):
The endpoint URL for the tracing exporter.
protocol (`str`, optional):
The protocol to use for the trace exporter. Supported values are:
- "grpc": Use gRPC protocol
- "http/protobuf": Use HTTP protocol with protobuf encoding
Defaults to "http/protobuf".
"""
# Lazy import
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
OTLPSpanExporter,
)
from opentelemetry import trace

tracer_provider = TracerProvider()
if protocol == "grpc":
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
OTLPSpanExporter,
)
else:
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
OTLPSpanExporter,
)
exporter = OTLPSpanExporter(endpoint=endpoint)
span_processor = BatchSpanProcessor(exporter)
tracer_provider.add_span_processor(span_processor)
Expand Down