mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
Add a built-in telemetry system that records what the agent does — workflows,
model calls, tool calls, errors — to the local machine, powers `/insights`, and
can export to an operator-chosen destination. Default-on locally; nothing leaves
the machine unless the user exports it or opts into the aggregate plane.
Three planes with a hard wall between them:
- local: full-fidelity observability (real model/provider/tool names), on by
default, never leaves the machine.
- aggregate: opt-in metadata, default off. No uploader ships — consent is
recorded via telemetry.consent_state, and `preview` shows what would be
produced, computed locally.
- trajectories: full message content, opt-in, exported only to the operator's
own destination.
Mechanism:
- Bundled `telemetry` plugin registers observational lifecycle hooks
(on_session_start / post_api_request / post_tool_call / on_session_finalize).
No core call sites are edited; hooks already carry the data.
- Fire-and-forget emitter: emit() returns in microseconds, never blocks or
raises into a model/tool call. A daemon thread writes events to an
append-only JSONL log and the tel_* tables in state.db (its own sqlite
connection, separate from SessionDB).
- tel_runs / tel_model_calls / tel_tool_calls live in the declarative
SCHEMA_SQL and are reconciled automatically; SCHEMA_VERSION 16 -> 17.
- metrics derives rollups for /usage and /insights; rollup builds per-run
summaries for `hermes telemetry preview`.
Consent is config, not a parallel command surface. The config file is the root
of trust: set telemetry.consent_state with `hermes config set`, or pin any
telemetry.* key (including allow_aggregate) via managed scope, which overrides
the user's value per key. `hermes telemetry` exposes only what config cannot:
status (report), preview (query), and export.
Export:
- exporter_bulk writes telemetry (and, when the trajectories plane is enabled,
session content) to ndjson/json.
- otlp_exporter streams spans to a configured OpenTelemetry Collector over
OTLP/HTTP. The SDK is an optional extra (hermes-agent[otlp]), lazily
installed via tools.lazy_deps on first use.
- Secrets are always redacted on every export path
(redact_sensitive_text(force=True)); content export is gated by the
trajectories plane, and PII scrubbing follows telemetry.content_redaction.
OTLP auth headers reference environment variable names, never inline values.
No outbound emission to Nous. The aggregate uploader is intentionally not built.
83 lines
2.1 KiB
Python
83 lines
2.1 KiB
Python
"""Trace / run / span id propagation via contextvars.
|
|
|
|
Telemetry events share IDs so a workflow can be reconstructed: one ``trace_id`` per
|
|
workflow, one ``run_id`` per top-level execution, ``span_id`` per timed operation, and
|
|
``parent_span_id`` for nesting. These live in contextvars so async tool calls and
|
|
spawned subagents inherit the lineage automatically.
|
|
|
|
Provides helpers to start/clear a run context and mint child span ids. The telemetry
|
|
plugin sets the run context on session start and reads it in each hook callback.
|
|
Nothing here writes to storage — it only carries ids.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import contextvars
|
|
import uuid
|
|
from dataclasses import dataclass
|
|
from typing import Optional
|
|
|
|
_trace_id: contextvars.ContextVar[Optional[str]] = contextvars.ContextVar(
|
|
"hermes_tel_trace_id", default=None
|
|
)
|
|
_run_id: contextvars.ContextVar[Optional[str]] = contextvars.ContextVar(
|
|
"hermes_tel_run_id", default=None
|
|
)
|
|
_parent_span_id: contextvars.ContextVar[Optional[str]] = contextvars.ContextVar(
|
|
"hermes_tel_parent_span_id", default=None
|
|
)
|
|
|
|
|
|
def new_id() -> str:
|
|
return uuid.uuid4().hex
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class RunContext:
|
|
trace_id: str
|
|
run_id: str
|
|
|
|
|
|
def start_run(trace_id: Optional[str] = None, run_id: Optional[str] = None) -> RunContext:
|
|
"""Begin a run context, minting ids when not supplied. Sets contextvars."""
|
|
tid = trace_id or new_id()
|
|
rid = run_id or new_id()
|
|
_trace_id.set(tid)
|
|
_run_id.set(rid)
|
|
_parent_span_id.set(None)
|
|
return RunContext(trace_id=tid, run_id=rid)
|
|
|
|
|
|
def current_trace_id() -> Optional[str]:
|
|
return _trace_id.get()
|
|
|
|
|
|
def current_run_id() -> Optional[str]:
|
|
return _run_id.get()
|
|
|
|
|
|
def current_parent_span_id() -> Optional[str]:
|
|
return _parent_span_id.get()
|
|
|
|
|
|
def new_span_id() -> str:
|
|
"""Mint a span id (does not alter the parent pointer)."""
|
|
return new_id()
|
|
|
|
|
|
def clear_run() -> None:
|
|
_trace_id.set(None)
|
|
_run_id.set(None)
|
|
_parent_span_id.set(None)
|
|
|
|
|
|
__all__ = [
|
|
"RunContext",
|
|
"new_id",
|
|
"start_run",
|
|
"current_trace_id",
|
|
"current_run_id",
|
|
"current_parent_span_id",
|
|
"new_span_id",
|
|
"clear_run",
|
|
]
|