agentengine-sdk-python 0.7.0__py3-none-any.whl → 0.8.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {agentengine_sdk_python-0.7.0.dist-info → agentengine_sdk_python-0.8.1.dist-info}/METADATA +52 -20
- agentengine_sdk_python-0.8.1.dist-info/RECORD +644 -0
- {agentengine_sdk_python-0.7.0.dist-info → agentengine_sdk_python-0.8.1.dist-info}/WHEEL +1 -1
- ksadk/a2a/__init__.py +180 -11
- ksadk/a2a/bootstrap.py +332 -0
- ksadk/a2a/card.py +15 -0
- ksadk/a2a/context_store.py +203 -0
- ksadk/a2a/control_plane.py +1056 -0
- ksadk/a2a/credential.py +99 -0
- ksadk/a2a/event_adapter.py +149 -0
- ksadk/a2a/executor.py +361 -137
- ksadk/a2a/external_transport.py +516 -0
- ksadk/a2a/identity.py +304 -0
- ksadk/a2a/ids.py +22 -0
- ksadk/a2a/langgraph.py +285 -0
- ksadk/a2a/resume_store.py +201 -0
- ksadk/a2a/routes.py +112 -0
- ksadk/a2a/server.py +102 -62
- ksadk/a2a/service_env.py +33 -0
- ksadk/a2a/space_client.py +1122 -0
- ksadk/a2a/task_adapter.py +423 -0
- ksadk/a2a/task_event_dispatcher.py +167 -0
- ksadk/a2a/task_event_outbox.py +326 -0
- ksadk/a2a/task_store.py +206 -0
- ksadk/a2a_card.py +101 -0
- ksadk/a2ui/__init__.py +27 -0
- ksadk/a2ui/core.py +186 -0
- ksadk/a2ui/models.py +166 -0
- ksadk/a2ui/renderer.py +198 -0
- ksadk/agui/__init__.py +9 -0
- ksadk/agui/a2ui_projection.py +92 -0
- ksadk/agui/agent.py +872 -0
- ksadk/agui/config.py +92 -0
- ksadk/agui/routes.py +47 -0
- ksadk/api/client.py +238 -204
- ksadk/api/environment.py +27 -0
- ksadk/builders/__init__.py +2 -0
- ksadk/builders/base.py +16 -14
- ksadk/builders/code_builder.py +375 -270
- ksadk/builders/container_builder.py +240 -200
- ksadk/builders/framework_requirements.py +31 -12
- ksadk/builders/ks3_uploader.py +60 -29
- ksadk/builders/managed_runtime_builder.py +173 -0
- ksadk/builders/mcp_builder.py +91 -92
- ksadk/builders/requirements_utils.py +1 -5
- ksadk/cli/__init__.py +97 -171
- ksadk/cli/cmd_a2a.py +255 -84
- ksadk/cli/cmd_agent.py +19 -5
- ksadk/cli/cmd_build.py +158 -29
- ksadk/cli/cmd_completion.py +47 -35
- ksadk/cli/cmd_config.py +267 -198
- ksadk/cli/cmd_create.py +507 -740
- ksadk/cli/cmd_dashboard.py +120 -45
- ksadk/cli/cmd_deploy.py +135 -59
- ksadk/cli/cmd_destroy.py +44 -14
- ksadk/cli/cmd_eval.py +386 -0
- ksadk/cli/cmd_files.py +63 -24
- ksadk/cli/cmd_hermes.py +313 -135
- ksadk/cli/cmd_invoke.py +184 -122
- ksadk/cli/cmd_launch.py +76 -28
- ksadk/cli/cmd_mcp.py +98 -64
- ksadk/cli/cmd_model.py +14 -5
- ksadk/cli/cmd_openclaw.py +564 -268
- ksadk/cli/cmd_replay.py +71 -0
- ksadk/cli/cmd_run.py +58 -73
- ksadk/cli/cmd_status.py +80 -31
- ksadk/cli/cmd_studio.py +110 -0
- ksadk/cli/cmd_version.py +20 -13
- ksadk/cli/cmd_web.py +95 -18
- ksadk/cli/deploy_utils.py +20 -20
- ksadk/cli/dry_run.py +7 -4
- ksadk/cli/env_options.py +83 -0
- ksadk/cli/error_utils.py +37 -12
- ksadk/cli/global_options.py +4 -3
- ksadk/cli/local_runtime.py +1 -2
- ksadk/cli/model_catalog.py +6 -2
- ksadk/cli/network_options.py +41 -19
- ksadk/cli/resource_common.py +6 -2
- ksadk/cli/runtime_bootstrap.py +51 -0
- ksadk/cli/storage.py +0 -1
- ksadk/cli/ui.py +11 -3
- ksadk/cli/workflow_common.py +52 -18
- ksadk/codex/__init__.py +12 -0
- ksadk/codex/client.py +1042 -0
- ksadk/codex/phase.py +143 -0
- ksadk/codex/runtime.py +940 -0
- ksadk/common/aicp_env.py +0 -1
- ksadk/common/auth.py +2 -2
- ksadk/common/constants.py +1 -3
- ksadk/common/kop_client.py +232 -0
- ksadk/common/llm_utils.py +17 -13
- ksadk/compat/__init__.py +5 -0
- ksadk/compat/adk_compat.py +200 -0
- ksadk/compat/copilotkit_a2ui.py +330 -0
- ksadk/configs/__init__.py +12 -14
- ksadk/configs/env_registry.py +661 -116
- ksadk/configs/global_config.py +3 -2
- ksadk/configs/settings.py +133 -209
- ksadk/conversations/__init__.py +85 -92
- ksadk/conversations/attachment_storage.py +16 -11
- ksadk/conversations/attachments.py +44 -36
- ksadk/conversations/compaction_pipeline.py +12 -12
- ksadk/conversations/compaction_prompt.py +9 -5
- ksadk/conversations/context.py +221 -6
- ksadk/conversations/message_projection.py +438 -51
- ksadk/conversations/model_context.py +12 -6
- ksadk/conversations/model_options.py +0 -1
- ksadk/conversations/normalize.py +20 -16
- ksadk/conversations/reasoning_markup.py +1 -1
- ksadk/conversations/runtime.py +339 -5255
- ksadk/conversations/runtime_compaction.py +333 -0
- ksadk/conversations/runtime_constants.py +27 -0
- ksadk/conversations/runtime_governance.py +185 -0
- ksadk/conversations/runtime_input.py +660 -0
- ksadk/conversations/runtime_invocation.py +395 -0
- ksadk/conversations/runtime_metadata.py +475 -0
- ksadk/conversations/runtime_observability.py +367 -0
- ksadk/conversations/runtime_payloads.py +272 -0
- ksadk/conversations/runtime_persistence.py +440 -0
- ksadk/conversations/runtime_preparation.py +426 -0
- ksadk/conversations/runtime_resume.py +836 -0
- ksadk/conversations/runtime_stream_events.py +902 -0
- ksadk/conversations/runtime_streaming.py +700 -0
- ksadk/conversations/semantic_summary.py +17 -5
- ksadk/conversations/session_title.py +6 -3
- ksadk/deployment/__init__.py +15 -16
- ksadk/deployment/agent_access.py +12 -23
- ksadk/deployment/base.py +51 -38
- ksadk/deployment/managed_runtime.py +33 -0
- ksadk/deployment/manager.py +85 -71
- ksadk/deployment/providers/serverless.py +379 -210
- ksadk/deployment/registry.py +38 -32
- ksadk/deployment/state.py +11 -11
- ksadk/deployment/ui_config.py +1 -1
- ksadk/detection/__init__.py +1 -1
- ksadk/detection/detector.py +146 -83
- ksadk/detection/mcp_detector.py +9 -6
- ksadk/evaluation/__init__.py +84 -0
- ksadk/evaluation/a2a_adapter.py +444 -0
- ksadk/evaluation/adapters.py +54 -0
- ksadk/evaluation/contracts.py +409 -0
- ksadk/evaluation/evalset.py +351 -0
- ksadk/evaluation/evaluators.py +461 -0
- ksadk/evaluation/executor.py +118 -0
- ksadk/evaluation/storage.py +68 -0
- ksadk/evaluation/target.py +73 -0
- ksadk/events/__init__.py +25 -0
- ksadk/events/parser.py +191 -0
- ksadk/events/replay.py +37 -0
- ksadk/events/runtime_event.py +273 -0
- ksadk/events/store.py +317 -0
- ksadk/harness/__init__.py +35 -0
- ksadk/harness/app.py +269 -0
- ksadk/harness/config.py +181 -0
- ksadk/harness/reasoner.py +105 -0
- ksadk/harness/runtime.py +364 -0
- ksadk/harness/sandbox.py +141 -0
- ksadk/harness/tools.py +222 -0
- ksadk/hermes_terminal.py +28 -12
- ksadk/identity/resolver.py +15 -9
- ksadk/ids.py +32 -0
- ksadk/knowledge_base/adk_tool.py +1 -1
- ksadk/knowledge_base/client.py +38 -33
- ksadk/managed_a2a_card.py +160 -0
- ksadk/managed_runtime.py +195 -0
- ksadk/markdown.py +5 -2
- ksadk/mcp_runtime/__init__.py +9 -6
- ksadk/memory/__init__.py +1 -1
- ksadk/memory/adk/__init__.py +2 -0
- ksadk/memory/adk/backends/base_ltm_backend.py +2 -6
- ksadk/memory/adk/backends/http_ltm_backend.py +13 -21
- ksadk/memory/adk/backends/inmemory_ltm_backend.py +7 -13
- ksadk/memory/adk/backends/sdk_ltm_backend.py +36 -36
- ksadk/memory/adk/long_term_memory.py +34 -37
- ksadk/memory/adk/resilient_session_service.py +4 -5
- ksadk/memory/adk/short_term_memory.py +12 -21
- ksadk/memory/adk_tool.py +3 -2
- ksadk/memory/backends/base.py +19 -19
- ksadk/memory/backends/memory.py +24 -25
- ksadk/memory/backends/redis.py +31 -30
- ksadk/memory/langchain_tool.py +2 -1
- ksadk/memory/manager.py +52 -47
- ksadk/memory/service.py +16 -6
- ksadk/memory/tool.py +4 -0
- ksadk/model_policy.py +15 -7
- ksadk/model_proxy/__init__.py +51 -0
- ksadk/model_proxy/bootstrap.py +80 -0
- ksadk/model_proxy/cache.py +165 -0
- ksadk/model_proxy/config.py +66 -0
- ksadk/model_proxy/demo_webui.py +115 -0
- ksadk/model_proxy/detect.py +145 -0
- ksadk/model_proxy/gate.py +68 -0
- ksadk/model_proxy/namespace.py +201 -0
- ksadk/model_proxy/protocol_proxy.py +22 -0
- ksadk/model_proxy/server.py +443 -0
- ksadk/model_proxy/transform.py +735 -0
- ksadk/openclaw_gateway.py +29 -14
- ksadk/runners/adk_runner.py +354 -146
- ksadk/runners/base_runner.py +71 -29
- ksadk/runners/deepagents_runner.py +0 -1
- ksadk/runners/factory.py +18 -5
- ksadk/runners/langchain_runner.py +29 -742
- ksadk/runners/langgraph_runner.py +417 -97
- ksadk/runners/patch_langchain.py +16 -16
- ksadk/runners/remote_runner.py +134 -33
- ksadk/runners/usage_accumulator.py +1 -0
- ksadk/runners/utils/__init__.py +0 -10
- ksadk/runners/utils/loader.py +23 -7
- ksadk/runtime/__init__.py +67 -0
- ksadk/runtime/adapter.py +412 -0
- ksadk/runtime/conversation_execution.py +553 -0
- ksadk/runtime/executor.py +251 -0
- ksadk/runtime/factory.py +156 -0
- ksadk/runtime/framework_adapters.py +118 -0
- ksadk/runtime/launch.py +48 -0
- ksadk/runtime/preprocessing.py +131 -0
- ksadk/runtime/runner_adapter.py +1032 -0
- ksadk/runtime/runner_loading.py +36 -0
- ksadk/runtime_context.py +18 -3
- ksadk/runtime_state.py +0 -1
- ksadk/sandbox/__init__.py +4 -1
- ksadk/sandbox/backends/__init__.py +4 -1
- ksadk/sandbox/backends/e2b.py +13 -5
- ksadk/sandbox/backends/local_process.py +11 -3
- ksadk/sandbox/base.py +10 -15
- ksadk/sandbox/factory.py +14 -4
- ksadk/sandbox/registry.py +57 -6
- ksadk/server/__init__.py +39 -4
- ksadk/server/api_models.py +15 -5
- ksadk/server/app.py +24 -3928
- ksadk/server/composition.py +47 -0
- ksadk/server/factory.py +622 -0
- ksadk/server/routes/__init__.py +1 -0
- ksadk/server/routes/checkpoint_resolution.py +116 -0
- ksadk/server/routes/common.py +682 -0
- ksadk/server/routes/control.py +331 -0
- ksadk/server/routes/dependencies.py +98 -0
- ksadk/server/routes/misc.py +425 -0
- ksadk/server/routes/models.py +451 -0
- ksadk/server/routes/openai_compat.py +229 -0
- ksadk/server/routes/projection.py +848 -0
- ksadk/server/routes/routers.py +52 -0
- ksadk/server/routes/run.py +519 -0
- ksadk/server/routes/sessions.py +644 -0
- ksadk/server/routes/streaming.py +265 -0
- ksadk/server/routes/workspace.py +414 -0
- ksadk/server/static/assets/{ArtifactsPanel-D-SSn2ee.js → ArtifactsPanel-CakCqxEN.js} +1 -1
- ksadk/server/static/assets/CodeBlock-CynLknyO.js +9 -0
- ksadk/server/static/assets/MathMessageMarkdown-3sQdOysS.js +8 -0
- ksadk/server/static/assets/MermaidBlock-NERD_ef7.js +303 -0
- ksadk/server/static/assets/NativeTerminalPanel-CiHWf82q.js +1 -0
- ksadk/server/static/assets/{arc-BA13sHc9.js → arc-DrgjcKhl.js} +1 -1
- ksadk/server/static/assets/architecture-7EHR7CIX-CpZLRCcY.js +1 -0
- ksadk/server/static/assets/{architectureDiagram-3BPJPVTR-DzJzgVoL.js → architectureDiagram-3BPJPVTR-BXoVRbQ6.js} +1 -1
- ksadk/server/static/assets/{blockDiagram-GPEHLZMM-UTdVcZhn.js → blockDiagram-GPEHLZMM-BnaB3Ywv.js} +1 -1
- ksadk/server/static/assets/{c4Diagram-AAUBKEIU-Dix0GD4m.js → c4Diagram-AAUBKEIU-Dw4weZcp.js} +1 -1
- ksadk/server/static/assets/channel-Dt3rQTFI.js +1 -0
- ksadk/server/static/assets/{chunk-2J33WTMH-C2JxuZ42.js → chunk-2J33WTMH-ChjpLK7j.js} +1 -1
- ksadk/server/static/assets/{chunk-4BX2VUAB-Be57QPgD.js → chunk-4BX2VUAB-B9drTgOW.js} +1 -1
- ksadk/server/static/assets/{chunk-55IACEB6-WjSuy4nv.js → chunk-55IACEB6-CK4ROATy.js} +1 -1
- ksadk/server/static/assets/{chunk-727SXJPM-BOovP-b5.js → chunk-727SXJPM-B9KtTTgw.js} +1 -1
- ksadk/server/static/assets/{chunk-AQP2D5EJ-DOmZNmWj.js → chunk-AQP2D5EJ-1O1mki3m.js} +1 -1
- ksadk/server/static/assets/{chunk-FMBD7UC4-DzwlodkU.js → chunk-FMBD7UC4-B_fH2YuH.js} +1 -1
- ksadk/server/static/assets/{chunk-ND2GUHAM-DMqAikEU.js → chunk-ND2GUHAM-D6rFLaVV.js} +1 -1
- ksadk/server/static/assets/chunk-QZHKN3VN-iXEZtKQc.js +1 -0
- ksadk/server/static/assets/classDiagram-4FO5ZUOK-DIveV17Q.js +1 -0
- ksadk/server/static/assets/classDiagram-v2-Q7XG4LA2-DIveV17Q.js +1 -0
- ksadk/server/static/assets/{cose-bilkent-S5V4N54A-Zhy2GBnP.js → cose-bilkent-S5V4N54A-ChNs_Mdk.js} +1 -1
- ksadk/server/static/assets/{dagre-BM42HDAG-CbKrtIUz.js → dagre-BM42HDAG-D0QXg_MQ.js} +1 -1
- ksadk/server/static/assets/{diagram-2AECGRRQ-BRApgQRK.js → diagram-2AECGRRQ-DBDDKwBQ.js} +1 -1
- ksadk/server/static/assets/{diagram-5GNKFQAL-D0xbnDiK.js → diagram-5GNKFQAL-BkwDOxmO.js} +1 -1
- ksadk/server/static/assets/{diagram-KO2AKTUF-CVugVI9L.js → diagram-KO2AKTUF-Bv8waYvY.js} +1 -1
- ksadk/server/static/assets/{diagram-LMA3HP47-DozUGgee.js → diagram-LMA3HP47-e1VECDdu.js} +1 -1
- ksadk/server/static/assets/{diagram-OG6HWLK6-ZOYsKlru.js → diagram-OG6HWLK6-Ds9adfRl.js} +1 -1
- ksadk/server/static/assets/{dist-DpKtt4Bo.js → dist-4N2NMVYg.js} +1 -1
- ksadk/server/static/assets/{dist-CkEKJFtk.js → dist-B-jNR6Vn.js} +1 -1
- ksadk/server/static/assets/{dist-D57wnGWd.js → dist-B5J2uhNk.js} +1 -1
- ksadk/server/static/assets/{dist-8_SfK1IP.js → dist-B5oS1zcj.js} +1 -1
- ksadk/server/static/assets/{dist-DMezvBGJ.js → dist-BB3-OwTQ.js} +1 -1
- ksadk/server/static/assets/{dist-2hInLS_J.js → dist-BIlKoW0k.js} +1 -1
- ksadk/server/static/assets/{dist-BrCcQ47T.js → dist-BjOdlDjj.js} +1 -1
- ksadk/server/static/assets/{dist-BmlMPsCc.js → dist-C1sz5Eo3.js} +1 -1
- ksadk/server/static/assets/{dist-B0NP_Z0z.js → dist-CWV4lDr1.js} +1 -1
- ksadk/server/static/assets/{dist-D-xOD048.js → dist-CmZHzlwZ.js} +1 -1
- ksadk/server/static/assets/{dist-DL_NKc_z.js → dist-D6MpCZtz.js} +1 -1
- ksadk/server/static/assets/{dist-DyOkZmze.js → dist-DJN_6Y39.js} +1 -1
- ksadk/server/static/assets/{dist-DmFaq_Iy.js → dist-DQU9EWw-.js} +1 -1
- ksadk/server/static/assets/{dist-gX6GOLKq.js → dist-DWKByxWV.js} +1 -1
- ksadk/server/static/assets/{dist-DyWsb78Q.js → dist-Dli7qW-B.js} +1 -1
- ksadk/server/static/assets/{dist-BIJVDMuB.js → dist-DsHPdk3W.js} +1 -1
- ksadk/server/static/assets/{dist-Dp0k7bWI.js → dist-X4TnqwOK.js} +1 -1
- ksadk/server/static/assets/{dist-8EEyGgDx.js → dist-_pHPTyZ-.js} +1 -1
- ksadk/server/static/assets/{dist-CPBtqRTW.js → dist-fhOuWds5.js} +1 -1
- ksadk/server/static/assets/{dist-D-JMdWPQ.js → dist-gLnrGsoU.js} +1 -1
- ksadk/server/static/assets/{dist-BssVmjIo.js → dist-j6Dxmf_j.js} +1 -1
- ksadk/server/static/assets/{dist-C3HIIaN5.js → dist-o2OVmKvt.js} +1 -1
- ksadk/server/static/assets/{erDiagram-TEJ5UH35-Bk-9bgE5.js → erDiagram-TEJ5UH35-BPItFKH6.js} +1 -1
- ksadk/server/static/assets/eventmodeling-FCH6USID-DYFSxbqr.js +1 -0
- ksadk/server/static/assets/{flowDiagram-I6XJVG4X-Dn9kZVuZ.js → flowDiagram-I6XJVG4X-BbYB9w5Y.js} +1 -1
- ksadk/server/static/assets/{ganttDiagram-6RSMTGT7-BGxXlJqm.js → ganttDiagram-6RSMTGT7-TQXa0KXv.js} +1 -1
- ksadk/server/static/assets/gitGraph-WXDBUCRP-DyNYp6BN.js +1 -0
- ksadk/server/static/assets/{gitGraphDiagram-PVQCEYII-C7Q0X-wi.js → gitGraphDiagram-PVQCEYII-bKblCnpa.js} +1 -1
- ksadk/server/static/assets/index-R52pJJvg.js +177 -0
- ksadk/server/static/assets/index-jJr3_955.css +2 -0
- ksadk/server/static/assets/info-J43DQDTF-WUx7qTde.js +1 -0
- ksadk/server/static/assets/{infoDiagram-5YYISTIA-BO_CSqq5.js → infoDiagram-5YYISTIA-BGVT7J_i.js} +1 -1
- ksadk/server/static/assets/{ishikawaDiagram-YF4QCWOH-eYLPLd46.js → ishikawaDiagram-YF4QCWOH-K0OOfVna.js} +1 -1
- ksadk/server/static/assets/{journeyDiagram-JHISSGLW-De9geSb0.js → journeyDiagram-JHISSGLW-8LeOJaZp.js} +1 -1
- ksadk/server/static/assets/{kanban-definition-UN3LZRKU-Cs80kQto.js → kanban-definition-UN3LZRKU-C6uCgZrW.js} +1 -1
- ksadk/server/static/assets/{katex-DSrCFD8n.js → katex-CYWseY5E.js} +1 -1
- ksadk/server/static/assets/{linear-CN9sbKTF.js → linear-hdBcYzM8.js} +1 -1
- ksadk/server/static/assets/{mermaid-parser.core-DlgSBU8l.js → mermaid-parser.core-BWrDjSZM.js} +2 -2
- ksadk/server/static/assets/{mindmap-definition-RKZ34NQL-9afz7Oug.js → mindmap-definition-RKZ34NQL-Bjy_2v4_.js} +1 -1
- ksadk/server/static/assets/packet-YPE3B663-BN13y_xl.js +1 -0
- ksadk/server/static/assets/pie-LRSECV5Y-ftSLXhhc.js +1 -0
- ksadk/server/static/assets/{pieDiagram-4H26LBE5-DPv_VstN.js → pieDiagram-4H26LBE5-GmmqNiVH.js} +1 -1
- ksadk/server/static/assets/{quadrantDiagram-W4KKPZXB-o77wZ1XK.js → quadrantDiagram-W4KKPZXB-BmkRbwCg.js} +1 -1
- ksadk/server/static/assets/radar-GUYGQ44K-jaICkIeO.js +1 -0
- ksadk/server/static/assets/{requirementDiagram-4Y6WPE33-BYVw81VC.js → requirementDiagram-4Y6WPE33-DSWFqL7c.js} +1 -1
- ksadk/server/static/assets/{sankeyDiagram-5OEKKPKP-B7nZFMKT.js → sankeyDiagram-5OEKKPKP-l6iXmU65.js} +1 -1
- ksadk/server/static/assets/{sequenceDiagram-3UESZ5HK-BGRRrFw5.js → sequenceDiagram-3UESZ5HK-Cq6UBeQB.js} +1 -1
- ksadk/server/static/assets/{stateDiagram-AJRCARHV-BUfHCoPs.js → stateDiagram-AJRCARHV-qzTQpg3Y.js} +1 -1
- ksadk/server/static/assets/stateDiagram-v2-BHNVJYJU-BYCoYGAN.js +1 -0
- ksadk/server/static/assets/{timeline-definition-PNZ67QCA-FbCTGrf9.js → timeline-definition-PNZ67QCA-DEz24vV0.js} +1 -1
- ksadk/server/static/assets/treeView-BLDUP644-Cpib_vlz.js +1 -0
- ksadk/server/static/assets/treemap-LRROVOQU-DQxfmVHK.js +1 -0
- ksadk/server/static/assets/{vennDiagram-CIIHVFJN-DWgwtPsJ.js → vennDiagram-CIIHVFJN-CMCixki2.js} +1 -1
- ksadk/server/static/assets/wardley-L42UT6IY-BQYHnnkU.js +1 -0
- ksadk/server/static/assets/{wardleyDiagram-YWT4CUSO-2x8G-tTM.js → wardleyDiagram-YWT4CUSO-CVzB8F9K.js} +1 -1
- ksadk/server/static/assets/{xychartDiagram-2RQKCTM6-Drma3AYl.js → xychartDiagram-2RQKCTM6-BFBqBLc1.js} +1 -1
- ksadk/server/static/index.html +2 -2
- ksadk/server/terminal_sessions.py +111 -58
- ksadk/sessions/__init__.py +69 -19
- ksadk/sessions/base.py +31 -11
- ksadk/sessions/continuity.py +2 -3
- ksadk/sessions/in_memory.py +46 -2
- ksadk/sessions/local_service.py +148 -17
- ksadk/sessions/postgres_service.py +143 -25
- ksadk/sessions/resilient.py +36 -8
- ksadk/skills/models.py +17 -5
- ksadk/skills/package_store.py +5 -2
- ksadk/skills/runtime/agent.py +20 -5
- ksadk/skills/runtime/artifacts.py +1 -5
- ksadk/skills/runtime/backends/e2b.py +10 -4
- ksadk/skills/runtime/backends/local.py +15 -3
- ksadk/skills/runtime/base.py +1 -2
- ksadk/skills/runtime/executor.py +10 -3
- ksadk/skills/runtime/factory.py +4 -2
- ksadk/skills/runtime/loader.py +1 -2
- ksadk/skills/runtime/registry.py +15 -10
- ksadk/skills/runtime/request.py +3 -1
- ksadk/skills/service_client.py +47 -16
- ksadk/skills/tool_defs.py +15 -10
- ksadk/studio/__init__.py +5 -0
- ksadk/studio/agent_avatar_assets.py +120 -0
- ksadk/studio/agent_lifecycle.py +51 -0
- ksadk/studio/api.py +1046 -0
- ksadk/studio/api_catalog_routes.py +324 -0
- ksadk/studio/api_contracts.py +177 -0
- ksadk/studio/api_helpers.py +132 -0
- ksadk/studio/authoring.py +524 -0
- ksadk/studio/authoring_coordinator.py +285 -0
- ksadk/studio/builder.py +320 -0
- ksadk/studio/capabilities.py +351 -0
- ksadk/studio/cloud.py +278 -0
- ksadk/studio/codex_agent_service.py +583 -0
- ksadk/studio/codex_builder.py +324 -0
- ksadk/studio/codex_manifest.py +278 -0
- ksadk/studio/codex_run.py +352 -0
- ksadk/studio/compiler.py +189 -0
- ksadk/studio/contracts.py +644 -0
- ksadk/studio/errors.py +47 -0
- ksadk/studio/evaluation.py +192 -0
- ksadk/studio/event_store.py +284 -0
- ksadk/studio/framework_run.py +114 -0
- ksadk/studio/identifiers.py +33 -0
- ksadk/studio/mcp_runtime.py +229 -0
- ksadk/studio/model_client.py +543 -0
- ksadk/studio/model_profile_service.py +214 -0
- ksadk/studio/operations.py +243 -0
- ksadk/studio/otel_trace.py +754 -0
- ksadk/studio/pagination.py +182 -0
- ksadk/studio/python_tool_inspection.py +183 -0
- ksadk/studio/repository.py +306 -0
- ksadk/studio/resource_catalog.py +1217 -0
- ksadk/studio/run_service.py +707 -0
- ksadk/studio/runtime_catalog.py +74 -0
- ksadk/studio/runtime_source.py +339 -0
- ksadk/studio/service.py +1141 -0
- ksadk/studio/shared_web.py +994 -0
- ksadk/studio/skill_discovery.py +628 -0
- ksadk/studio/static/assets/PrismRenderer-IYN-ffww.js +3 -0
- ksadk/studio/static/assets/index-CaEN0eTR.css +1 -0
- ksadk/studio/static/assets/index-DI2eY_NH.js +252 -0
- ksadk/studio/static/assets/react-vendor-BNmTiJ-I.js +9 -0
- ksadk/studio/static/assets/rolldown-runtime-hePW80VL.js +1 -0
- ksadk/studio/static/index.html +15 -0
- ksadk/studio/templates.py +630 -0
- ksadk/studio/validator.py +246 -0
- ksadk/studio/workspace.py +137 -0
- ksadk/terminal_exec_policy.py +3 -4
- ksadk/tools/gateway.py +101 -15
- ksadk/tools/result_budget.py +12 -4
- ksadk/toolsets/__init__.py +161 -37
- ksadk/toolsets/a2a.py +426 -0
- ksadk/toolsets/platform.py +21 -5
- ksadk/toolsets/sandbox.py +82 -16
- ksadk/toolsets/skills.py +200 -39
- ksadk/toolsets/web.py +123 -26
- ksadk/toolsets/workspace.py +212 -101
- ksadk/tracing/__init__.py +19 -21
- ksadk/tracing/exporters/inmemory_exporter.py +62 -51
- ksadk/tracing/setup.py +135 -506
- ksadk/tracing/span_utils.py +0 -41
- ksadk/tui/loop.py +78 -30
- ksadk/tui/stream_render.py +1 -0
- ksadk/ui_config.py +5 -3
- ksadk/version.py +1 -1
- ksadk_runtime_common/memory_backend/registry.py +56 -56
- ksadk_runtime_common/schemas/runtime_event_v1.json +95 -0
- ksadk_runtime_common/workspace_files/router.py +8 -2
- agentengine_sdk_python-0.7.0.dist-info/RECORD +0 -482
- ksadk/a2a/card_builder.py +0 -64
- ksadk/a2a/client.py +0 -309
- ksadk/runners/utils/langfuse.py +0 -230
- ksadk/server/static/assets/CodeBlock-P16VOLYi.js +0 -9
- ksadk/server/static/assets/MathMessageMarkdown-CdGiQWHR.js +0 -8
- ksadk/server/static/assets/MermaidBlock-IOcal8cm.js +0 -303
- ksadk/server/static/assets/NativeTerminalPanel-DGSDWsei.js +0 -1
- ksadk/server/static/assets/architecture-7EHR7CIX-BvUlsA6l.js +0 -1
- ksadk/server/static/assets/channel-CTuydARX.js +0 -1
- ksadk/server/static/assets/chunk-QZHKN3VN-BnCydivS.js +0 -1
- ksadk/server/static/assets/classDiagram-4FO5ZUOK-BNHg45Be.js +0 -1
- ksadk/server/static/assets/classDiagram-v2-Q7XG4LA2-BNHg45Be.js +0 -1
- ksadk/server/static/assets/eventmodeling-FCH6USID-DsAgWnlf.js +0 -1
- ksadk/server/static/assets/gitGraph-WXDBUCRP-BIao9Yjg.js +0 -1
- ksadk/server/static/assets/index-C00Lwg3L.js +0 -165
- ksadk/server/static/assets/index-DAURbCsP.css +0 -1
- ksadk/server/static/assets/info-J43DQDTF-CyipqGxU.js +0 -1
- ksadk/server/static/assets/packet-YPE3B663-CGkyRxlg.js +0 -1
- ksadk/server/static/assets/pie-LRSECV5Y-M0uWAf4n.js +0 -1
- ksadk/server/static/assets/radar-GUYGQ44K-Bdbm24-x.js +0 -1
- ksadk/server/static/assets/stateDiagram-v2-BHNVJYJU-8xBN_gLM.js +0 -1
- ksadk/server/static/assets/treeView-BLDUP644-npMo0ak1.js +0 -1
- ksadk/server/static/assets/treemap-LRROVOQU-CHdzrrdo.js +0 -1
- ksadk/server/static/assets/wardley-L42UT6IY-BEQz7wyy.js +0 -1
- ksadk/tracing/exporters/langfuse_exporter.py +0 -445
- {agentengine_sdk_python-0.7.0.dist-info → agentengine_sdk_python-0.8.1.dist-info}/entry_points.txt +0 -0
- {agentengine_sdk_python-0.7.0.dist-info → agentengine_sdk_python-0.8.1.dist-info}/licenses/LICENSE +0 -0
- {agentengine_sdk_python-0.7.0.dist-info → agentengine_sdk_python-0.8.1.dist-info}/top_level.txt +0 -0
ksadk/tracing/setup.py
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
"""
|
|
2
|
-
Tracing 初始化 -
|
|
2
|
+
Tracing 初始化 - 标准 OTLP 双写(Langfuse 主 + CloudMonitor 次)
|
|
3
|
+
|
|
4
|
+
每个 Python Agent 进程一个 TracerProvider,挂两个 OTLP/HTTP protobuf BatchSpanProcessor,
|
|
5
|
+
消费同一批 ReadableSpan。不在 exporter 阶段按目的地改写 span。
|
|
6
|
+
|
|
3
7
|
支持 ADK 自动插桩 via openinference-instrumentation-google-adk
|
|
4
8
|
"""
|
|
5
9
|
|
|
6
10
|
import atexit
|
|
7
|
-
import
|
|
11
|
+
import importlib
|
|
8
12
|
import logging
|
|
9
13
|
import os
|
|
10
14
|
from dataclasses import dataclass
|
|
11
15
|
from typing import Any, Optional
|
|
12
16
|
from urllib.parse import unquote
|
|
13
17
|
|
|
18
|
+
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult
|
|
19
|
+
|
|
14
20
|
from ksadk.tracing.exporters.inmemory_exporter import InMemoryExporter
|
|
15
21
|
|
|
16
22
|
logger = logging.getLogger(__name__)
|
|
@@ -36,9 +42,9 @@ def _batch_span_processor_kwargs() -> dict:
|
|
|
36
42
|
|
|
37
43
|
|
|
38
44
|
_exporter_instance: Optional[InMemoryExporter] = None
|
|
39
|
-
_langfuse_exporter: Optional[Any] = None
|
|
40
45
|
_tracing_initialized: bool = False
|
|
41
46
|
_adk_instrumented: bool = False
|
|
47
|
+
_managed_span_processors: list[Any] = []
|
|
42
48
|
|
|
43
49
|
|
|
44
50
|
@dataclass(frozen=True)
|
|
@@ -49,28 +55,26 @@ class _OtlpHttpConfig:
|
|
|
49
55
|
service_name: str
|
|
50
56
|
|
|
51
57
|
|
|
52
|
-
class _LoggingSpanExporter:
|
|
53
|
-
"""
|
|
58
|
+
class _LoggingSpanExporter(SpanExporter):
|
|
59
|
+
"""Delegating exporter that logs external trace export flow."""
|
|
54
60
|
|
|
55
61
|
def __init__(
|
|
56
62
|
self,
|
|
57
|
-
exporter:
|
|
63
|
+
exporter: SpanExporter,
|
|
58
64
|
*,
|
|
59
65
|
name: str,
|
|
60
66
|
endpoint: str,
|
|
61
67
|
service_name: str,
|
|
62
68
|
header_keys: list[str],
|
|
63
|
-
span_transform: Any = None,
|
|
64
69
|
):
|
|
65
70
|
self._exporter = exporter
|
|
66
71
|
self._name = name
|
|
67
72
|
self._endpoint = endpoint
|
|
68
73
|
self._service_name = service_name
|
|
69
74
|
self._header_keys = header_keys
|
|
70
|
-
self._span_transform = span_transform
|
|
71
75
|
|
|
72
|
-
def export(self, spans):
|
|
73
|
-
span_count = len(spans)
|
|
76
|
+
def export(self, spans) -> SpanExportResult:
|
|
77
|
+
span_count = len(spans)
|
|
74
78
|
logger.info(
|
|
75
79
|
"%s export started: endpoint=%s service_name=%s spans=%s headers=%s",
|
|
76
80
|
self._name,
|
|
@@ -80,8 +84,7 @@ class _LoggingSpanExporter:
|
|
|
80
84
|
",".join(self._header_keys),
|
|
81
85
|
)
|
|
82
86
|
try:
|
|
83
|
-
|
|
84
|
-
result = self._exporter.export(export_spans)
|
|
87
|
+
result = self._exporter.export(spans)
|
|
85
88
|
except Exception:
|
|
86
89
|
logger.exception(
|
|
87
90
|
"%s export failed: endpoint=%s service_name=%s spans=%s",
|
|
@@ -101,282 +104,18 @@ class _LoggingSpanExporter:
|
|
|
101
104
|
)
|
|
102
105
|
return result
|
|
103
106
|
|
|
104
|
-
def shutdown(self):
|
|
105
|
-
shutdown = getattr(self._exporter, "shutdown", None)
|
|
106
|
-
if shutdown is None:
|
|
107
|
-
return None
|
|
107
|
+
def shutdown(self) -> None:
|
|
108
108
|
logger.info("%s exporter shutdown: endpoint=%s", self._name, self._endpoint)
|
|
109
|
-
|
|
109
|
+
self._exporter.shutdown()
|
|
110
110
|
|
|
111
111
|
def force_flush(self, timeout_millis: int = 30000) -> bool:
|
|
112
|
-
force_flush = getattr(self._exporter, "force_flush", None)
|
|
113
|
-
if force_flush is None:
|
|
114
|
-
return True
|
|
115
112
|
logger.info(
|
|
116
113
|
"%s exporter force_flush: endpoint=%s timeout_millis=%s",
|
|
117
114
|
self._name,
|
|
118
115
|
self._endpoint,
|
|
119
116
|
timeout_millis,
|
|
120
117
|
)
|
|
121
|
-
return force_flush(timeout_millis)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
def _coerce_int(value: Any) -> int:
|
|
125
|
-
if value is None or isinstance(value, bool):
|
|
126
|
-
return 0
|
|
127
|
-
try:
|
|
128
|
-
return int(value)
|
|
129
|
-
except (TypeError, ValueError):
|
|
130
|
-
try:
|
|
131
|
-
return int(float(value))
|
|
132
|
-
except (TypeError, ValueError):
|
|
133
|
-
return 0
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
def _span_context(span: Any) -> Any:
|
|
137
|
-
context = getattr(span, "context", None)
|
|
138
|
-
if context is not None:
|
|
139
|
-
return context
|
|
140
|
-
get_context = getattr(span, "get_span_context", None)
|
|
141
|
-
if callable(get_context):
|
|
142
|
-
try:
|
|
143
|
-
return get_context()
|
|
144
|
-
except Exception:
|
|
145
|
-
return None
|
|
146
|
-
return None
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
def _span_id(span: Any) -> Optional[int]:
|
|
150
|
-
return getattr(_span_context(span), "span_id", None)
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
def _trace_id(span: Any) -> Optional[int]:
|
|
154
|
-
return getattr(_span_context(span), "trace_id", None)
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
def _parent_span_id(span: Any) -> Optional[int]:
|
|
158
|
-
return getattr(getattr(span, "parent", None), "span_id", None)
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
def _span_token_usage(span: Any) -> dict[str, int]:
|
|
162
|
-
attributes = getattr(span, "attributes", None) or {}
|
|
163
|
-
return {
|
|
164
|
-
"input": _coerce_int(attributes.get("gen_ai.usage.input_tokens")),
|
|
165
|
-
"output": _coerce_int(attributes.get("gen_ai.usage.output_tokens")),
|
|
166
|
-
"cache_read": _coerce_int(attributes.get("gen_ai.usage.cache_read.input_tokens")),
|
|
167
|
-
"reasoning_output": _coerce_int(
|
|
168
|
-
attributes.get("gen_ai.usage.reasoning.output_tokens")
|
|
169
|
-
),
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
def _has_nonzero_token_usage(usage: dict[str, int]) -> bool:
|
|
174
|
-
return any(value > 0 for value in usage.values())
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
def _instrumentation_scope_name(span: Any) -> str:
|
|
178
|
-
scope = getattr(span, "instrumentation_scope", None)
|
|
179
|
-
name = getattr(scope, "name", None)
|
|
180
|
-
if name:
|
|
181
|
-
return str(name)
|
|
182
|
-
info = getattr(span, "instrumentation_info", None)
|
|
183
|
-
name = getattr(info, "name", None)
|
|
184
|
-
return str(name or "")
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
def _clone_span_with_attributes(span: Any, attributes: dict[str, Any]) -> Any:
|
|
188
|
-
try:
|
|
189
|
-
from opentelemetry.sdk.trace import ReadableSpan
|
|
190
|
-
|
|
191
|
-
return ReadableSpan(
|
|
192
|
-
name=span.name,
|
|
193
|
-
context=span.context,
|
|
194
|
-
parent=span.parent,
|
|
195
|
-
resource=span.resource,
|
|
196
|
-
attributes=attributes,
|
|
197
|
-
events=span.events,
|
|
198
|
-
links=span.links,
|
|
199
|
-
kind=span.kind,
|
|
200
|
-
instrumentation_info=getattr(span, "instrumentation_info", None),
|
|
201
|
-
status=span.status,
|
|
202
|
-
start_time=span.start_time,
|
|
203
|
-
end_time=span.end_time,
|
|
204
|
-
instrumentation_scope=getattr(span, "instrumentation_scope", None),
|
|
205
|
-
)
|
|
206
|
-
except Exception:
|
|
207
|
-
logger.debug("Failed to clone span for CloudMonitor token compatibility", exc_info=True)
|
|
208
|
-
return span
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
def _prepare_langfuse_spans(spans: Any) -> Any:
|
|
212
|
-
"""Keep ksadk response usage authoritative for Langfuse exports.
|
|
213
|
-
|
|
214
|
-
OpenInference LangChain instrumentation can emit llm.token_count.* on nested
|
|
215
|
-
ChatOpenAI spans. In LangGraph streaming these counts can be cumulative and
|
|
216
|
-
disagree with the usage returned by ksadk to RunAgent/session state. When a
|
|
217
|
-
trace already has a ksadk.conversations span with gen_ai.usage.*, strip token
|
|
218
|
-
attrs from nested OpenInference LangChain spans before exporting to
|
|
219
|
-
Langfuse. The child spans remain useful for timing and structure, but cannot
|
|
220
|
-
pollute token accounting.
|
|
221
|
-
"""
|
|
222
|
-
if not spans:
|
|
223
|
-
return spans
|
|
224
|
-
span_list = list(spans)
|
|
225
|
-
authoritative_trace_ids = {
|
|
226
|
-
_trace_id(span)
|
|
227
|
-
for span in span_list
|
|
228
|
-
if _trace_id(span) is not None
|
|
229
|
-
and _instrumentation_scope_name(span) == "ksadk.conversations"
|
|
230
|
-
and _has_nonzero_token_usage(_span_token_usage(span))
|
|
231
|
-
}
|
|
232
|
-
if not authoritative_trace_ids:
|
|
233
|
-
return spans
|
|
234
|
-
|
|
235
|
-
strip_prefixes = ("llm.token_count.", "llm.usage.", "gen_ai.usage.")
|
|
236
|
-
replacements: dict[int, Any] = {}
|
|
237
|
-
for span in span_list:
|
|
238
|
-
if _trace_id(span) not in authoritative_trace_ids:
|
|
239
|
-
continue
|
|
240
|
-
if _instrumentation_scope_name(span) != "openinference.instrumentation.langchain":
|
|
241
|
-
continue
|
|
242
|
-
attributes = dict(getattr(span, "attributes", None) or {})
|
|
243
|
-
stripped = {
|
|
244
|
-
key: value
|
|
245
|
-
for key, value in attributes.items()
|
|
246
|
-
if not any(str(key).startswith(prefix) for prefix in strip_prefixes)
|
|
247
|
-
}
|
|
248
|
-
if len(stripped) != len(attributes):
|
|
249
|
-
if str(stripped.get("openinference.span.kind") or "").upper() == "LLM":
|
|
250
|
-
stripped["openinference.span.kind"] = "CHAIN"
|
|
251
|
-
replacements[id(span)] = _clone_span_with_attributes(span, stripped)
|
|
252
|
-
|
|
253
|
-
if not replacements:
|
|
254
|
-
return spans
|
|
255
|
-
return [replacements.get(id(span), span) for span in span_list]
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
def _prepare_cloud_monitor_spans(spans: Any) -> Any:
|
|
259
|
-
"""Add root token rollups for CloudMonitor without changing other exporters.
|
|
260
|
-
|
|
261
|
-
CloudMonitor already parses gen_ai.usage.* on LLM child spans, but its root-span
|
|
262
|
-
and overview APIs currently use root-level token fields. ADK/OpenInference often
|
|
263
|
-
emits token usage on nested LLM spans only, and sometimes duplicates the same LLM
|
|
264
|
-
usage on a parent and a child span. To avoid double counting, aggregate only
|
|
265
|
-
token-bearing leaf spans and copy the result to the root span for this exporter.
|
|
266
|
-
"""
|
|
267
|
-
if not spans:
|
|
268
|
-
return spans
|
|
269
|
-
span_list = list(spans)
|
|
270
|
-
span_ids = {_span_id(span) for span in span_list if _span_id(span) is not None}
|
|
271
|
-
if not span_ids:
|
|
272
|
-
return spans
|
|
273
|
-
|
|
274
|
-
children_by_parent: dict[tuple[Optional[int], int], list[Any]] = {}
|
|
275
|
-
roots: list[Any] = []
|
|
276
|
-
for span in span_list:
|
|
277
|
-
sid = _span_id(span)
|
|
278
|
-
if sid is None:
|
|
279
|
-
continue
|
|
280
|
-
parent_id = _parent_span_id(span)
|
|
281
|
-
trace_id = _trace_id(span)
|
|
282
|
-
if parent_id is None or parent_id not in span_ids:
|
|
283
|
-
roots.append(span)
|
|
284
|
-
else:
|
|
285
|
-
children_by_parent.setdefault((trace_id, parent_id), []).append(span)
|
|
286
|
-
|
|
287
|
-
token_usage_by_id = {
|
|
288
|
-
(_trace_id(span), _span_id(span)): _span_token_usage(span)
|
|
289
|
-
for span in span_list
|
|
290
|
-
if _span_id(span) is not None
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
has_token_descendant_cache: dict[tuple[Optional[int], int], bool] = {}
|
|
294
|
-
|
|
295
|
-
def has_token_descendant(span: Any) -> bool:
|
|
296
|
-
sid = _span_id(span)
|
|
297
|
-
key = (_trace_id(span), sid)
|
|
298
|
-
if sid is None:
|
|
299
|
-
return False
|
|
300
|
-
if key in has_token_descendant_cache:
|
|
301
|
-
return has_token_descendant_cache[key]
|
|
302
|
-
for child in children_by_parent.get(key, []):
|
|
303
|
-
child_key = (_trace_id(child), _span_id(child))
|
|
304
|
-
if _has_nonzero_token_usage(token_usage_by_id.get(child_key, {})):
|
|
305
|
-
has_token_descendant_cache[key] = True
|
|
306
|
-
return True
|
|
307
|
-
if has_token_descendant(child):
|
|
308
|
-
has_token_descendant_cache[key] = True
|
|
309
|
-
return True
|
|
310
|
-
has_token_descendant_cache[key] = False
|
|
311
|
-
return False
|
|
312
|
-
|
|
313
|
-
def collect_leaf_usage(span: Any, usage: dict[str, int]) -> None:
|
|
314
|
-
sid = _span_id(span)
|
|
315
|
-
key = (_trace_id(span), sid)
|
|
316
|
-
own_usage = token_usage_by_id.get(key, {})
|
|
317
|
-
if _has_nonzero_token_usage(own_usage) and not has_token_descendant(span):
|
|
318
|
-
for usage_key, value in own_usage.items():
|
|
319
|
-
usage[usage_key] += value
|
|
320
|
-
return
|
|
321
|
-
for child in children_by_parent.get(key, []):
|
|
322
|
-
collect_leaf_usage(child, usage)
|
|
323
|
-
|
|
324
|
-
replacements: dict[int, Any] = {}
|
|
325
|
-
for root in roots:
|
|
326
|
-
aggregate = {"input": 0, "output": 0, "cache_read": 0, "reasoning_output": 0}
|
|
327
|
-
collect_leaf_usage(root, aggregate)
|
|
328
|
-
if not _has_nonzero_token_usage(aggregate):
|
|
329
|
-
continue
|
|
330
|
-
|
|
331
|
-
attributes = dict(getattr(root, "attributes", None) or {})
|
|
332
|
-
updated = False
|
|
333
|
-
attr_map = {
|
|
334
|
-
"gen_ai.usage.input_tokens": "input",
|
|
335
|
-
"gen_ai.usage.output_tokens": "output",
|
|
336
|
-
"gen_ai.usage.cache_read.input_tokens": "cache_read",
|
|
337
|
-
"gen_ai.usage.reasoning.output_tokens": "reasoning_output",
|
|
338
|
-
}
|
|
339
|
-
for attr_name, usage_key in attr_map.items():
|
|
340
|
-
if _coerce_int(attributes.get(attr_name)) <= 0 and aggregate[usage_key] > 0:
|
|
341
|
-
attributes[attr_name] = aggregate[usage_key]
|
|
342
|
-
updated = True
|
|
343
|
-
|
|
344
|
-
if updated:
|
|
345
|
-
replacements[id(root)] = _clone_span_with_attributes(root, attributes)
|
|
346
|
-
|
|
347
|
-
if not replacements:
|
|
348
|
-
return spans
|
|
349
|
-
return [replacements.get(id(span), span) for span in span_list]
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
def _build_langfuse_otlp_config(langfuse_config: dict = None) -> Optional[dict]:
|
|
353
|
-
"""Build Langfuse OTLP direct exporter config from explicit config or env."""
|
|
354
|
-
if langfuse_config:
|
|
355
|
-
public_key = langfuse_config.get("public_key") or ""
|
|
356
|
-
secret_key = langfuse_config.get("secret_key") or ""
|
|
357
|
-
host = langfuse_config.get("host") or "http://localhost:3000"
|
|
358
|
-
else:
|
|
359
|
-
public_key = os.getenv("LANGFUSE_PUBLIC_KEY", "")
|
|
360
|
-
secret_key = os.getenv("LANGFUSE_SECRET_KEY", "")
|
|
361
|
-
host = os.getenv("LANGFUSE_BASE_URL") or os.getenv("LANGFUSE_HOST") or "http://localhost:3000"
|
|
362
|
-
|
|
363
|
-
if not public_key or not secret_key:
|
|
364
|
-
return None
|
|
365
|
-
|
|
366
|
-
headers = _build_langfuse_otlp_headers(public_key, secret_key)
|
|
367
|
-
return {
|
|
368
|
-
"endpoint": f"{host.rstrip('/')}/api/public/otel/v1/traces",
|
|
369
|
-
"headers": headers,
|
|
370
|
-
"protocol": "http/protobuf",
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
def _build_langfuse_otlp_headers(public_key: str, secret_key: str) -> dict[str, str]:
|
|
375
|
-
auth = base64.b64encode(f"{public_key}:{secret_key}".encode("utf-8")).decode("ascii")
|
|
376
|
-
return {
|
|
377
|
-
"Authorization": f"Basic {auth}",
|
|
378
|
-
"x-langfuse-ingestion-version": "4",
|
|
379
|
-
}
|
|
118
|
+
return self._exporter.force_flush(timeout_millis)
|
|
380
119
|
|
|
381
120
|
|
|
382
121
|
def _parse_otlp_headers(raw: str) -> dict[str, str]:
|
|
@@ -402,7 +141,7 @@ def _decode_otlp_header_value(key: str, value: str) -> str:
|
|
|
402
141
|
for scheme in ("Basic", "Bearer"):
|
|
403
142
|
prefix = f"{scheme}+"
|
|
404
143
|
if decoded.startswith(prefix):
|
|
405
|
-
return f"{scheme} {decoded[len(prefix):]}"
|
|
144
|
+
return f"{scheme} {decoded[len(prefix) :]}"
|
|
406
145
|
return decoded
|
|
407
146
|
|
|
408
147
|
|
|
@@ -414,10 +153,6 @@ def _derive_otlp_traces_endpoint(endpoint: str) -> str:
|
|
|
414
153
|
return f"{endpoint}/v1/traces"
|
|
415
154
|
|
|
416
155
|
|
|
417
|
-
def _env_flag_disabled(value: str) -> bool:
|
|
418
|
-
return value.strip().lower() in {"0", "false", "no", "off", "disabled"}
|
|
419
|
-
|
|
420
|
-
|
|
421
156
|
def _env_flag_enabled(value: str) -> bool:
|
|
422
157
|
return value.strip().lower() in {"1", "true", "yes", "on"}
|
|
423
158
|
|
|
@@ -443,53 +178,48 @@ def _get_service_name() -> str:
|
|
|
443
178
|
|
|
444
179
|
|
|
445
180
|
def _build_cloud_monitor_otlp_http_config() -> Optional[_OtlpHttpConfig]:
|
|
446
|
-
"""Build CloudMonitor OTLP HTTP traces exporter config from
|
|
447
|
-
enabled = os.getenv("CLOUD_MONITOR_OTLP_ENABLED", "").strip()
|
|
448
|
-
if enabled and _env_flag_disabled(enabled):
|
|
449
|
-
logger.info("CloudMonitor OTLP exporter disabled by CLOUD_MONITOR_OTLP_ENABLED=%s", enabled)
|
|
450
|
-
return None
|
|
451
|
-
cloud_monitor_langfuse_configured = all(
|
|
452
|
-
os.getenv(name, "").strip()
|
|
453
|
-
for name in (
|
|
454
|
-
"CLOUD_MONITOR_LANGFUSE_PUBLIC_KEY",
|
|
455
|
-
"CLOUD_MONITOR_LANGFUSE_SECRET_KEY",
|
|
456
|
-
)
|
|
457
|
-
) and bool(
|
|
458
|
-
os.getenv("CLOUD_MONITOR_LANGFUSE_HOST", "").strip()
|
|
459
|
-
or os.getenv("CLOUD_MONITOR_OTLP_ENDPOINT", "").strip()
|
|
460
|
-
)
|
|
461
|
-
cloud_monitor_otlp_forced = _env_flag_enabled(enabled)
|
|
462
|
-
cloud_monitor_callback_requested = _env_flag_enabled(
|
|
463
|
-
os.getenv("LANGFUSE_USE_CALLBACK", "")
|
|
464
|
-
) and not _env_flag_disabled(os.getenv("CLOUD_MONITOR_LANGFUSE_ENABLED", ""))
|
|
465
|
-
if (
|
|
466
|
-
cloud_monitor_langfuse_configured
|
|
467
|
-
and cloud_monitor_callback_requested
|
|
468
|
-
and not cloud_monitor_otlp_forced
|
|
469
|
-
):
|
|
470
|
-
logger.info(
|
|
471
|
-
"CloudMonitor OTLP exporter skipped because CloudMonitor Langfuse SDK "
|
|
472
|
-
"callback is requested"
|
|
473
|
-
)
|
|
474
|
-
return None
|
|
181
|
+
"""Build CloudMonitor OTLP HTTP traces exporter config from CLOUD_MONITOR_OTLP_* env.
|
|
475
182
|
|
|
476
|
-
|
|
183
|
+
traces 专用 headers 优先于通用 headers。仅当两个 headers 环境变量整体缺失时,
|
|
184
|
+
才把旧 CLOUD_MONITOR_APP_KEY 按过渡兼容翻译为 Ksc-Appkey header。
|
|
185
|
+
"""
|
|
477
186
|
traces_endpoint = os.getenv("CLOUD_MONITOR_OTLP_TRACES_ENDPOINT", "").strip()
|
|
478
187
|
base_endpoint = os.getenv("CLOUD_MONITOR_OTLP_ENDPOINT", "").strip()
|
|
479
188
|
endpoint = traces_endpoint
|
|
480
189
|
if not endpoint and base_endpoint:
|
|
481
190
|
endpoint = _derive_otlp_traces_endpoint(base_endpoint)
|
|
482
191
|
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
192
|
+
raw_traces_headers = os.getenv("CLOUD_MONITOR_OTLP_TRACES_HEADERS", "")
|
|
193
|
+
raw_generic_headers = os.getenv("CLOUD_MONITOR_OTLP_HEADERS", "")
|
|
194
|
+
raw_headers = raw_traces_headers.strip() or raw_generic_headers
|
|
195
|
+
headers = _parse_otlp_headers(raw_headers)
|
|
196
|
+
# One-version compatibility applies only when both header variables are
|
|
197
|
+
# absent. A present-but-invalid traces or generic header bundle must fail
|
|
198
|
+
# closed rather than silently mixing it with the legacy AppKey.
|
|
199
|
+
if not raw_traces_headers.strip() and not raw_generic_headers.strip():
|
|
200
|
+
app_key = os.getenv("CLOUD_MONITOR_APP_KEY", "").strip()
|
|
201
|
+
if app_key:
|
|
202
|
+
if not endpoint:
|
|
203
|
+
logger.warning(
|
|
204
|
+
"CloudMonitor OTLP exporter skipped: CLOUD_MONITOR_APP_KEY fallback "
|
|
205
|
+
"requires CLOUD_MONITOR_OTLP_ENDPOINT or CLOUD_MONITOR_OTLP_TRACES_ENDPOINT"
|
|
206
|
+
)
|
|
207
|
+
return None
|
|
208
|
+
headers["Ksc-Appkey"] = app_key
|
|
209
|
+
logger.info(
|
|
210
|
+
"CloudMonitor OTLP using deprecated CLOUD_MONITOR_APP_KEY -> Ksc-Appkey fallback; "
|
|
211
|
+
"server should inject CLOUD_MONITOR_OTLP_TRACES_HEADERS or "
|
|
212
|
+
"CLOUD_MONITOR_OTLP_HEADERS instead"
|
|
213
|
+
)
|
|
214
|
+
|
|
489
215
|
if not endpoint:
|
|
216
|
+
logger.debug("CloudMonitor OTLP exporter not configured: missing endpoint")
|
|
217
|
+
return None
|
|
218
|
+
if not _has_header(headers, "Ksc-Appkey"):
|
|
490
219
|
logger.warning(
|
|
491
|
-
"CloudMonitor OTLP exporter skipped:
|
|
492
|
-
"
|
|
220
|
+
"CloudMonitor OTLP exporter skipped: Ksc-Appkey missing in "
|
|
221
|
+
"CLOUD_MONITOR_OTLP_TRACES_HEADERS/CLOUD_MONITOR_OTLP_HEADERS and no "
|
|
222
|
+
"CLOUD_MONITOR_APP_KEY fallback"
|
|
493
223
|
)
|
|
494
224
|
return None
|
|
495
225
|
|
|
@@ -505,16 +235,12 @@ def _build_cloud_monitor_otlp_http_config() -> Optional[_OtlpHttpConfig]:
|
|
|
505
235
|
)
|
|
506
236
|
return None
|
|
507
237
|
|
|
508
|
-
headers = _parse_otlp_headers(os.getenv("CLOUD_MONITOR_OTLP_HEADERS", ""))
|
|
509
|
-
headers["Ksc-Appkey"] = app_key
|
|
510
238
|
service_name = _get_service_name()
|
|
511
239
|
logger.info(
|
|
512
|
-
"CloudMonitor OTLP config resolved: endpoint=%s protocol=%s service_name=%s "
|
|
513
|
-
"app_key_present=%s header_keys=%s",
|
|
240
|
+
"CloudMonitor OTLP config resolved: endpoint=%s protocol=%s service_name=%s header_keys=%s",
|
|
514
241
|
endpoint,
|
|
515
242
|
protocol,
|
|
516
243
|
service_name or "",
|
|
517
|
-
bool(app_key),
|
|
518
244
|
",".join(sorted(headers)),
|
|
519
245
|
)
|
|
520
246
|
return _OtlpHttpConfig(
|
|
@@ -525,50 +251,12 @@ def _build_cloud_monitor_otlp_http_config() -> Optional[_OtlpHttpConfig]:
|
|
|
525
251
|
)
|
|
526
252
|
|
|
527
253
|
|
|
528
|
-
def _is_langfuse_otlp_endpoint(endpoint: str) -> bool:
|
|
529
|
-
if not endpoint:
|
|
530
|
-
return False
|
|
531
|
-
normalized_endpoint = endpoint.strip().rstrip("/")
|
|
532
|
-
langfuse_hosts = [
|
|
533
|
-
os.getenv("LANGFUSE_HOST", "").strip().rstrip("/"),
|
|
534
|
-
os.getenv("LANGFUSE_BASE_URL", "").strip().rstrip("/"),
|
|
535
|
-
]
|
|
536
|
-
if any(host and normalized_endpoint.startswith(host) for host in langfuse_hosts):
|
|
537
|
-
return True
|
|
538
|
-
return "/api/public/otel" in normalized_endpoint
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
def _is_langfuse_callback_endpoint(endpoint: str) -> bool:
|
|
542
|
-
return _env_flag_enabled(os.getenv("LANGFUSE_USE_CALLBACK", "")) and _is_langfuse_otlp_endpoint(
|
|
543
|
-
endpoint
|
|
544
|
-
)
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
def _apply_langfuse_auth_fallback(endpoint: str, headers: dict[str, str]) -> bool:
|
|
548
|
-
"""Add Langfuse OTLP auth when the platform provides endpoint + keys but no headers."""
|
|
549
|
-
if not _is_langfuse_otlp_endpoint(endpoint) or _has_header(headers, "Authorization"):
|
|
550
|
-
return False
|
|
551
|
-
|
|
552
|
-
public_key = os.getenv("LANGFUSE_PUBLIC_KEY", "").strip()
|
|
553
|
-
secret_key = os.getenv("LANGFUSE_SECRET_KEY", "").strip()
|
|
554
|
-
if not public_key or not secret_key:
|
|
555
|
-
logger.warning(
|
|
556
|
-
"Generic OTLP endpoint looks like Langfuse but LANGFUSE_PUBLIC_KEY/SECRET_KEY "
|
|
557
|
-
"are missing; Authorization header was not added"
|
|
558
|
-
)
|
|
559
|
-
return False
|
|
560
|
-
|
|
561
|
-
langfuse_headers = _build_langfuse_otlp_headers(public_key, secret_key)
|
|
562
|
-
headers["Authorization"] = langfuse_headers["Authorization"]
|
|
563
|
-
if not _has_header(headers, "x-langfuse-ingestion-version"):
|
|
564
|
-
headers["x-langfuse-ingestion-version"] = langfuse_headers[
|
|
565
|
-
"x-langfuse-ingestion-version"
|
|
566
|
-
]
|
|
567
|
-
return True
|
|
568
|
-
|
|
569
|
-
|
|
570
254
|
def _build_generic_otlp_http_config() -> Optional[dict]:
|
|
571
|
-
"""Build generic OTLP HTTP traces exporter config from standard OTEL env.
|
|
255
|
+
"""Build generic OTLP HTTP traces exporter config from standard OTEL env.
|
|
256
|
+
|
|
257
|
+
This path consumes only the standard OTEL_EXPORTER_OTLP_* contract. Backend
|
|
258
|
+
identity and authentication are opaque to KsADK.
|
|
259
|
+
"""
|
|
572
260
|
traces_endpoint = os.getenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", "").strip()
|
|
573
261
|
base_endpoint = os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT", "").strip()
|
|
574
262
|
endpoint = traces_endpoint
|
|
@@ -576,13 +264,6 @@ def _build_generic_otlp_http_config() -> Optional[dict]:
|
|
|
576
264
|
endpoint = _derive_otlp_traces_endpoint(base_endpoint)
|
|
577
265
|
if not endpoint:
|
|
578
266
|
return None
|
|
579
|
-
if _is_langfuse_callback_endpoint(endpoint):
|
|
580
|
-
logger.info(
|
|
581
|
-
"Generic OTLP HTTP exporter skipped because LANGFUSE_USE_CALLBACK is enabled "
|
|
582
|
-
"for endpoint=%s",
|
|
583
|
-
endpoint,
|
|
584
|
-
)
|
|
585
|
-
return None
|
|
586
267
|
|
|
587
268
|
protocol = (
|
|
588
269
|
os.getenv("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL", "").strip().lower()
|
|
@@ -595,58 +276,62 @@ def _build_generic_otlp_http_config() -> Optional[dict]:
|
|
|
595
276
|
)
|
|
596
277
|
return None
|
|
597
278
|
|
|
598
|
-
raw_headers = (
|
|
599
|
-
|
|
600
|
-
or os.getenv("OTEL_EXPORTER_OTLP_HEADERS", "")
|
|
279
|
+
raw_headers = os.getenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS", "").strip() or os.getenv(
|
|
280
|
+
"OTEL_EXPORTER_OTLP_HEADERS", ""
|
|
601
281
|
)
|
|
602
282
|
headers = _parse_otlp_headers(raw_headers)
|
|
603
|
-
langfuse_auth_fallback = _apply_langfuse_auth_fallback(endpoint, headers)
|
|
604
283
|
logger.info(
|
|
605
|
-
"Generic OTLP HTTP config resolved: endpoint=%s protocol=http/protobuf "
|
|
606
|
-
"header_keys=%s langfuse_auth_fallback=%s",
|
|
284
|
+
"Generic OTLP HTTP config resolved: endpoint=%s protocol=http/protobuf header_keys=%s",
|
|
607
285
|
endpoint,
|
|
608
286
|
",".join(sorted(headers)),
|
|
609
|
-
langfuse_auth_fallback,
|
|
610
287
|
)
|
|
611
288
|
return {
|
|
612
289
|
"endpoint": endpoint,
|
|
613
290
|
"headers": headers,
|
|
614
291
|
"protocol": "http/protobuf",
|
|
615
|
-
"langfuse_auth_fallback": langfuse_auth_fallback,
|
|
616
292
|
}
|
|
617
293
|
|
|
618
294
|
|
|
295
|
+
def _register_span_processor(provider: Any, processor: Any) -> None:
|
|
296
|
+
provider.add_span_processor(processor)
|
|
297
|
+
_managed_span_processors.append(processor)
|
|
298
|
+
|
|
299
|
+
|
|
619
300
|
def setup_tracing(
|
|
620
301
|
enable_inmemory: bool = True,
|
|
621
|
-
enable_langfuse: bool = None,
|
|
622
|
-
langfuse_config: dict = None,
|
|
302
|
+
enable_langfuse: bool | None = None,
|
|
303
|
+
langfuse_config: dict[str, Any] | None = None,
|
|
623
304
|
enable_otlp: bool = False,
|
|
624
305
|
otlp_endpoint: str = "localhost:4317",
|
|
625
306
|
enable_adk_instrumentation: bool = True, # Auto-instrument ADK
|
|
626
|
-
|
|
627
|
-
**kwargs
|
|
307
|
+
**kwargs,
|
|
628
308
|
) -> Optional[InMemoryExporter]:
|
|
629
309
|
"""初始化 Tracing (支持多 Exporter)
|
|
630
310
|
|
|
631
311
|
Args:
|
|
632
312
|
enable_inmemory: 是否启用内存 Exporter (Web UI 使用)
|
|
633
|
-
enable_langfuse:
|
|
634
|
-
langfuse_config:
|
|
635
|
-
enable_otlp: 是否启用 OTLP Exporter
|
|
636
|
-
otlp_endpoint: OTLP 端点地址
|
|
313
|
+
enable_langfuse: 已废弃并忽略;请配置标准 OTEL_EXPORTER_OTLP_*
|
|
314
|
+
langfuse_config: 已废弃并忽略;认证信息放入标准 OTLP headers
|
|
315
|
+
enable_otlp: 是否启用 OTLP gRPC Exporter
|
|
316
|
+
otlp_endpoint: OTLP gRPC 端点地址
|
|
637
317
|
enable_adk_instrumentation: 是否启用 ADK 自动插桩
|
|
638
|
-
use_callback_only: 是否仅使用 CallbackHandler (防止 OTel 重复)
|
|
639
318
|
|
|
640
319
|
Returns:
|
|
641
320
|
InMemoryExporter 实例 (用于 Web UI 获取 traces)
|
|
642
321
|
"""
|
|
643
|
-
global _exporter_instance,
|
|
322
|
+
global _exporter_instance, _tracing_initialized, _adk_instrumented
|
|
644
323
|
|
|
645
324
|
# 防止重复初始化
|
|
646
325
|
if _tracing_initialized:
|
|
647
326
|
logger.debug("Tracing already initialized, skipping")
|
|
648
327
|
return _exporter_instance
|
|
649
328
|
|
|
329
|
+
if enable_langfuse or langfuse_config:
|
|
330
|
+
logger.warning(
|
|
331
|
+
"enable_langfuse/langfuse_config are ignored; configure the standard "
|
|
332
|
+
"OTEL_EXPORTER_OTLP_* contract instead"
|
|
333
|
+
)
|
|
334
|
+
|
|
650
335
|
try:
|
|
651
336
|
from opentelemetry import trace
|
|
652
337
|
from opentelemetry.sdk.trace import TracerProvider
|
|
@@ -657,12 +342,10 @@ def setup_tracing(
|
|
|
657
342
|
|
|
658
343
|
# 检查是否已有 TracerProvider (避免覆盖)
|
|
659
344
|
existing_provider = trace.get_tracer_provider()
|
|
660
|
-
if existing_provider and hasattr(existing_provider,
|
|
661
|
-
# 使用现有 provider,直接添加 processor
|
|
345
|
+
if existing_provider and hasattr(existing_provider, "add_span_processor"):
|
|
662
346
|
provider = existing_provider
|
|
663
347
|
logger.debug("Using existing TracerProvider")
|
|
664
348
|
else:
|
|
665
|
-
# 创建新的 TracerProvider
|
|
666
349
|
provider = TracerProvider()
|
|
667
350
|
trace.set_tracer_provider(provider)
|
|
668
351
|
logger.debug("Created new TracerProvider")
|
|
@@ -671,37 +354,31 @@ def setup_tracing(
|
|
|
671
354
|
if enable_inmemory:
|
|
672
355
|
exporter = InMemoryExporter(max_traces=kwargs.get("max_traces", 1000))
|
|
673
356
|
_exporter_instance = exporter
|
|
674
|
-
provider
|
|
357
|
+
_register_span_processor(provider, SimpleSpanProcessor(exporter))
|
|
675
358
|
logger.info("InMemory exporter enabled")
|
|
676
359
|
|
|
677
|
-
# 2. Generic OTLP HTTP exporter
|
|
678
|
-
# This keeps user code backend-agnostic; Langfuse is only one possible OTLP backend.
|
|
360
|
+
# 2. Generic OTLP HTTP exporter (标准 OTEL_* —— 托管 Agent 的 Langfuse 主路)
|
|
679
361
|
generic_otlp_config = _build_generic_otlp_http_config()
|
|
680
362
|
if generic_otlp_config:
|
|
681
363
|
try:
|
|
682
|
-
from opentelemetry.exporter.otlp.proto.http.trace_exporter import
|
|
364
|
+
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
|
|
365
|
+
OTLPSpanExporter as HttpOTLPSpanExporter,
|
|
366
|
+
)
|
|
683
367
|
|
|
684
|
-
|
|
368
|
+
raw_otlp_exporter = HttpOTLPSpanExporter(
|
|
685
369
|
endpoint=generic_otlp_config["endpoint"],
|
|
686
370
|
headers=generic_otlp_config["headers"],
|
|
687
371
|
)
|
|
688
372
|
otlp_exporter = _LoggingSpanExporter(
|
|
689
|
-
|
|
373
|
+
raw_otlp_exporter,
|
|
690
374
|
name="Generic OTLP",
|
|
691
375
|
endpoint=generic_otlp_config["endpoint"],
|
|
692
376
|
service_name=_get_service_name(),
|
|
693
377
|
header_keys=sorted(generic_otlp_config["headers"]),
|
|
694
|
-
span_transform=(
|
|
695
|
-
_prepare_langfuse_spans
|
|
696
|
-
if _is_langfuse_otlp_endpoint(generic_otlp_config["endpoint"])
|
|
697
|
-
else None
|
|
698
|
-
),
|
|
699
378
|
)
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
**_batch_span_processor_kwargs(),
|
|
704
|
-
)
|
|
379
|
+
_register_span_processor(
|
|
380
|
+
provider,
|
|
381
|
+
BatchSpanProcessor(otlp_exporter, **_batch_span_processor_kwargs()),
|
|
705
382
|
)
|
|
706
383
|
logger.info(
|
|
707
384
|
"Generic OTLP HTTP exporter enabled: %s (%s) headers=%s",
|
|
@@ -714,31 +391,28 @@ def setup_tracing(
|
|
|
714
391
|
except Exception as e:
|
|
715
392
|
logger.error(f"Failed to initialize generic OTLP HTTP exporter: {e}")
|
|
716
393
|
|
|
717
|
-
# 3. CloudMonitor OTLP HTTP exporter
|
|
718
|
-
# This uses CloudMonitor-specific env vars, so existing Langfuse / generic OTLP paths
|
|
719
|
-
# continue to behave exactly as configured by the user or platform.
|
|
394
|
+
# 3. CloudMonitor OTLP HTTP exporter (次路 —— 平台 CLOUD_MONITOR_OTLP_*)
|
|
720
395
|
cloud_monitor_config = _build_cloud_monitor_otlp_http_config()
|
|
721
396
|
if cloud_monitor_config:
|
|
722
397
|
try:
|
|
723
|
-
from opentelemetry.exporter.otlp.proto.http.trace_exporter import
|
|
398
|
+
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
|
|
399
|
+
OTLPSpanExporter as HttpOTLPSpanExporter,
|
|
400
|
+
)
|
|
724
401
|
|
|
725
|
-
|
|
402
|
+
raw_cloud_monitor_exporter = HttpOTLPSpanExporter(
|
|
726
403
|
endpoint=cloud_monitor_config.endpoint,
|
|
727
404
|
headers=cloud_monitor_config.headers,
|
|
728
405
|
)
|
|
729
406
|
cloud_monitor_exporter = _LoggingSpanExporter(
|
|
730
|
-
|
|
407
|
+
raw_cloud_monitor_exporter,
|
|
731
408
|
name="CloudMonitor OTLP",
|
|
732
409
|
endpoint=cloud_monitor_config.endpoint,
|
|
733
410
|
service_name=cloud_monitor_config.service_name,
|
|
734
411
|
header_keys=sorted(cloud_monitor_config.headers),
|
|
735
|
-
span_transform=_prepare_cloud_monitor_spans,
|
|
736
412
|
)
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
**_batch_span_processor_kwargs(),
|
|
741
|
-
)
|
|
413
|
+
_register_span_processor(
|
|
414
|
+
provider,
|
|
415
|
+
BatchSpanProcessor(cloud_monitor_exporter, **_batch_span_processor_kwargs()),
|
|
742
416
|
)
|
|
743
417
|
logger.info(
|
|
744
418
|
"CloudMonitor OTLP exporter enabled: endpoint=%s protocol=%s service_name=%s",
|
|
@@ -751,81 +425,29 @@ def setup_tracing(
|
|
|
751
425
|
except Exception as e:
|
|
752
426
|
logger.error(f"Failed to initialize CloudMonitor OTLP exporter: {e}")
|
|
753
427
|
|
|
754
|
-
# 4.
|
|
755
|
-
|
|
756
|
-
# 同时使用两者会导致重复的 trace
|
|
757
|
-
langfuse_enabled = enable_langfuse
|
|
758
|
-
if langfuse_enabled is None:
|
|
759
|
-
# Auto-detect from environment variables
|
|
760
|
-
langfuse_enabled = (
|
|
761
|
-
not generic_otlp_config
|
|
762
|
-
and bool(os.getenv("LANGFUSE_PUBLIC_KEY") or (langfuse_config or {}).get("public_key"))
|
|
763
|
-
)
|
|
764
|
-
|
|
765
|
-
# 检查是否应该禁用 LangfuseExporter (当使用 LangChain/LangGraph 时)
|
|
766
|
-
# 优先使用显式参数,否则读取环境变量
|
|
767
|
-
if use_callback_only is None:
|
|
768
|
-
use_callback_only = os.getenv("LANGFUSE_USE_CALLBACK", "false").lower() == "true"
|
|
769
|
-
|
|
770
|
-
if langfuse_enabled and not use_callback_only:
|
|
428
|
+
# 4. OTLP gRPC Exporter (optional compatibility path)
|
|
429
|
+
if enable_otlp:
|
|
771
430
|
try:
|
|
772
|
-
from opentelemetry.exporter.otlp.proto.
|
|
773
|
-
|
|
774
|
-
config = _build_langfuse_otlp_config(langfuse_config)
|
|
775
|
-
if config:
|
|
776
|
-
otlp_exporter = OTLPSpanExporter(
|
|
777
|
-
endpoint=config["endpoint"],
|
|
778
|
-
headers=config["headers"],
|
|
779
|
-
)
|
|
780
|
-
otlp_exporter = _LoggingSpanExporter(
|
|
781
|
-
otlp_exporter,
|
|
782
|
-
name="Langfuse OTLP",
|
|
783
|
-
endpoint=config["endpoint"],
|
|
784
|
-
service_name=_get_service_name(),
|
|
785
|
-
header_keys=sorted(config["headers"]),
|
|
786
|
-
span_transform=_prepare_langfuse_spans,
|
|
787
|
-
)
|
|
788
|
-
provider.add_span_processor(
|
|
789
|
-
BatchSpanProcessor(
|
|
790
|
-
otlp_exporter,
|
|
791
|
-
**_batch_span_processor_kwargs(),
|
|
792
|
-
)
|
|
431
|
+
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
|
|
432
|
+
OTLPSpanExporter as GrpcOTLPSpanExporter,
|
|
793
433
|
)
|
|
794
|
-
logger.info(
|
|
795
|
-
"Langfuse OTLP exporter enabled: %s (%s) headers=%s",
|
|
796
|
-
config["endpoint"],
|
|
797
|
-
config["protocol"],
|
|
798
|
-
",".join(sorted(config["headers"])),
|
|
799
|
-
)
|
|
800
|
-
else:
|
|
801
|
-
logger.warning("Langfuse credentials not found, skipping")
|
|
802
434
|
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
elif langfuse_enabled:
|
|
808
|
-
logger.info("Langfuse will use CallbackHandler (recommended for LangChain/LangGraph)")
|
|
809
|
-
|
|
810
|
-
# 5. OTLP Exporter (optional)
|
|
811
|
-
if enable_otlp:
|
|
812
|
-
try:
|
|
813
|
-
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
|
|
814
|
-
otlp_exporter = OTLPSpanExporter(endpoint=otlp_endpoint)
|
|
815
|
-
provider.add_span_processor(
|
|
816
|
-
BatchSpanProcessor(
|
|
817
|
-
otlp_exporter,
|
|
818
|
-
**_batch_span_processor_kwargs(),
|
|
819
|
-
)
|
|
435
|
+
grpc_otlp_exporter = GrpcOTLPSpanExporter(endpoint=otlp_endpoint)
|
|
436
|
+
_register_span_processor(
|
|
437
|
+
provider,
|
|
438
|
+
BatchSpanProcessor(grpc_otlp_exporter, **_batch_span_processor_kwargs()),
|
|
820
439
|
)
|
|
821
440
|
logger.info(f"OTLP exporter enabled: {otlp_endpoint}")
|
|
822
441
|
except ImportError:
|
|
823
442
|
logger.warning("OTLP exporter not installed")
|
|
824
443
|
|
|
825
|
-
#
|
|
444
|
+
# 5. ADK Auto-Instrumentation (for Google ADK projects)
|
|
826
445
|
if enable_adk_instrumentation and not _adk_instrumented:
|
|
827
446
|
try:
|
|
828
|
-
|
|
447
|
+
instrumentation_module = importlib.import_module(
|
|
448
|
+
"openinference.instrumentation.google_adk"
|
|
449
|
+
)
|
|
450
|
+
GoogleADKInstrumentor = getattr(instrumentation_module, "GoogleADKInstrumentor")
|
|
829
451
|
GoogleADKInstrumentor().instrument()
|
|
830
452
|
_adk_instrumented = True
|
|
831
453
|
logger.info("Google ADK instrumentation enabled")
|
|
@@ -837,10 +459,11 @@ def setup_tracing(
|
|
|
837
459
|
except Exception as e:
|
|
838
460
|
logger.debug(f"ADK instrumentation failed: {e}")
|
|
839
461
|
|
|
840
|
-
#
|
|
462
|
+
# 6. LangChain Auto-Instrumentation (covers LangChain + LangGraph LLM/tool/node spans)
|
|
841
463
|
if enable_adk_instrumentation:
|
|
842
464
|
try:
|
|
843
465
|
from openinference.instrumentation.langchain import LangChainInstrumentor
|
|
466
|
+
|
|
844
467
|
LangChainInstrumentor().instrument()
|
|
845
468
|
logger.info("LangChain instrumentation enabled")
|
|
846
469
|
except ImportError:
|
|
@@ -859,18 +482,28 @@ def setup_tracing(
|
|
|
859
482
|
|
|
860
483
|
|
|
861
484
|
def shutdown_tracing():
|
|
862
|
-
"""
|
|
863
|
-
global _langfuse_exporter, _tracing_initialized
|
|
485
|
+
"""Flush and stop every processor installed by this module.
|
|
864
486
|
|
|
865
|
-
|
|
487
|
+
The global provider may belong to another library, so KsADK never shuts the
|
|
488
|
+
provider itself. It owns and closes only the processors it registered.
|
|
489
|
+
"""
|
|
490
|
+
processors = list(_managed_span_processors)
|
|
491
|
+
_managed_span_processors.clear()
|
|
492
|
+
for processor in processors:
|
|
866
493
|
try:
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
494
|
+
processor.force_flush(timeout_millis=30000)
|
|
495
|
+
except TypeError:
|
|
496
|
+
try:
|
|
497
|
+
processor.force_flush()
|
|
498
|
+
except Exception:
|
|
499
|
+
logger.exception("Tracing processor force_flush failed")
|
|
870
500
|
except Exception:
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
501
|
+
logger.exception("Tracing processor force_flush failed")
|
|
502
|
+
for processor in processors:
|
|
503
|
+
try:
|
|
504
|
+
processor.shutdown()
|
|
505
|
+
except Exception:
|
|
506
|
+
logger.exception("Tracing processor shutdown failed")
|
|
874
507
|
|
|
875
508
|
|
|
876
509
|
def get_memory_exporter() -> Optional[InMemoryExporter]:
|
|
@@ -878,15 +511,11 @@ def get_memory_exporter() -> Optional[InMemoryExporter]:
|
|
|
878
511
|
return _exporter_instance
|
|
879
512
|
|
|
880
513
|
|
|
881
|
-
def get_langfuse_exporter():
|
|
882
|
-
"""获取 Langfuse Exporter 实例"""
|
|
883
|
-
return _langfuse_exporter
|
|
884
|
-
|
|
885
|
-
|
|
886
514
|
def get_tracer(name: str = "ksadk"):
|
|
887
515
|
"""获取 Tracer"""
|
|
888
516
|
try:
|
|
889
517
|
from opentelemetry import trace
|
|
518
|
+
|
|
890
519
|
return trace.get_tracer(name)
|
|
891
520
|
except ImportError:
|
|
892
521
|
return None
|