monitor

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package monitor provides the orchestration API for agentwatch. It constructs and runs monitors, exposes snapshots, and delivers events.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Seq  uint64    `json:"seq"`
	At   time.Time `json:"at"`
	Type EventType `json:"type"`

	Sessions []session.SessionState `json:"sessions,omitempty"`
	Updates  []session.SessionState `json:"updates,omitempty"`
	Removed  []string               `json:"removed,omitempty"`

	Lifecycle *session.LifecycleEvent `json:"lifecycle,omitempty"`
	Health    *Health                 `json:"health,omitempty"`
}

Event is the single envelope delivered to EventSink implementations. Seq is monotonically increasing per monitor instance. Events are delivered after state commit and outside store locks.

type EventSink

type EventSink interface {
	HandleEvent(ctx context.Context, ev Event) error
}

EventSink receives events from a Monitor. HandleEvent must return quickly; long-running sinks should wrap themselves in an async queue.

type EventSinkFunc

type EventSinkFunc func(ctx context.Context, ev Event) error

EventSinkFunc is a function adapter for EventSink.

func (EventSinkFunc) HandleEvent

func (f EventSinkFunc) HandleEvent(ctx context.Context, ev Event) error

HandleEvent implements EventSink.

type EventType

type EventType string

EventType names the kind of monitor event.

const (
	// EventSnapshot carries the full current state of all sessions.
	EventSnapshot EventType = "snapshot"
	// EventDelta carries only sessions that changed since the last event.
	EventDelta EventType = "delta"
	// EventLifecycle carries a single session lifecycle transition.
	EventLifecycle EventType = "lifecycle"
	// EventHealth carries an updated health record for a source.
	EventHealth EventType = "health"
)

type Health

type Health struct {
	Source           string       `json:"source"`
	Status           HealthStatus `json:"status"`
	DiscoverFailures int          `json:"discoverFailures"`
	ParseFailures    int          `json:"parseFailures"`
	LastError        string       `json:"lastError,omitempty"`
	UpdatedAt        time.Time    `json:"updatedAt"`
}

Health reports the operational state of a single source.

type HealthStatus

type HealthStatus string

HealthStatus indicates the operational state of a source.

const (
	// HealthHealthy means the source is operating normally.
	HealthHealthy HealthStatus = "healthy"
	// HealthDegraded means the source is experiencing intermittent failures.
	HealthDegraded HealthStatus = "degraded"
	// HealthFailed means the source has stopped providing data.
	HealthFailed HealthStatus = "failed"
)

type Monitor

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

Monitor orchestrates source polling, state management, and event delivery.

func New

func New(opts ...Option) (*Monitor, error)

New creates a Monitor with the given options. It returns an error if the configuration is invalid.

func (*Monitor) Get

func (m *Monitor) Get(id string) (session.SessionState, bool)

Get returns a deep copy of the session with the given ID and true, or a zero SessionState and false if no session with that ID exists.

func (*Monitor) Health

func (m *Monitor) Health() map[string]Health

Health returns the current health state for all sources. The returned map is safe to read and mutate without affecting the monitor.

func (*Monitor) PollOnce

func (m *Monitor) PollOnce(ctx context.Context) error

PollOnce discovers and parses all sources, updates the internal store, and delivers events to sinks. Individual source errors are logged but do not stop the poll. Returns ctx.Err() if the context is canceled.

func (*Monitor) Run

func (m *Monitor) Run(ctx context.Context) error

Run starts the monitor loop. It polls sources at the configured interval and reaps terminal sessions whose retention window has expired. Run blocks until ctx is canceled and returns ctx.Err().

func (*Monitor) Snapshot

func (m *Monitor) Snapshot() []session.SessionState

Snapshot returns a deep copy of all currently tracked sessions. The returned slice is safe to read and mutate without affecting the monitor.

func (*Monitor) Sources

func (m *Monitor) Sources() []string

Sources returns the names of all configured sources in their registration order.

type MultiSink

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

MultiSink delivers events to multiple sinks sequentially. It returns a joined error from all failing sinks.

func NewMultiSink

func NewMultiSink(sinks ...EventSink) *MultiSink

NewMultiSink returns a MultiSink that delivers to the given sinks in order.

func (*MultiSink) HandleEvent

func (ms *MultiSink) HandleEvent(ctx context.Context, ev Event) error

HandleEvent implements EventSink. It delivers ev to each wrapped sink in order and returns a joined error.

type Option

type Option func(*config)

Option configures a Monitor.

func WithClock

func WithClock(c clock.Clock) Option

WithClock sets the clock for time operations. Used in tests.

func WithCompletionRetention

func WithCompletionRetention(d time.Duration) Option

WithCompletionRetention sets how long terminal sessions remain in the store before being removed.

func WithHealthThreshold

func WithHealthThreshold(n int) Option

WithHealthThreshold sets the number of consecutive failures before a source transitions from healthy to degraded. At 2*threshold it transitions to failed. A successful poll resets the failure count and returns the source to healthy.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the structured logger.

func WithPollInterval

func WithPollInterval(d time.Duration) Option

WithPollInterval sets how often Run calls PollOnce.

func WithSink

func WithSink(sink EventSink) Option

WithSink sets the event sink for the monitor.

func WithSources

func WithSources(sources ...source.Source) Option

WithSources sets the sources the monitor will poll.

func WithStaleThreshold

func WithStaleThreshold(d time.Duration) Option

WithStaleThreshold sets how long an active session can go without new data before being transitioned to terminal with a "stale" event. A zero value disables stale detection.

Jump to

Keyboard shortcuts

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