appinit

package
v0.40.416 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: MIT Imports: 21 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package appinit wires common service start-up concerns: logging setup, the metrics pipeline and optional CPU profiling. It defines the Flags and LogConfig structs (tagged for github.com/alecthomas/kong CLI parsing) that services embed in their command line definitions.

Each initializer returns an io.Closer (possibly nil) that the caller should close on shutdown, typically in reverse order:

logCloser, err := appinit.Logs(&flags.LogConfig, "myservice")
if err != nil {
	return err
}
if logCloser != nil {
	defer logCloser.Close()
}

prof, err := appinit.CPUProfiler(flags.CPUProfile) // nil closer when empty
if err != nil {
	return err
}
if prof != nil {
	defer prof.Close()
}

mc, err := appinit.Metrics(&cfg.Metrics, "myservice", "cluster-1", version, commit, myMetrics)
if err != nil {
	return err
}
if mc != nil {
	defer mc.Close()
}

Metrics and Logs mutate process-global state (xlog formatter, the global metrics sink, the default Prometheus registry) and are meant to be called once per process. Metrics also registers an xlog error hook that counts logged errors in metricskey.HealthLogErrors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CPUProfiler

func CPUProfiler(file string) (io.Closer, error)

CPUProfiler starts a CPU profile written to file and returns a closer that stops it. It returns a nil closer and nil error when file is empty or "/dev/null". The profile file handle is not closed by the closer.

func Logs

func Logs(flags *LogConfig, serviceName string) (io.Closer, error)

Logs configures the process-global xlog formatter from flags and logs a "service_starting" line with os.Args. When LogDir is set, log rotation is initialized under LogDir/<serviceName>.log and the returned closer must be closed at shutdown; otherwise the closer is nil. LogDir "/dev/null" discards all output.

func Metrics

func Metrics(cfg *config.Metrics, svcName, clusterName string, version string, commitNumber int, describe []*metrics.Describe) (io.Closer, error)

Metrics initializes the global metrics pipeline from cfg. Provider is a comma-separated list of "prometheus", "cloudwatch" and "inmem"; an empty provider or Disabled=true is a no-op returning (nil, nil). Prometheus registers with the default registry and, when Prometheus.Addr is set, serves promhttp on that address in a goroutine (fatal on listen error). CloudWatch starts a publishing goroutine; the returned closer flushes it. GlobalTags accepts "service", "cluster_id" and "node" (from $NODE_NAME). describe lists the caller's metric descriptors, merged with metricskey.Metrics for Prometheus help text. It returns an error if a sink is already initialized or a provider is unknown.

Types

type Flags

type Flags struct {
	Version ctl.VersionFlag `name:"version" help:"Print version information and quit" hidden:""`

	Cfg         string `short:"c" help:"load configuration file"`
	CfgOverride string `help:"configuration override file"`
	CPUProfile  string `help:"enable CPU profiling, specify a file to store CPU profiling info"`
	DryRun      bool   `help:"verify config etc, and do not start the service"`

	ClientCert      string `help:"Path to the client TLS cert file"`
	ClientKey       string `help:"Path to the client TLS key file"`
	ClientTrustedCA string `help:"Path to the client TLS trusted CA file"`
	Env             string `help:"Override environment value"`
	ServiceName     string `help:"Override service value"`
	Region          string `help:"Override region value"`
	Cluster         string `help:"Override cluster value"`

	WaitOnExit int `help:"Number of seconds to wait on exist"`
}

Flags holds the command line flags shared by services (kong tags): config file paths, CPU profiling output, dry-run, client TLS files and environment/service/region/cluster overrides.

type LogConfig

type LogConfig struct {
	LogStd         bool   `help:"output logs to stderr"`
	LogDebug       bool   `help:"output logs with debug info, such as filename:line"`
	LogPretty      bool   `help:"output logs in pretty format, with colors"`
	LogJSON        bool   `help:"output logs in JSON format"`
	LogStackdriver bool   `help:"output logs in GCP stackdriver format"`
	LogDir         string `help:"Store logs in folder"`
}

LogConfig holds the logging command line flags consumed by Logs. The help tags are for kong. Precedence: LogDir (file rotation) > "/dev/null" (discard) > LogStackdriver > LogJSON > LogPretty > plain text.

Directories

Path Synopsis
Package config defines the metrics pipeline configuration consumed by appinit.Metrics.
Package config defines the metrics pipeline configuration consumed by appinit.Metrics.

Jump to

Keyboard shortcuts

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