Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(tracing): compact one-line span format instead of verbose JSON dump
trohitg778a7584e45b
diff --git a/server/core/tracing.py b/server/core/tracing.pyindex ea443019..0143f9f1 100644--- a/server/core/tracing.py+++ b/server/core/tracing.py@@ -10,11 +10,13 @@ want to emit spans do::with tracer.start_as_current_span("namespace.operation"):...-Spans are exported via ``ConsoleSpanExporter`` so cold-start-benchmarking is grep-able from the same terminal that runs the dev-server::+In dev, ``ConsoleSpanExporter`` is wired with a compact one-line+``formatter`` so each completed span shows up as::- pnpm run dev 2>&1 | grep '"name":'+ [span] broadcaster.refresh_telegram 4012ms path=auto_reconnect connected=False++(The default OTel formatter emits a 30-line JSON document per span,+which makes interactive logs unreadable.)In production, swap ``ConsoleSpanExporter`` for an OTLP exporterpointed at a collector (Jaeger / Tempo / Honeycomb / etc.) — purely@@ -29,7 +31,7 @@ from __future__ import annotationsfrom opentelemetry import tracefrom opentelemetry.sdk.resources import SERVICE_NAME, Resource-from opentelemetry.sdk.trace import TracerProvider+from opentelemetry.sdk.trace import ReadableSpan, TracerProviderfrom opentelemetry.sdk.trace.export import (BatchSpanProcessor,ConsoleSpanExporter,@@ -39,6 +41,23 @@ from opentelemetry.sdk.trace.export import (_INITIALIZED = False+def _compact_span_formatter(span: ReadableSpan) -> str:+ """Render a finished span as a single ``[span] name Xms key=value ...`` line.++ Hooked into ``ConsoleSpanExporter`` via its standard ``formatter``+ parameter (the documented OTel extension point for changing output+ format without subclassing the exporter).+ """+ duration_ms = (span.end_time - span.start_time) // 1_000_000 # ns -> ms+ parts = [f"[span] {span.name} {duration_ms}ms"]+ if span.attributes:+ for key, value in span.attributes.items():+ parts.append(f"{key}={value}")+ if span.status.status_code.name != "UNSET":+ parts.append(f"status={span.status.status_code.name}")+ return " ".join(parts) + "\n"++def init_tracing(service_name: str = "machinaos-backend") -> None:"""Configure the global TracerProvider.@@ -52,9 +71,12 @@ def init_tracing(service_name: str = "machinaos-backend") -> None:provider = TracerProvider(resource=Resource.create({SERVICE_NAME: service_name}))- # ConsoleSpanExporter writes one JSON document per span to stdout —- # ideal for dev/benchmark mode. Span fields include ``name``,- # ``start_time``, ``end_time``, ``attributes``, ``status``.- provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))+ # Compact one-line console output instead of the default 30-line+ # JSON dump per span — keeps interactive logs readable.+ provider.add_span_processor(+ BatchSpanProcessor(+ ConsoleSpanExporter(formatter=_compact_span_formatter)+ )+ )trace.set_tracer_provider(provider)_INITIALIZED = True