profiler

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: GPL-3.0 Imports: 10 Imported by: 0

README

profiler

Import path: github.com/InsideGallery/core/profiler

Overview

profiler exposes health checks, Kubernetes-style probes, Prometheus metrics, and pprof handlers through a standalone HTTP monitor server.

Main APIs

  • State owns health checks and startup/readiness flags.
  • NewState creates isolated profiler state; DefaultState returns package-level compatibility state.
  • AddHealthCheck, CheckHealth, ExecuteHealthCheck, and Monitor operate on the default state.
  • (*State).AddHealthCheck, CheckHealth, Reset, SetStarted, IsStarted, SetReady, IsReady, and Monitor operate on explicit state.
  • Started and Ready are package-level atomic probe flags used by compatibility helpers.
  • ErrServiceIsOffline is a reusable health-check error value.

Usage

state := profiler.NewState()
state.AddHealthCheck(func() error {
	return nil
})

shutdown := state.Monitor(":8011")
defer shutdown()

state.SetStarted(true)
state.SetReady(true)

Endpoints And Operations

Monitor(addr) is a no-op when addr is empty. Otherwise it starts an HTTP server with /metrics, /healthz, /readyz, /livez, /startupz, and /debug/pprof/* endpoints, and returns a shutdown function.

Health checks run concurrently and are joined with errors.Join. /healthz and /readyz return HTTP 503 when checks fail; /readyz also requires the ready flag. /livez returns OK when the process can respond. /startupz depends on the started flag.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Started is true once the app has initialized successfully (DB connected, etc.).
	Started atomic.Bool
	// Ready is true once the server is accepting traffic (Fiber listening / NATS subscribed).
	Ready atomic.Bool
)

Probe flags for Kubernetes. Set by the application lifecycle (main.go).

View Source
var ErrServiceIsOffline = errors.New("service is offline")

ErrServiceIsOffline indicates that a health check dependency is offline.

Functions

func AddHealthCheck

func AddHealthCheck(f func() error)

AddHealthCheck registers a function called on /healthz, /readyz, and /livez. If any check returns an error the service is considered unhealthy.

func CheckHealth

func CheckHealth() error

CheckHealth runs every registered health check concurrently and returns a joined error if any fail. Exported so the main app server can expose /healthz on the Traefik-facing port without auth.

func ExecuteHealthCheck

func ExecuteHealthCheck() error

ExecuteHealthCheck runs every registered health check.

func Monitor

func Monitor(addr string) func()

Monitor starts a standalone HTTP server on addr exposing health probes and pprof. It should be called as early as possible in main(), before DB/NATS/app init. Returns a shutdown function that should be deferred.

Usage:

shutdown := profiler.Monitor(":8011")
defer shutdown()

Types

type State

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

State owns profiler health checks and probe flags.

func DefaultState

func DefaultState() *State

DefaultState returns the package-level compatibility profiler state.

func NewState

func NewState() *State

NewState returns isolated profiler health and probe state.

func (*State) AddHealthCheck

func (s *State) AddHealthCheck(f func() error)

AddHealthCheck registers a function called on /healthz, /readyz, and /livez. If any check returns an error the service is considered unhealthy.

func (*State) CheckHealth

func (s *State) CheckHealth() error

CheckHealth runs every registered health check concurrently and returns a joined error if any fail.

func (*State) IsReady

func (s *State) IsReady() bool

IsReady returns the readiness probe flag.

func (*State) IsStarted

func (s *State) IsStarted() bool

IsStarted returns the startup probe flag.

func (*State) Monitor

func (s *State) Monitor(addr string) func()

Monitor starts a standalone HTTP server on addr using this state.

func (*State) Reset

func (s *State) Reset()

Reset clears health checks and probe flags.

func (*State) SetReady

func (s *State) SetReady(ready bool)

SetReady stores the readiness probe flag.

func (*State) SetStarted

func (s *State) SetStarted(started bool)

SetStarted stores the startup probe flag.

Jump to

Keyboard shortcuts

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