observability

package
v1.13.5 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package observability runs forge's local Grafana stack and renders the dashboard and alert rules that ship with the CLI.

Index

Constants

View Source
const (
	// DashboardFile is the exported Forge Service dashboard.
	DashboardFile = "forge-service-dashboard.json"
	// RulesFile is the exported Prometheus rule file.
	RulesFile = "forge-rules.yaml"
)
View Source
const (
	// Image is the all-in-one Grafana, Tempo, Loki, Prometheus and collector image.
	Image = "grafana/otel-lgtm:0.36.0"
	// ContainerName is the one long-lived stack container.
	ContainerName = "forge-lgtm"
	// VolumeName holds the stack's data across recreates.
	VolumeName = "forge-lgtm-data"
	// NetworkName is the user network the stack runs on. Dev containers
	// join it so they reach the stack, and it them, by container name.
	NetworkName = "forge-observability"
)

Variables

View Source
var (
	// ErrInvalidSLO reports an availability objective outside (0, 100).
	ErrInvalidSLO = errors.New("observability: slo must be greater than 0 and less than 100")
	// ErrInvalidP95 reports a latency threshold that is not positive.
	ErrInvalidP95 = errors.New("observability: p95 threshold must be positive")
)
View Source
var (
	// ErrPortInUse reports a host port another process holds.
	ErrPortInUse = errors.New("observability: port in use")
	// ErrNotReady reports a stack that didn't finish starting in time.
	ErrNotReady = errors.New("observability: stack did not become ready")
)
View Source
var ErrExists = errors.New("observability: file exists")

ErrExists reports an export target that already exists.

View Source
var ErrInvalidTarget = errors.New("observability: invalid target")

ErrInvalidTarget reports a --target value that isn't name=host:port.

Functions

func DashboardJSON

func DashboardJSON() ([]byte, error)

DashboardJSON returns the Forge Service dashboard. The same build always produces the same bytes, which the examples drift test relies on.

func DefaultStateDir

func DefaultStateDir() (string, error)

DefaultStateDir is where the CLI keeps the files it mounts into the stack.

func Export

func Export(dir string, o RulesOptions, force bool) ([]string, error)

Export writes the dashboard and the rule file into dir and returns their paths. It renders and checks everything before it writes anything.

func RemoveTarget

func RemoveTarget(dir, name string) error

RemoveTarget deletes a target file. A missing file is not an error.

func RenderRules

func RenderRules(o RulesOptions) ([]byte, error)

RenderRules renders the Prometheus rule file for o.

func WriteState

func WriteState(dir string) (string, error)

WriteState writes the generated files into dir and returns a hash of their contents. Target files are left alone and are not part of the hash, because Prometheus reads them live.

func WriteTarget

func WriteTarget(dir string, t Target) error

WriteTarget writes a file_sd file for t. It writes a temporary file and renames it, so Prometheus never reads half a file.

Types

type ContainerState

type ContainerState struct {
	Exists  bool
	Running bool
	Hash    string
	Ports   Ports
}

ContainerState is what docker reports about the stack's container.

type Ports

type Ports struct {
	Grafana    int
	OTLP       int
	Prometheus int
}

Ports are the host ports the stack publishes on 127.0.0.1.

func DefaultPorts

func DefaultPorts() Ports

DefaultPorts are the image's own ports.

func ParsePorts

func ParsePorts(s string) (Ports, error)

ParsePorts reads the forge.ports container label.

func (Ports) GrafanaURL

func (p Ports) GrafanaURL() string

GrafanaURL is where Grafana answers on the host.

func (Ports) OTLPURL

func (p Ports) OTLPURL() string

OTLPURL is the OTLP/HTTP endpoint on the host.

func (Ports) PrometheusURL

func (p Ports) PrometheusURL() string

PrometheusURL is where Prometheus answers on the host.

func (Ports) String

func (p Ports) String() string

type RulesOptions

type RulesOptions struct {
	// SLO is the availability objective in percent, for example 99.9.
	SLO float64
	// P95Threshold is the latency above which ForgeHighLatencyP95 fires.
	P95Threshold time.Duration
}

RulesOptions sets the objective and threshold the alert rules use.

func DefaultRulesOptions

func DefaultRulesOptions() RulesOptions

DefaultRulesOptions is a 99.9% objective and a one second p95.

type Stack

type Stack struct {
	Runner       execx.Runner
	Dir          string // state directory, see DefaultStateDir
	Name         string // container name, ContainerName when empty
	Volume       string // data volume, VolumeName when empty
	Network      string // user network, NetworkName when empty
	Image        string // Image when empty
	Ports        Ports
	Out          io.Writer     // progress lines, discarded when nil
	ReadyTimeout time.Duration // 90s when zero
	PollInterval time.Duration // 1s when zero
	// PortFree reports whether a host port is free. It checks 127.0.0.1 when nil.
	PortFree func(ctx context.Context, port int) bool
}

Stack runs grafana/otel-lgtm as one long-lived container with the files from WriteState mounted into it.

func (*Stack) Down

func (s *Stack) Down(ctx context.Context, purge bool) error

Down removes the container. With purge it also removes the data volume and the state directory.

func (*Stack) Inspect

func (s *Stack) Inspect(ctx context.Context) (ContainerState, error)

Inspect reports the container's state. A missing container is not an error.

func (*Stack) Up

func (s *Stack) Up(ctx context.Context) (Ports, error)

Up writes the state directory and makes sure the container runs with it. A container whose configuration differs is recreated; the data volume stays.

type Target

type Target struct {
	Name    string // becomes the job label
	Address string // host:port as seen from inside the container
	// Rewritten is true when ParseTarget replaced a loopback host with
	// host.docker.internal.
	Rewritten bool
}

Target is one app Prometheus scrapes.

func ParseTarget

func ParseTarget(s string) (Target, error)

ParseTarget reads name=host:port. A loopback host means the developer's machine, which the container reaches as host.docker.internal.

type TargetHealth

type TargetHealth struct {
	Job       string
	URL       string
	Health    string
	LastError string
}

TargetHealth is one scrape target as Prometheus sees it.

func Targets

func Targets(ctx context.Context, client *http.Client, prometheusURL string) ([]TargetHealth, error)

Targets asks Prometheus at prometheusURL for its active scrape targets, sorted by job.

Directories

Path Synopsis
Command syncexamples rewrites the dashboard and rules in examples/observability from the copies the forge CLI embeds.
Command syncexamples rewrites the dashboard and rules in examples/observability from the copies the forge CLI embeds.

Jump to

Keyboard shortcuts

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