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. Prometheus binds synchronously, returns bind errors to the caller, and uses bounded HTTP read deadlines. Closing the metrics closer stops its endpoint, the runtime stats collector and the CloudWatch publisher, after the publisher's final publish.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CPUProfiler ¶
CPUProfiler starts the process CPU profile written to file and returns a closer that stops it and closes the file. It returns a nil closer and nil error when file is empty or "/dev/null". It returns an error when the file cannot be created or a CPU profile is already running in the process; a profile started by an earlier CPUProfiler call is detected before file is created, one started otherwise after, and the created file is then removed; concurrent calls fail as already running until the removal completes.
func Logs ¶
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) (closer io.Closer, err 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, binds synchronously and serves promhttp with bounded HTTP read deadlines. Bind errors are returned; unexpected serve errors are logged. The returned closer stops the HTTP endpoint, including active connections. CloudWatch starts a publishing goroutine; the returned closer stops it and waits for its final publish. With a Prometheus or CloudWatch sink, EnableRuntimeMetrics starts a runtime stats collector; the returned closer signals it to stop. 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 exit"`
}
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.