telemetry

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureGlobalTelemetryInitialized

func EnsureGlobalTelemetryInitialized()

EnsureGlobalTelemetryInitialized ensures telemetry is initialized exactly once This handles all the setup automatically - no explicit initialization needed

func GetTelemetryEnabled

func GetTelemetryEnabled() bool

func NewTelemetryLogger

func NewTelemetryLogger(logger *slog.Logger) *telemetryLogger

NewTelemetryLogger creates a new telemetry logger that automatically prepends "[Telemetry]" to all messages

func RecordError added in v1.5.8

func RecordError(ctx context.Context, err string)

func RecordSessionEnd added in v1.5.8

func RecordSessionEnd(ctx context.Context)

func RecordSessionStart added in v1.5.8

func RecordSessionStart(ctx context.Context, sessionID, agentName string)

func RecordTokenUsage added in v1.5.8

func RecordTokenUsage(ctx context.Context, model string, inputTokens, outputTokens int64, cost float64)

func RecordToolCall added in v1.5.8

func RecordToolCall(ctx context.Context, toolName, sessionID, agentName string, duration time.Duration, err error)

func SetGlobalTelemetryDebugMode

func SetGlobalTelemetryDebugMode(debug bool)

SetGlobalTelemetryDebugMode sets the debug mode for automatic telemetry initialization This should be called by the root package to pass the --debug flag state

func SetGlobalTelemetryVersion

func SetGlobalTelemetryVersion(version string)

SetGlobalTelemetryVersion sets the version for automatic telemetry initialization This should be called by the root package to provide the correct version

func TrackCommand

func TrackCommand(action string, args []string)

TrackCommand records a command event using automatic telemetry initialization

func WithClient

func WithClient(ctx context.Context, client *Client) context.Context

WithClient adds a telemetry client to the context

Types

type Client

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

Client provides simplified telemetry functionality for cagent

func FromContext

func FromContext(ctx context.Context) *Client

FromContext retrieves the telemetry client from context

func GetGlobalTelemetryClient

func GetGlobalTelemetryClient() *Client

GetGlobalTelemetryClient returns the global telemetry client for adding to context

func (*Client) RecordError

func (tc *Client) RecordError(ctx context.Context, errorMsg string)

RecordError records a general session error

func (*Client) RecordSessionEnd

func (tc *Client) RecordSessionEnd(ctx context.Context)

RecordSessionEnd finalizes session tracking

func (*Client) RecordSessionStart

func (tc *Client) RecordSessionStart(ctx context.Context, agentName, sessionID string) string

RecordSessionStart initializes session tracking

func (*Client) RecordTokenUsage

func (tc *Client) RecordTokenUsage(ctx context.Context, model string, inputTokens, outputTokens int64, cost float64)

RecordTokenUsage records token usage metrics

func (*Client) RecordToolCall

func (tc *Client) RecordToolCall(ctx context.Context, toolName, sessionID, agentName string, duration time.Duration, err error)

RecordToolCall records a tool call event

func (*Client) Track

func (tc *Client) Track(ctx context.Context, structuredEvent StructuredEvent)

Track records a structured telemetry event with type-safe properties (synchronous) This is the only method for telemetry tracking, all event-specific methods are wrappers around this one

type CommandEvent

type CommandEvent struct {
	Action  string   `json:"action"`
	Args    []string `json:"args,omitempty"`
	Success bool     `json:"success"`
	Error   string   `json:"error,omitempty"`
}

CommandEvent represents command execution events

func (*CommandEvent) GetEventType

func (e *CommandEvent) GetEventType() EventType

func (*CommandEvent) ToStructuredProperties

func (e *CommandEvent) ToStructuredProperties() any

type CommandInfo

type CommandInfo struct {
	Action string
	Args   []string
	Flags  []string
}

CommandInfo represents the parsed command information

func BuildCommandInfo

func BuildCommandInfo(cmd *cobra.Command, args []string, baseName string) CommandInfo

BuildCommandInfo extracts detailed command information for telemetry

type CommandPayload

type CommandPayload struct {
	Action    string   `json:"action"`
	Args      []string `json:"args,omitempty"`
	IsSuccess bool     `json:"is_success"`
	Error     string   `json:"error,omitempty"`
}

CommandPayload represents the HTTP payload structure for command events

type EventPayload

type EventPayload struct {
	Event          EventType `json:"event"`
	EventTimestamp int64     `json:"event_timestamp"`
	Source         string    `json:"source"`

	Properties map[string]any `json:"properties,omitempty"`
}

EventPayload represents a structured telemetry event

type EventType

type EventType string

EventType represents the type of telemetry event

const (
	EventTypeCommand EventType = "command"
	EventTypeSession EventType = "session"
	EventTypeToken   EventType = "token"
	EventTypeTool    EventType = "tool"
)

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient interface for making HTTP requests (allows mocking in tests)

type SessionEndEvent

type SessionEndEvent struct {
	Action       string   `json:"action"`
	SessionID    string   `json:"session_id"`
	AgentName    string   `json:"agent_name"`
	Duration     int64    `json:"duration_ms"`
	ToolCalls    int      `json:"tool_calls"`
	InputTokens  int64    `json:"input_tokens"`
	OutputTokens int64    `json:"output_tokens"`
	TotalTokens  int64    `json:"total_tokens"`
	ErrorCount   int      `json:"error_count"`
	Cost         float64  `json:"cost"`
	IsSuccess    bool     `json:"is_success"`
	Error        []string `json:"error"`
}

SessionEndEvent represents session events

func (*SessionEndEvent) GetEventType

func (e *SessionEndEvent) GetEventType() EventType

func (*SessionEndEvent) ToStructuredProperties

func (e *SessionEndEvent) ToStructuredProperties() any

type SessionEndPayload

type SessionEndPayload struct {
	Action       string   `json:"action"`
	SessionID    string   `json:"session_id"`
	AgentName    string   `json:"agent_name"`
	DurationMs   int64    `json:"duration_ms"`
	ToolCalls    int      `json:"tool_calls"`
	InputTokens  int64    `json:"input_tokens"`
	OutputTokens int64    `json:"output_tokens"`
	TotalTokens  int64    `json:"total_tokens"`
	Cost         float64  `json:"cost"`
	ErrorCount   int      `json:"error_count"`
	IsSuccess    bool     `json:"is_success"`
	Error        []string `json:"error"`
}

SessionEndPayload represents the HTTP payload structure for session events

type SessionStartEvent

type SessionStartEvent struct {
	Action    string `json:"action"`
	SessionID string `json:"session_id"`
	AgentName string `json:"agent_name"`
}

SessionStartEvent represents session events

func (*SessionStartEvent) GetEventType

func (e *SessionStartEvent) GetEventType() EventType

func (*SessionStartEvent) ToStructuredProperties

func (e *SessionStartEvent) ToStructuredProperties() any

type SessionStartPayload

type SessionStartPayload struct {
	Action    string `json:"action"`
	SessionID string `json:"session_id"`
	AgentName string `json:"agent_name"`
}

SessionStartPayload represents the HTTP payload structure for session events

type SessionState

type SessionState struct {
	ID           string
	AgentName    string
	StartTime    time.Time
	ToolCalls    int
	TokenUsage   SessionTokenUsage
	ErrorCount   int
	Error        []string
	SessionEnded bool
}

SessionState consolidates all session-related tracking

type SessionTokenUsage

type SessionTokenUsage struct {
	InputTokens  int64   `json:"input_tokens"`
	OutputTokens int64   `json:"output_tokens"`
	Cost         float64 `json:"cost"`
}

SessionTokenUsage tracks token consumption

type StructuredEvent

type StructuredEvent interface {
	GetEventType() EventType
	ToStructuredProperties() any
}

StructuredEvent represents a type-safe telemetry event with structured properties

type TokenEvent

type TokenEvent struct {
	Action       string  `json:"action"`
	ModelName    string  `json:"model_name"`
	SessionID    string  `json:"session_id"`
	AgentName    string  `json:"agent_name"`
	InputTokens  int64   `json:"input_tokens"`
	OutputTokens int64   `json:"output_tokens"`
	TotalTokens  int64   `json:"total_tokens"`
	Cost         float64 `json:"cost"`
}

TokenEvent represents token usage events

func (*TokenEvent) GetEventType

func (e *TokenEvent) GetEventType() EventType

func (*TokenEvent) ToStructuredProperties

func (e *TokenEvent) ToStructuredProperties() any

type TokenPayload

type TokenPayload struct {
	Action       string  `json:"action"`
	SessionID    string  `json:"session_id"`
	AgentName    string  `json:"agent_name"`
	ModelName    string  `json:"model_name"`
	InputTokens  int64   `json:"input_tokens"`
	OutputTokens int64   `json:"output_tokens"`
	TotalTokens  int64   `json:"total_tokens"`
	Cost         float64 `json:"cost"`
}

TokenPayload represents the HTTP payload structure for token events

type ToolEvent

type ToolEvent struct {
	Action    string `json:"action"`
	ToolName  string `json:"tool_name"`
	SessionID string `json:"session_id"`
	AgentName string `json:"agent_name"`
	Duration  int64  `json:"duration_ms"`
	Success   bool   `json:"success"`
	Error     string `json:"error,omitempty"`
}

ToolEvent represents tool call events

func (*ToolEvent) GetEventType

func (e *ToolEvent) GetEventType() EventType

func (*ToolEvent) ToStructuredProperties

func (e *ToolEvent) ToStructuredProperties() any

type ToolPayload

type ToolPayload struct {
	Action     string `json:"action"`
	SessionID  string `json:"session_id"`
	AgentName  string `json:"agent_name"`
	ToolName   string `json:"tool_name"`
	DurationMs int64  `json:"duration_ms"`
	IsSuccess  bool   `json:"is_success"`
	Error      string `json:"error,omitempty"`
}

ToolPayload represents the HTTP payload structure for tool events

Jump to

Keyboard shortcuts

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