telemetry

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//   - EventCommandExecuted: sent after a CLI command finishes, whether it
	//     succeeds or fails. This helps measure command usage, failure rates, and
	//     runtime. Event-specific properties are PropExitCode (process exit code)
	//     and PropDurationMs (command runtime in milliseconds). Related groups:
	//     none added directly by this event.
	EventCommandExecuted = "cli_command_executed"
	//   - EventProjectLinked: sent after the local CLI directory is linked to a
	//     Supabase project. This helps measure project-linking adoption and connect
	//     future events to the right project and organization. Event-specific
	//     properties: none. Related groups: GroupOrganization and GroupProject.
	//     Related group-identify payloads sent during linking are:
	//     organization group -> organization_slug, and project group -> name,
	//     organization_slug.
	EventProjectLinked = "cli_project_linked"
	//   - EventLoginCompleted: sent after a login flow completes successfully. This
	//     helps measure successful login completion and supports identity stitching
	//     between anonymous and authenticated usage. Event-specific properties:
	//     none. Related groups: none added directly by this event.
	EventLoginCompleted = "cli_login_completed"
	//   - EventStackStarted: sent after the local development stack starts
	//     successfully. This helps measure local development usage and successful
	//     stack startup. Event-specific properties: none. Related groups: none
	//     added directly by this event, but linked project groups may still be
	//     attached when available.
	EventStackStarted = "cli_stack_started"
	//   - EventUpgradeSuggested: sent when a CLI command hits a plan-gated
	//     feature and displays a billing upgrade link. This helps identify
	//     which plan gates users encounter most often so we can improve
	//     error messages and documentation. Event-specific properties are
	//     PropFeatureKey (the entitlement key that was gated) and PropOrgSlug
	//     (the organization slug, empty if lookup failed).
	EventUpgradeSuggested = "cli_upgrade_suggested"
)

CLI telemetry catalog.

This file is the single place to review what analytics events the CLI sends and what metadata may be attached to them. Comments live next to the event, property, group, or signal definition they describe so the catalog is easy to scan without reading the rest of the implementation.

View Source
const (
	// PropFeatureKey is the entitlement key that triggered the upgrade suggestion.
	PropFeatureKey = "feature_key"
	// PropOrgSlug is the organization slug associated with the project.
	PropOrgSlug = "org_slug"
)

Properties specific to EventUpgradeSuggested.

View Source
const (
	// PropPlatform identifies the product source for the event. The CLI always
	// sends "cli".
	PropPlatform = "platform"
	// PropSchemaVersion is the version of the telemetry payload format. This is
	// not a database schema version.
	PropSchemaVersion = "schema_version"
	// PropDeviceID is an anonymous identifier for this CLI installation on this
	// machine.
	PropDeviceID = "device_id"
	// PropSessionID is the PostHog session identifier used to group activity from
	// one CLI session together.
	PropSessionID = "$session_id"
	// PropIsFirstRun is true when the current telemetry state was created during
	// this run, which helps distinguish first-time setup from repeat usage.
	PropIsFirstRun = "is_first_run"
	// PropIsTTY is true when stdout is attached to an interactive terminal.
	PropIsTTY = "is_tty"
	// PropIsCI is true when the CLI appears to be running in a CI environment.
	PropIsCI = "is_ci"
	// PropIsAgent is true when the CLI appears to be running under an AI agent or
	// automation tool.
	PropIsAgent = "is_agent"
	// PropOS is the operating system reported by the Go runtime.
	PropOS = "os"
	// PropArch is the CPU architecture reported by the Go runtime.
	PropArch = "arch"
	// PropCLIVersion is the version string of the CLI build that sent the event.
	PropCLIVersion = "cli_version"
	// PropEnvSignals is an optional summary of coarse environment hints. It is
	// not a raw dump of environment variables.
	PropEnvSignals = "env_signals"
	// PropCommandRunID identifies one command invocation and can be used to tie
	// together telemetry emitted during a single command run.
	PropCommandRunID = "command_run_id"
	// PropCommand is the normalized command path, such as "link" or "db push".
	PropCommand = "command"
	// PropFlags contains changed CLI flags for that command run. Safe flag values
	// may be included, while sensitive values are redacted in the command
	// telemetry implementation.
	PropFlags = "flags"
	// PropExitCode is the process exit code for the command that produced the
	// event.
	PropExitCode = "exit_code"
	// PropDurationMs is the command runtime in milliseconds.
	PropDurationMs = "duration_ms"
)

Shared event properties added to every captured event by Service.Capture.

View Source
const (
	// GroupOrganization identifies the Supabase organization related to the
	// event.
	GroupOrganization = "organization"
	// GroupProject identifies the Supabase project related to the event.
	GroupProject = "project"
)

Group identifiers associate events with higher-level entities in PostHog.

View Source
const SchemaVersion = 1

Variables

View Source
var (
	// EnvSignalPresenceKeys lists environment variables whose presence is recorded
	// as true inside the "env_signals" property.
	EnvSignalPresenceKeys = [...]string{

		"CURSOR_AGENT",
		"CURSOR_TRACE_ID",
		"GEMINI_CLI",
		"CODEX_SANDBOX",
		"CODEX_CI",
		"CODEX_THREAD_ID",
		"ANTIGRAVITY_AGENT",
		"AUGMENT_AGENT",
		"OPENCODE_CLIENT",
		"CLAUDECODE",
		"CLAUDE_CODE",
		"REPL_ID",
		"COPILOT_MODEL",
		"COPILOT_ALLOW_ALL",
		"COPILOT_GITHUB_TOKEN",

		"CI",
		"GITHUB_ACTIONS",
		"BUILDKITE",
		"TF_BUILD",
		"JENKINS_URL",
		"GITLAB_CI",

		"GITHUB_TOKEN",
		"GITHUB_HEAD_REF",
		"BITBUCKET_CLONE_DIR",

		"SUPABASE_ACCESS_TOKEN",
		"SUPABASE_HOME",
		"SYSTEMROOT",
		"SUPABASE_SSL_DEBUG",
		"SUPABASE_CA_SKIP_VERIFY",
		"SSL_CERT_FILE",
		"SSL_CERT_DIR",
		"NPM_CONFIG_REGISTRY",
		"SUPABASE_SERVICE_ROLE_KEY",
		"SUPABASE_PROJECT_ID",
		"SUPABASE_POSTGRES_URL",
		"SUPABASE_ENV",
	}

	// EnvSignalValueKeys lists environment variables whose trimmed values may be
	// recorded inside the "env_signals" property.
	EnvSignalValueKeys = [...]string{
		"AI_AGENT",
		"CURSOR_EXTENSION_HOST_ROLE",
		"TERM",
		"TERM_PROGRAM",
		"TERM_PROGRAM_VERSION",
		"TERM_COLOR_MODE",
	}
)

Functions

func CacheProjectAndIdentifyGroups

func CacheProjectAndIdentifyGroups(project api.V1ProjectWithDatabaseResponse, service *Service, fsys afero.Fs)

CacheProjectAndIdentifyGroups writes project metadata to linked-project.json and fires GroupIdentify for the org and project so PostHog has group metadata. This matches the behavior of the `supabase link` flow.

The caller is responsible for fetching the project from the API and checking auth — this function only handles caching and PostHog group identification.

Best-effort: logs errors to debug output, never returns them.

func Disabled

func Disabled(fsys afero.Fs, now time.Time) (bool, error)

func HasLinkedProject

func HasLinkedProject(fsys afero.Fs) bool

HasLinkedProject reports whether a cached linked-project.json exists.

func SaveLinkedProject

func SaveLinkedProject(project api.V1ProjectWithDatabaseResponse, fsys afero.Fs) error

func SaveState

func SaveState(state State, fsys afero.Fs) error

func TrackUpgradeSuggested

func TrackUpgradeSuggested(ctx context.Context, featureKey, orgSlug string)

TrackUpgradeSuggested fires an EventUpgradeSuggested telemetry event. Safe to call with any context; no-ops when telemetry is not configured.

func WithCommandContext

func WithCommandContext(ctx context.Context, cmd CommandContext) context.Context

func WithService

func WithService(ctx context.Context, service *Service) context.Context

Types

type Analytics

type Analytics interface {
	Enabled() bool
	Capture(distinctID string, event string, properties map[string]any, groups map[string]string) error
	Identify(distinctID string, properties map[string]any) error
	Alias(distinctID string, alias string) error
	GroupIdentify(groupType string, groupKey string, properties map[string]any) error
	Close() error
}

type Client

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

func NewClient

func NewClient(apiKey string, endpoint string, baseProperties map[string]any, factory constructor) (*Client, error)

func (*Client) Alias

func (c *Client) Alias(distinctID string, alias string) error

func (*Client) Capture

func (c *Client) Capture(distinctID string, event string, properties map[string]any, groups map[string]string) error

func (*Client) Close

func (c *Client) Close() error

func (*Client) Enabled

func (c *Client) Enabled() bool

func (*Client) GroupIdentify

func (c *Client) GroupIdentify(groupType string, groupKey string, properties map[string]any) error

func (*Client) Identify

func (c *Client) Identify(distinctID string, properties map[string]any) error

type CommandContext

type CommandContext struct {
	RunID   string
	Command string
	Flags   map[string]any
	Groups  map[string]string
}

type LinkedProject

type LinkedProject struct {
	Ref              string `json:"ref"`
	Name             string `json:"name"`
	OrganizationID   string `json:"organization_id"`
	OrganizationSlug string `json:"organization_slug"`
}

func LoadLinkedProject

func LoadLinkedProject(fsys afero.Fs) (LinkedProject, error)

type Options

type Options struct {
	Analytics  Analytics
	Now        func() time.Time
	IsTTY      bool
	IsCI       bool
	IsAgent    bool
	EnvSignals map[string]any
	CLIName    string
	GOOS       string
	GOARCH     string
}

type Service

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

func FromContext

func FromContext(ctx context.Context) *Service

func NewService

func NewService(fsys afero.Fs, opts Options) (*Service, error)

func (*Service) Capture

func (s *Service) Capture(ctx context.Context, event string, properties map[string]any, groups map[string]string) error

Property catalog: see events.go.

func (*Service) ClearDistinctID

func (s *Service) ClearDistinctID() error

func (*Service) Close

func (s *Service) Close() error

func (*Service) GroupIdentify

func (s *Service) GroupIdentify(groupType string, groupKey string, properties map[string]any) error

func (*Service) NeedsIdentityStitch

func (s *Service) NeedsIdentityStitch() bool

func (*Service) StitchLogin

func (s *Service) StitchLogin(distinctID string) error

type State

type State struct {
	Enabled           bool      `json:"enabled"`
	DeviceID          string    `json:"device_id"`
	SessionID         string    `json:"session_id"`
	SessionLastActive time.Time `json:"session_last_active"`
	DistinctID        string    `json:"distinct_id,omitempty"`
	SchemaVersion     int       `json:"schema_version"`
}

func LoadOrCreateState

func LoadOrCreateState(fsys afero.Fs, now time.Time) (State, bool, error)

func LoadState

func LoadState(fsys afero.Fs) (State, error)

func SetEnabled

func SetEnabled(fsys afero.Fs, enabled bool, now time.Time) (State, error)

func Status

func Status(fsys afero.Fs, now time.Time) (State, bool, error)

Jump to

Keyboard shortcuts

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