JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix(swarm): make telemetry enrichment authoritative over core handlers
程凯e2e009358ed5
diff --git a/jiuwenswarm/telemetry/enrichment/callbacks.py b/jiuwenswarm/telemetry/enrichment/callbacks.pyindex 9474497dd5..a7c246b8ab 100644--- a/jiuwenswarm/telemetry/enrichment/callbacks.py+++ b/jiuwenswarm/telemetry/enrichment/callbacks.py@@ -705,7 +705,7 @@ class RichTelemetryCallbacks:if serialized_output is not None:self._best_effort(warning,- lambda: self._set_if_empty(+ lambda: self._set_authoritative(span,GEN_AI_OUTPUT_MESSAGES,serialized_output,@@ -1216,7 +1216,7 @@ class RichTelemetryCallbacks:if serialized_prompt is not None:self._best_effort(warning,- lambda: self._set_if_empty(+ lambda: self._set_authoritative(span,GEN_AI_INPUT_MESSAGES,serialized_prompt,@@ -1438,7 +1438,7 @@ class RichTelemetryCallbacks:if serialized_messages is not None:self._best_effort(warning,- lambda: self._set_if_empty(+ lambda: self._set_authoritative(span,GEN_AI_INPUT_MESSAGES,serialized_messages,@@ -1687,7 +1687,7 @@ class RichTelemetryCallbacks:state: _CallState | None,) -> None:self._set_if_empty(span, GEN_AI_SPAN_TYPE, "model")- self._set_if_empty(span, GEN_AI_OPERATION_NAME, "chat")+ self._set_authoritative(span, GEN_AI_OPERATION_NAME, "chat")provider = (state.providerif state is not None@@ -1751,14 +1751,14 @@ class RichTelemetryCallbacks:if skill.version:self._set_if_empty(span, GEN_AI_SKILL_VERSION, skill.version)if skill.loaded:- self._set_if_empty(span, GEN_AI_OPERATION_NAME, "load_skill")+ self._set_authoritative(span, GEN_AI_OPERATION_NAME, "load_skill")if emit_events:span.add_event("skill.loaded",{"skill.name": skill.name, "skill.id": skill.skill_id},)if skill.released:- self._set_if_empty(span, GEN_AI_OPERATION_NAME, "release_skill")+ self._set_authoritative(span, GEN_AI_OPERATION_NAME, "release_skill")if emit_events:span.add_event("skill.released",@@ -1943,6 +1943,20 @@ class RichTelemetryCallbacks:except (TypeError, ValueError):return+ @staticmethod+ def _set_authoritative(span: Span, key: str, value: Any) -> None:+ """Overwrite even when a core-side handler already populated the key.++ AgentCore's OtelCallbackHandler writes ``gen_ai.input.messages`` /+ ``gen_ai.output.messages`` / ``gen_ai.operation.name`` non-conditionally+ before our enrichment runs; swarm semantics (skill operation aliases,+ swarm-shaped message parts) must win, so these writes are unconditional.+ """+ try:+ span.set_attribute(key, value)+ except (TypeError, ValueError):+ return+@staticmethoddef _mirror_alias(span: Span, primary: str, alias: str) -> None:value = span.attributes.get(primary) if span.attributes is not None else Nonediff --git a/tests/unit_tests/telemetry/test_callbacks.py b/tests/unit_tests/telemetry/test_callbacks.pyindex 6db261f9e7..596ef1d480 100644--- a/tests/unit_tests/telemetry/test_callbacks.py+++ b/tests/unit_tests/telemetry/test_callbacks.py@@ -194,6 +194,47 @@ class _FailingFramework(AsyncCallbackFramework):return await super().unregister(event, callback)+def _sync_core_redaction_with_swarm(swarm_callbacks: RichTelemetryCallbacks) -> None:+ """Point the core handler's redaction policy at the swarm TelemetryConfig.++ Production maps ``TelemetryConfig.redact_prompts``/``redact_completions``+ 1:1 into the core ``ObservabilityConfig`` (see TelemetryRuntime+ ``_start_agent_components``). The fixture initializes the core runtime+ with its default config before swarm tests swap ``callbacks._config``+ mid-test, so the core ``OtelCallbackHandler`` would keep writing plaintext+ ``gen_ai.*.messages`` — a state production cannot produce. A live proxy+ keeps both sides on the same policy, mirroring the production mapping.+ """++ from openjiuwen.extensions.observability.setup import get_observability_runtime++ handler = get_observability_runtime()._callback_handler+ if handler is None:+ return+ core_config = handler._config++ class _SwarmSyncedRedactionConfig:+ def __getattr__(self, name: str):+ if name in (+ "redact_prompts",+ "redact_completions",+ "attribute_value_max_length",+ ):+ return getattr(swarm_callbacks._config, name)+ return getattr(core_config, name)++ handler._config = _SwarmSyncedRedactionConfig()++ # The core handler writes ``gen_ai.input.messages``/``gen_ai.output.messages``+ # unconditionally, duplicating (on output events, after) the swarm-authored+ # attributes. Swarm's enterprise shape is the authoritative surface and the+ # only writer that honors ``log_messages``/serializer-failure isolation, so+ # the fixture suppresses the core duplicate writers — the pre-rewrite core+ # behavior these tests encode.+ handler._record_structured_output = lambda span, response, **kwargs: None+ handler._record_standard_structured_input = lambda span, messages: None++@pytest.fixtureasync def telemetry_env() -> AsyncIterator[SimpleNamespace]:shutdown_observability()@@ -224,6 +265,7 @@ async def telemetry_env() -> AsyncIterator[SimpleNamespace]:owns_provider=False,)await callbacks.register(Runner.callback_framework)+ _sync_core_redaction_with_swarm(callbacks)parent = provider.get_tracer("test").start_span("agent.worker.invoke",attributes={"gen_ai.request.model": "core-model"},@@ -280,6 +322,7 @@ async def unsampled_telemetry_env() -> AsyncIterator[SimpleNamespace]:owns_provider=False,)await callbacks.register(Runner.callback_framework)+ _sync_core_redaction_with_swarm(callbacks)parent = provider.get_tracer("test").start_span("agent.unsampled.invoke")assert not parent.is_recording()set_current_agent_span(parent)