Help us shape the future of Go! Take the 2026 Go Developer Survey

instance

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: AGPL-3.0 Imports: 29 Imported by: 0

Documentation

Overview

Package instance owns the Instance's own state (ADR 0032): the one row that no Workspace owns (its random Instance id, the Telemetry report setting, the acknowledged notice) and the rule of who decides about it. It reads across Workspaces, so it holds the raw ent client.

Index

Constants

This section is empty.

Variables

View Source
var ErrTelemetryLocked = errors.New("the telemetry setting is decided by the environment")

ErrTelemetryLocked is returned when the Owner tries to change a setting the environment decides.

Functions

func BuildReport

func BuildReport(ctx context.Context, client *ent.Client, host Host, now time.Time) (telemetryapi.TelemetryReport, error)

BuildReport assembles the Telemetry report (ADR 0032) from core tables only: never from an Enterprise Usage snapshot, which a plain install does not have. It is the one builder, the preview and the daily sender both call it. Counts leave it only as orders of magnitude, so an exact number never does, and it reads nothing that names a Workspace or a person.

func DeployShapeOf

func DeployShapeOf(container string) telemetryapi.TelemetryDeployShape

DeployShapeOf maps what upgrade.DetectContainer found ("helm" on Kubernetes, "docker" in a container, "" otherwise) to the schema's deploy shape.

func ReportOffset

func ReportOffset(instanceID string) time.Duration

ReportOffset is how far into the day (UTC) the Instance reports: a fixed 0 to 6 hours derived from its Instance id, so it is the same every day and after every restart.

Types

type Environment

type Environment struct {
	Telemetry  *bool
	DoNotTrack bool
	// ReleaseCheck is SPHERICON_RELEASE_CHECK; it never depends on the two above.
	ReleaseCheck bool
	// Dev is a development or test environment: neither the Telemetry report nor the
	// Release check ever runs there.
	Dev bool
	// SchemaURL is the page that documents the Telemetry report's schema.
	SchemaURL string
}

Environment is what the deployment says about the Instance: SPHERICON_TELEMETRY when set to true or false, DO_NOT_TRACK=1, SPHERICON_RELEASE_CHECK and the version that runs.

func EnvironmentOf

func EnvironmentOf(cfg *config.Config) Environment

EnvironmentOf reads the Instance switches from the configuration.

type Host

type Host struct {
	AppVersion  string
	Edition     telemetryapi.TelemetryEdition
	DeployShape telemetryapi.TelemetryDeployShape
	OS          string
	Arch        string
	Locale      telemetryapi.TelemetryLocale
	// LicenseExpiredAt is when the license key expired; zero unless it has lapsed (ADR 0035).
	LicenseExpiredAt time.Time
}

Host is what the report says about the running binary and its surroundings. The builder never reads the process (build info, environment, files): the caller hands these in, so a test builds the same report every time.

func HostOf

func HostOf(cfg *config.Config, version string, licenseExpiredAt time.Time) Host

HostOf reads the Host of this process: the version it was built as, the edition its license key switches on (a bad key fails boot, so a key that is set is a valid one, and a lapsed one, whose expiry the caller passes, runs the core: ADR 0035), where it runs and the configured locale.

type Lock

type Lock string

Lock names what decides a setting the Owner cannot change.

const (
	// LockNone means the Owner decides.
	LockNone Lock = ""
	// LockEnvironment means SPHERICON_TELEMETRY decides.
	LockEnvironment Lock = "environment"
	// LockDoNotTrack means DO_NOT_TRACK=1 decides (always off).
	LockDoNotTrack Lock = "do_not_track"
)

type Module

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

Module is the Instance state and the rules around it.

func New

func New(client *ent.Client, env Environment, host Host, now func() time.Time, opts ...Option) *Module

New builds the module; a nil clock means time.Now. The host is what the Telemetry report says about the running binary; its AppVersion is the version the Release check compares to.

func (*Module) AcknowledgeNotice

func (m *Module) AcknowledgeNotice(ctx context.Context) (State, error)

AcknowledgeNotice records that the one-time notice was seen; the first time stands.

func (*Module) AnnounceRelease

func (m *Module) AnnounceRelease(ctx context.Context) error

AnnounceRelease logs the cached newer release, if any: called at start, so a Self-hoster's logs show it before the next daily check.

func (*Module) CheckRelease

func (m *Module) CheckRelease(ctx context.Context) error

CheckRelease asks the source for the newest release and stores it with the check time only when it is strictly newer than the running version and not a prerelease. A source that cannot be reached leaves the previous result and fails nothing. A build that is not a release, or SPHERICON_RELEASE_CHECK=false, asks nobody.

func (*Module) Ensure

func (m *Module) Ensure(ctx context.Context) (*ent.InstanceState, error)

Ensure returns the Instance state row, creating it (with a fresh random Instance id) when this is the first start. Safe to call concurrently and repeatedly.

func (*Module) IsOwner

func (m *Module) IsOwner(ctx context.Context, userID int64) (bool, error)

IsOwner reports whether the User is an Owner of the oldest existing Workspace: the next oldest when that one is deleted, every Owner when there are several, and a transfer follows the role. These Owners alone decide about the Instance.

func (*Module) Jobs

func (m *Module) Jobs() jobs.Extension

Jobs is the daily Release check, handed to the job client as an extension. A restart checks again at once, which is harmless: the check sends nothing.

func (*Module) LatestRelease

func (m *Module) LatestRelease(ctx context.Context) (string, error)

LatestRelease is the cached result of the Release check: a stable release newer than the running version, or empty. It asks nobody.

func (*Module) MarkReportSent

func (m *Module) MarkReportSent(ctx context.Context, at time.Time) error

MarkReportSent stores when the last Telemetry report went out.

func (*Module) Report

Report is the Telemetry report as the Instance would send it now, whether or not sending is on: the same builder the daily sender uses.

func (*Module) SetTelemetry

func (m *Module) SetTelemetry(ctx context.Context, enabled bool) (State, error)

SetTelemetry stores the Owner's choice; ErrTelemetryLocked when the environment decides.

func (*Module) Setting

func (m *Module) Setting(ctx context.Context) (Setting, error)

Setting is the effective Telemetry report setting right now.

func (*Module) State

func (m *Module) State(ctx context.Context) (State, error)

State is the Instance state an Owner sees.

type Option

type Option func(*Module)

Option configures the module.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the logger of the Release check's lines; the default is slog's.

func WithReleaseSource

func WithReleaseSource(s ReleaseSource) Option

WithReleaseSource sets where the Release check asks for the newest release. Without one the check does nothing.

type ReleaseCheckArgs

type ReleaseCheckArgs struct{}

ReleaseCheckArgs is the daily tick of the Release check.

func (ReleaseCheckArgs) Kind

func (ReleaseCheckArgs) Kind() string

type ReleaseSource

type ReleaseSource interface {
	LatestRelease(ctx context.Context) (string, error)
}

ReleaseSource answers "what is the newest stable release", as a bare version: the one lookup `sphericon upgrade` builds, so there is no second way to ask (ADR 0032).

type SendReportArgs

type SendReportArgs struct{}

SendReportArgs is the tick that sends the Telemetry report when one is due.

func (SendReportArgs) Kind

func (SendReportArgs) Kind() string

type Sender

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

Sender sends the daily Telemetry report (ADR 0032). It sends nothing in a development environment or from a build that is not a release, and otherwise only while the effective setting is on, the Owner's notice is acknowledged and a report is due.

func NewSender

func NewSender(mod *Module, cfg *config.Config, opts ...SenderOption) (*Sender, error)

NewSender builds the sender over the receiver address in cfg. A development environment (the module's Environment.Dev) never sends, so a test opts in by building its config as production.

func (*Sender) Jobs

func (s *Sender) Jobs() jobs.Extension

Jobs is the sender's worker and periodic job, handed to the job client as an extension.

func (*Sender) Run

func (s *Sender) Run(ctx context.Context) error

Run sends the report when every gate lets it through. A failed send is logged at debug level and left for the next cycle; only a failure to read or store the state is an error.

type SenderOption

type SenderOption func(*Sender)

SenderOption tunes a Sender.

type Setting

type Setting struct {
	Enabled  bool
	Locked   bool
	LockedBy Lock
}

Setting is the Telemetry report setting as it applies: whether reports are sent and whether the environment (not the Owner) decides.

func Effective

func Effective(env Environment, stored bool) Setting

Effective applies ADR 0032 to the environment and the stored in-app setting (on until an Owner switches it off): off if SPHERICON_TELEMETRY is false or DO_NOT_TRACK=1; on and locked if SPHERICON_TELEMETRY is true; otherwise the stored setting decides. DO_NOT_TRACK only ever switches off. The sender of the report reuses it.

type State

type State struct {
	Telemetry          Setting
	NoticeAcknowledged bool
	// LatestRelease is the bare version of a newer stable release, empty when there is none.
	LatestRelease string
	// LicenseExpiredAt is when the license key expired; zero unless it has lapsed (ADR 0035).
	LicenseExpiredAt time.Time
	// SchemaURL is the page that documents the Telemetry report's schema.
	SchemaURL string
}

State is what an Owner sees about the Instance.

Jump to

Keyboard shortcuts

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