telemetry

package
v0.7.188 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package telemetry sends anonymous command-usage events to Segment.

Telemetry is opt-out: it fires unless disabled via a well-known opt-out environment variable or the persisted telemetry config preference (see internal/config.IsTelemetry). Only the command path, the names (never values) of flags the user set, the outcome ("success"/"failure"), the wall-clock duration, the Go type and message of any error, a per-install anonymous instance ID, the operating system, and the detected AI coding agent (if any) are ever collected — no flag values, argument values, or other PII.

Once the user authenticates, events also carry their CircleCI user UUID and an identify call joins the anonymous IDs the install was reporting under to that user, so the journey before logging in is not attributed to a stranger. See Sender.Identify and IdentifyUser.

Modeled on circleci-cli's internal/telemetry package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectCodingAgent added in v0.7.123

func DetectCodingAgent() string

DetectCodingAgent returns a stable name for the AI coding agent chunk-cli appears to have been invoked from, based on well-known environment variables those agents set in their shells. Returns "" if no known agent is detected, which is the common case for a human running chunk directly.

func DisableTelemetry

func DisableTelemetry(cmd *cobra.Command)

DisableTelemetry marks cmd so RecordForSubcommands skips wrapping it. Used for commands that should never report their own invocation, such as the hidden receive-telemetry command.

func IdentifyUser added in v0.7.188

func IdentifyUser(ctx context.Context, userID uuid.UUID)

IdentifyUser de-anonymizes the current session: it attaches userID to every event the Sender in ctx reports from here on, and sends an identify call so Segment joins the anonymous identifiers this install was reporting under to that user. Call it right after an authentication flow persists a user ID.

It is best-effort, like the rest of telemetry: a missing Sender or a failed enqueue is silently ignored rather than breaking the auth flow the user actually asked for.

func IsTelemetryDisabled

func IsTelemetryDisabled(cmd *cobra.Command) bool

IsTelemetryDisabled reports whether DisableTelemetry was called on cmd.

func RecordForSubcommands

func RecordForSubcommands(cmd *cobra.Command)

RecordForSubcommands wraps every descendant command's RunE so it reports a command_invocation event after running, without requiring per-command changes.

func RecordNow

func RecordNow(cmd *cobra.Command, err error, duration time.Duration)

RecordNow reports a command_invocation event immediately: the full command path, the sorted comma-joined names (never values) of flags the user set, the outcome ("success" or "failure"), the wall-clock duration in milliseconds, and — on failure — the Go type and message of the error. chunk-cli events are distinguished from circleci-cli events via Context.App.Name ("chunk-cli").

func WithSender

func WithSender(ctx context.Context, s *Sender) context.Context

WithSender attaches s to ctx so descendant commands can record events via RecordNow.

Types

type Config

type Config struct {
	// Send enables sending events to Segment via the delegate subprocess.
	Send bool
	// Log enables logging events to stderr for debugging.
	Log bool

	// Binary is re-exec'd with JSON-encoded events on stdin when Send is true.
	Binary string
	// WriteKey is the Segment write key used by the delegate subprocess.
	WriteKey string
	// Endpoint is the Segment endpoint. Optional; defaults to segment.io.
	// Normally only set for testing.
	Endpoint string

	// TestDestination, when non-nil, is added as an event destination so
	// tests can record events and assert on them synchronously without
	// spawning a subprocess or hitting the network.
	TestDestination destination

	Metadata Meta
}

Config configures a Sender.

type Meta

type Meta struct {
	Version    string
	InstanceID uuid.UUID
	// SessionTrackingID, when non-zero, is used as AnonymousId so all chunk
	// invocations within a session share a common anonymous identifier.
	// When zero, InstanceID is used as AnonymousId instead.
	SessionTrackingID uuid.UUID
	// UserID is the authenticated CircleCI user UUID. When non-zero it is sent
	// as UserId so events can be attributed to a real user.
	UserID uuid.UUID

	// OSName, OSVersion, KernelArch, and PlatformFamily populate the Segment
	// OS and Device context fields. Best-effort: zero values are sent as-is.
	OSName         string
	OSVersion      string
	KernelArch     string
	PlatformFamily string
	// Extra is forwarded to Context.Traits on every event (e.g. "agent", "is_tty").
	Extra map[string]any
}

Meta describes the fields attached to every tracked event.

type Sender

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

Sender tracks anonymous command-usage events. A nil *Sender is valid and silently drops events, so callers never need to nil-check it.

func FromContext

func FromContext(ctx context.Context) *Sender

FromContext returns the Sender attached to ctx, or nil if none was attached.

func NewSender

func NewSender(cfg Config) (*Sender, error)

NewSender creates a new Sender per cfg.

func (*Sender) Close

func (s *Sender) Close() error

Close flushes buffered events to their destinations. Safe to call multiple times and on a nil Sender.

func (*Sender) Identify added in v0.7.188

func (s *Sender) Identify(userID uuid.UUID) error

Identify sends a Segment identify joining this install's anonymous ID(s) to userID. One call goes out per anonymous ID (session and/or instance), so both the in-session and out-of-session histories join the user. No traits are sent. Safe to call on a nil or closed Sender; uuid.Nil is a no-op.

func (*Sender) SetUserID added in v0.7.188

func (s *Sender) SetUserID(userID uuid.UUID)

SetUserID attaches userID to every event this Sender reports from now on, including the in-flight command_invocation. Safe to call on a nil Sender.

func (*Sender) Track

func (s *Sender) Track(eventName string, props map[string]any) error

Track records an analytics event. Safe to call on a nil Sender.

Directories

Path Synopsis
Package receiver forwards buffered telemetry events to Segment.
Package receiver forwards buffered telemetry events to Segment.

Jump to

Keyboard shortcuts

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