tracebridge

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package tracebridge provides a Go library bridge that wraps trace's functionality for use by hawk, replacing the subprocess-based approach.

Since trace and hawk are separate Go modules, this package uses interface-based decoupling. The bridge defines thin interfaces that trace types satisfy, avoiding a direct go.mod dependency.

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyActive = fmt.Errorf("tracebridge: capture already active")

ErrAlreadyActive is returned when StartCapture is called on an already-active session.

View Source
var ErrNotActive = fmt.Errorf("tracebridge: no active capture")

ErrNotActive is returned when StopCapture is called without an active session.

Functions

This section is empty.

Types

type CaptureConfig

type CaptureConfig struct {
	// RepoPath is the absolute path to the repository root.
	RepoPath string

	// SessionID is a unique identifier for this capture session.
	// If empty, one is generated.
	SessionID string

	// Tags are initial metadata tags for the session (equivalent to
	// TRACE_TAG_* environment variables). Keys should not include the
	// TRACE_TAG_ prefix.
	Tags map[string]string
}

CaptureConfig holds the configuration for starting a session capture.

type CaptureResult

type CaptureResult struct {
	// SessionID is the unique identifier for this capture session.
	SessionID string

	// TranscriptPath is the filesystem path to the session transcript.
	TranscriptPath string

	// SpanCount is the number of spans recorded during the session.
	SpanCount int

	// Duration is the total time the session was active.
	Duration time.Duration

	// Tags are the session metadata tags at the time of capture end.
	Tags map[string]string
}

CaptureResult holds the outcome of a completed capture session.

type SessionCapture

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

SessionCapture wraps trace's session capture functionality for use as a Go library. It manages the lifecycle of a single capture session.

func NewSessionCapture

func NewSessionCapture(config CaptureConfig, manager TraceSessionManager) *SessionCapture

NewSessionCapture creates a new SessionCapture with the given config. The returned capture is not yet active; call StartCapture to begin.

If manager is nil, a default no-op manager is used (suitable for tests or environments where trace is not available).

func (*SessionCapture) AddTag

func (sc *SessionCapture) AddTag(key, value string)

AddTag adds a metadata tag to the active session. Tags follow the TRACE_TAG_* convention: the key is normalized (lowercased, hyphens replaced with underscores) before storage.

Returns ErrNotActive if no session is in progress.

func (*SessionCapture) GetTranscriptPath

func (sc *SessionCapture) GetTranscriptPath() string

GetTranscriptPath returns the filesystem path to the session transcript file, or an empty string if no session is active.

func (*SessionCapture) IsActive

func (sc *SessionCapture) IsActive() bool

IsActive reports whether a capture session is currently in progress.

func (*SessionCapture) StartCapture

func (sc *SessionCapture) StartCapture(ctx context.Context) error

StartCapture begins capturing a session. It is safe to call from multiple goroutines but only the first call takes effect; subsequent calls return ErrAlreadyActive.

func (*SessionCapture) StopCapture

func (sc *SessionCapture) StopCapture(ctx context.Context) (*CaptureResult, error)

StopCapture ends the capture session and returns the result. It is safe to call from multiple goroutines but only the first call returns a result; subsequent calls return ErrNotActive.

type SubprocessBridgeAdapter

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

SubprocessBridgeAdapter implements the same interface as the existing sessioncapture.Bridge but delegates to SessionCapture internally.

This allows callers that depend on the subprocess-based bridge interface to swap in the library bridge without changing their call sites.

func NewSubprocessBridgeAdapter

func NewSubprocessBridgeAdapter(repoPath string) *SubprocessBridgeAdapter

NewSubprocessBridgeAdapter creates a SubprocessBridgeAdapter for the given repository path. The returned adapter uses the default (nil/noop) session manager; inject a real TraceSessionManager via NewSubprocessBridgeAdapterWithManager if trace is available.

func NewSubprocessBridgeAdapterWithManager

func NewSubprocessBridgeAdapterWithManager(repoPath string, manager TraceSessionManager) *SubprocessBridgeAdapter

NewSubprocessBridgeAdapterWithManager creates a SubprocessBridgeAdapter backed by a specific TraceSessionManager.

func (*SubprocessBridgeAdapter) Disable

func (a *SubprocessBridgeAdapter) Disable(ctx context.Context, _ string) error

Disable stops the active capture session, mirroring the `trace disable` subprocess call.

func (*SubprocessBridgeAdapter) Enable

Enable starts a capture session, mirroring the `trace enable` subprocess call.

func (*SubprocessBridgeAdapter) GetCaptureResult

func (a *SubprocessBridgeAdapter) GetCaptureResult() *CaptureResult

GetCaptureResult returns the result of the last completed capture, or nil if no capture has completed.

func (*SubprocessBridgeAdapter) Ready

func (a *SubprocessBridgeAdapter) Ready() bool

Ready reports whether the underlying trace session manager is available (always true when the noop manager is in use).

func (*SubprocessBridgeAdapter) Status

func (a *SubprocessBridgeAdapter) Status(ctx context.Context, _ string) (map[string]any, error)

Status returns the current capture state as a map, mirroring the JSON output of `trace status --json`.

type TraceSession

type TraceSession interface {
	// ID returns the session identifier.
	ID() string

	// Phase returns the current session phase (e.g., "active", "ended").
	Phase() string

	// TranscriptPath returns the filesystem path to the transcript file.
	TranscriptPath() string

	// SpanCount returns the number of spans recorded in this session.
	SpanCount() int

	// StartedAt returns when the session was started.
	StartedAt() time.Time

	// EndedAt returns when the session ended, or nil if still active.
	EndedAt() *time.Time

	// Metadata returns session tags (equivalent to TRACE_TAG_* metadata).
	Metadata() map[string]string

	// SetMetadata updates a single tag in session metadata.
	SetMetadata(key, value string)

	// Close ends the session.
	Close(ctx context.Context) error
}

TraceSession represents an individual capture session. It abstracts over trace's session.State without importing the trace module.

type TraceSessionManager

type TraceSessionManager interface {
	// Start begins a new capture session for the given repo path and
	// returns a session handle.
	Start(ctx context.Context, repoPath string, sessionID string) (TraceSession, error)

	// Load retrieves an existing session by ID.
	Load(ctx context.Context, sessionID string) (TraceSession, error)
}

TraceSessionManager defines the operations a trace session manager must support. In production this is satisfied by trace's session.StateStore and related types; in tests it can be replaced with a mock.

Jump to

Keyboard shortcuts

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