telemetry

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package telemetry defines the flyedge protection-telemetry seam. It is explicit and injectable: the Guard records an Event per policy check into whatever Telemetry you install, and Report() returns a Summary you print if and when you want — the deliberate replacement for the Python SDK's auto-printed "PROTECTION SUMMARY". The default (in-memory Recorder) needs no wiring; a cloud/OTel sink is opt-in.

Index

Constants

View Source
const (
	EventProtection     = "protection_event" // a policy-check outcome (allow/deny/warn)
	EventLLMIO          = "llm_io"           // a model call: model/provider/tokens/latency (+audit)
	EventToolIO         = "tool_io"          // a tool call: name/args/result
	EventSessionStart   = "session_start"    // agent session opened
	EventSessionSummary = "session_summary"  // agent session ended (rolled-up stats in Data)
)

Event types prism's telemetry handler recognizes (event_type → activity_type). An empty Type is treated as EventProtection (a policy check).

Variables

This section is empty.

Functions

This section is empty.

Types

type Batched

type Batched struct {
	// contains filtered or unexported fields
}

Batched is a cloud telemetry sink: it records events locally (so Report still works) AND buffers them for periodic delivery to the gateway on an OWNED goroutine, flushed on Close. This is the explicit lifecycle the Python SDK does with fire-and-forget daemon threads — here it's owned and stopped deterministically.

func NewBatched

func NewBatched(sender Sender, session string, interval time.Duration) *Batched

NewBatched starts a batcher that flushes every interval (min 1s). Call Close to flush + stop.

func (*Batched) Close

func (b *Batched) Close() error

Close stops the flush goroutine and flushes any buffered events one last time. Idempotent — safe to call multiple times (e.g. an explicit Close plus a deferred one).

func (*Batched) Record

func (b *Batched) Record(ev Event)

func (*Batched) Report

func (b *Batched) Report() Summary

type Event

type Event struct {
	Type      string // event_type; "" ⇒ protection_event
	Stage     string
	Action    string
	Reason    string
	Model     string
	Provider  string
	Operation string
	Name      string // span / tool name
	LatencyMS float64
	// InputTokens is the provider-reported UNCACHED input count. Cache reads/writes are
	// reported separately below and are also input tokens — providers split them because they
	// price differently, not because the model didn't process them. The wire carries the sum;
	// see toWire.
	InputTokens  int64
	OutputTokens int64
	TotalTokens  int64
	// Cache token counts, when the provider reports them (Anthropic prompt caching). These
	// dominate real usage for long-running coding sessions: a turn commonly shows 2 uncached
	// input tokens against ~380k cache reads, so omitting them understates input by orders of
	// magnitude.
	CacheReadTokens  int64
	CacheWriteTokens int64
	Streaming        *bool // set on streamed model calls
	Err              string
	OccurredAt       time.Time
	// SessionID / RequestID correlate this record with the agent's other telemetry
	// and prism's /check record for the same session. Sourced from CheckRequest.
	SessionID string
	RequestID string
	// EndpointID / InstanceKey attribute this record to the endpoint-agent instance that
	// produced it — the durable device and the (agent, repository) identity a sensor resolves.
	// Both empty for a plain agent call; the platform joins on them when present.
	EndpointID  string
	InstanceKey string
	// TraceID / SpanID / ParentSpanID place this record in prism's lifecycle span
	// tree (W3C ids). Empty ⇒ prism treats it as unparented.
	TraceID      string
	SpanID       string
	ParentSpanID string
	// AgentFramework + audit payloads + arbitrary Data (session/protection events).
	AgentFramework string
	RequestFull    string
	ResponseFull   string
	Data           map[string]any
}

Event is one telemetry record. For a policy check (the default — Type=="" or EventProtection) Action is the normalized decision and Err is set when the enforcement call itself failed. Rich types (llm_io/tool_io/session_*) carry the model/tool/session fields below and are NOT counted as checks in Summary.

type Noop

type Noop struct{}

Noop discards events and reports an empty Summary.

func (Noop) Close

func (Noop) Close() error

func (Noop) Record

func (Noop) Record(Event)

func (Noop) Report

func (Noop) Report() Summary

type Recorder

type Recorder struct {
	// contains filtered or unexported fields
}

Recorder is the default in-memory telemetry: thread-safe aggregation, no I/O. Report reflects everything recorded so far.

func NewRecorder

func NewRecorder() *Recorder

NewRecorder returns an empty in-memory Recorder.

func (*Recorder) Close

func (r *Recorder) Close() error

func (*Recorder) Record

func (r *Recorder) Record(ev Event)

func (*Recorder) Report

func (r *Recorder) Report() Summary

Report returns a copy of the current aggregate (safe to read/print concurrently with Record).

type Sender

type Sender func(ctx context.Context, batchJSON []byte) error

Sender ships a serialized FlyedgeTelemetryBatch to the gateway (/v1/flyedge/telemetry). The Guard wires one backed by a signed POST; tests inject a fake.

type Summary

type Summary struct {
	Checks  int
	Allowed int
	Denied  int
	Warned  int
	Errors  int
	ByStage map[string]int
	TotalMS float64
}

Summary is an aggregate view over recorded events — the value Guard.Report() returns.

func (Summary) String

func (s Summary) String() string

type Telemetry

type Telemetry interface {
	Record(ev Event)
	Report() Summary
	Close() error
}

Telemetry receives protection events and can summarize them. Implementations must be safe for concurrent Record calls. Close flushes and releases any owned resources (goroutines).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL