AgentScope¶
AgentScope can report traces through OpenTelemetry to help analyze Agent execution, model requests, and tool calls. This guide uses Java as an example and sends data directly to Guance using OTLP HTTP/Protobuf.
Prerequisites¶
- You have a working AgentScope Java application with a model and its API Key configured. In the code below,
modelis an existing model instance in your application. - Choose the integration example that matches the AgentScope Java version used by your application: 2.0 uses
OtelTracingMiddleware, while 1.x usesTracerRegistryandTelemetryTracer.
Configuration¶
Obtain the integration configuration¶
Select AgentScope in the platform's Agent integration configuration and copy the reporting endpoints, headers, and resource attributes generated by the current platform. The credentials in the commands are valid only for the current platform.
Enable Associate workspace member information as needed, and select or confirm the username. When enabled, populate user_id and user_name with the member UUID and username generated by the interface. Remove these two attributes if you do not associate a member.
Configure environment variables¶
Set the following variables in the same terminal or container environment where you start the Java application. Replace <...> with the actual values from the integration configuration. The reporting domain varies by site, so use the complete endpoints generated by the interface.
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_TRACES_EXPORTER=otlp
export OTEL_METRICS_EXPORTER=otlp
export OTEL_LOGS_EXPORTER=otlp
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="https://<YOUR_LLM_INGESTION_HOST>/v1/write/otel-llm"
export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT="https://<YOUR_LLM_INGESTION_HOST>/v1/write/otel-metrics"
export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT="https://<YOUR_LLM_INGESTION_HOST>/v1/write/otel-logs"
export OTEL_RESOURCE_ATTRIBUTES="service.version=1.0.0,deployment.environment=production,agent_id=<AGENT_ID>,agent_name=<AGENT_NAME>,user_id=<USER_ID>,user_name=<USER_NAME>,agent_runtime=agentscope"
export OTEL_EXPORTER_OTLP_HEADERS="x-token=<WORKSPACE_TOKEN>,to-headless=true"
Parameter descriptions¶
| Parameter | Description |
|---|---|
OTEL_EXPORTER_OTLP_*_ENDPOINT |
Complete reporting endpoints for traces, metrics, and logs, ending in /v1/write/otel-llm, /v1/write/otel-metrics, and /v1/write/otel-logs, respectively. |
OTEL_EXPORTER_OTLP_HEADERS |
Authentication headers. Keep the x-token and to-headless=true generated by the interface. |
service.version, deployment.environment |
Application version and deployment environment. Adjust as needed. |
agent_id, agent_name |
Agent identifier and name generated by the current integration configuration. |
user_id, user_name |
Optional platform member UUID and username. |
agent_runtime |
Agent runtime framework. Set to agentscope in this example. |
Configuring metrics and logs exporters does not automatically generate the corresponding business data. The actual data depends on metric instrumentation and log bridging in the application. The code below primarily integrates traces.
Integration code¶
The application requires the io.opentelemetry:opentelemetry-sdk-extension-autoconfigure and io.opentelemetry:opentelemetry-exporter-otlp dependencies. Align their versions through the OpenTelemetry BOM used by your project to avoid conflicts with AgentScope's transitive dependencies.
Both versions below share the environment variables above. Select the tab for your version and add the code to the application initialization logic. Initialize the SDK only once per process.
Version 2.0 uses OtelTracingMiddleware to collect Agent execution, model requests, and tool calls. Register the automatically configured SDK with GlobalOpenTelemetry first, then create the Agent and add middleware to each Agent that needs tracing.
import io.agentscope.core.ReActAgent;
import io.agentscope.core.tracing.OtelTracingMiddleware;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
OpenTelemetrySdk sdk = AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal()
.build()
.getOpenTelemetrySdk();
Runtime.getRuntime().addShutdownHook(new Thread(sdk::close));
ReActAgent agent = ReActAgent.builder()
.name("Assistant")
.model(model)
.middleware(new OtelTracingMiddleware())
.build();
The SDK reads the environment variables above to configure export endpoints, headers, and resource attributes. If GlobalOpenTelemetry is already registered in the runtime environment, reuse the existing SDK and ensure it has the appropriate export endpoints and authentication settings. Only add middleware to the Agent to avoid registering the global instance again.
Ensure that your version provides TracerRegistry, TelemetryTracer, and the corresponding Studio extension dependencies. Register the tracer before creating and invoking the Agent:
import io.agentscope.core.ReActAgent;
import io.agentscope.core.tracing.TracerRegistry;
import io.agentscope.core.tracing.telemetry.TelemetryTracer;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
OpenTelemetrySdk sdk = AutoConfiguredOpenTelemetrySdk.initialize()
.getOpenTelemetrySdk();
Runtime.getRuntime().addShutdownHook(new Thread(sdk::close));
TracerRegistry.register(
TelemetryTracer.builder()
.tracer(sdk.getTracer("agentscope-java"))
.build()
);
ReActAgent agent = ReActAgent.builder()
.name("Assistant")
.model(model)
.build();
Explicitly pass the tracer from the automatically configured SDK to TelemetryTracer so that the reporting endpoints, headers, and resource attributes in the environment variables take effect. Calling only TelemetryTracer.builder().build() does not guarantee that the complete configuration above is read.
Verification¶
- Start the application in a process that has loaded the environment variables, and actually run an Agent conversation or tool call.
- View LLM traces on the current platform, filter by
agent_idoragent_name, and check Agent execution, model requests, and the tool calls that were actually triggered. - If no data appears, check whether the application has loaded the environment variables, whether the reporting endpoints and credentials belong to the same platform, whether the network is reachable, and whether the SDK reports export errors.