links

package
v0.1.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: 26 Imported by: 0 modules, 0 packages

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 links holds the links of C-12.FR-9: the Organization's Lookup tables — named columns and rows of a key and one value per column, which a template reads one cell of with `lookup "<table>" <key> "<column>"` — and its Link rules — a name, Matchers checked against an Alert Group's common labels, a scope and a URL template run in the sandbox of ADR-0012 — with the built-in "Explore" rule. It computes the links of an Alert Group: one per matching rule, or per value of a label, then the runbook_url and dashboard_url annotations and the generatorURL as "Source", each kept only as an http(s) URL. A rule that fails is left out and counted. Every change of a table or a rule is recorded in the Audit log; neither changes a message by itself.

Index

Constants

View Source
const (
	ExploreName  = "Explore"
	ExploreTable = "grafana"
)

The built-in "Explore" rule (C-12.FR-9): its name, and the Lookup table it reads, keyed by the environment label, else the cluster label, with the columns address and datasource_uid.

View Source
const (
	ActionTableCreated = "lookup_table.created"
	ActionTableUpdated = "lookup_table.updated"
	ActionTableDeleted = "lookup_table.deleted"
	ActionRuleCreated  = "link_rule.created"
	ActionRuleUpdated  = "link_rule.updated"
	ActionRuleDeleted  = "link_rule.deleted"
	ResourceTable      = "lookup_table"
	ResourceRule       = "link_rule"
)

The Audit log actions and resource types of Lookup tables and Link rules (C-03.FR-14).

View Source
const (
	MaxNameLength        = 200
	MaxDescriptionLength = 2000
	MaxValueLength       = 4096
	MaxColumns           = 50
	MaxEntries           = 10_000
	MaxLookups           = 100
	// MaxTablesPage bounds a page of Lookup tables, which carry their rows; the list continues with its cursor
	// (`lookup_table.page_max`).
	MaxTablesPage = 50
)

The limits of a Lookup table and of the lookups of one render: names and column names in characters, the description, a key or a value, the columns and rows of a table (`lookup_table.size_max`), and the rows one render reads (`template.lookups_per_render`).

View Source
const (
	CodeInvalidFormat   = "invalid_format"
	CodeTooLong         = "too_long"
	CodeDuplicate       = "duplicate"
	CodeColumnMismatch  = "column_mismatch"
	CodeInvalidRegex    = "invalid_regex"
	CodeRequired        = "required"
	CodeTemplateSyntax  = templates.CodeSyntax
	CodeUnknownFunction = templates.CodeUnknownFunction
)

The codes of FieldError, as the API names them.

View Source
const (
	KindRule      = "link_rule"
	KindRunbook   = "runbook"
	KindDashboard = "dashboard"
	KindSource    = "source"
)

The kinds of a link, as the API names them: a Link rule's, or the runbook_url and dashboard_url annotations and the generatorURL of the Alerts.

View Source
const (
	NameRunbook   = "Runbook"
	NameDashboard = "Dashboard"
	NameSource    = "Source"
)

The names of the links that are not a Link rule's, as the API shows them; messages translate them.

View Source
const (
	ScopeAlertGroup = "alert_group"
	ScopeLabelValue = "label_value"
)

The scopes of a Link rule: one link for the Alert Group, or one per distinct value of a label among its Alerts.

View Source
const DryRunSample = 20

DryRunSample is template.dry_run_sample: the most recent Stored Snapshots a URL template is dry-run against.

View Source
const ExploreTemplate = `` /* 808-byte string literal not displayed */

ExploreTemplate is the URL template of the built-in rule: the PromQL expression of the first firing Alert's generatorURL (its g0.expr parameter), opened in Grafana Explore at the address with the data source of the Lookup table "grafana", in Grafana's documented Explore URL format. Without the environment and the cluster label, the table, the row, a cell or an expression it writes nothing, which is no link and no error.

View Source
const MaxRules = 1000

MaxRules is the most Link rules an Alert Group's links are computed from.

View Source
const ReadAlerts = 1000

ReadAlerts is the most Alerts of an Alert Group its links are computed from, as many as its messages read.

Variables

View Source
var (
	// ErrTableNotFound is a Lookup table that does not exist in the Organization.
	ErrTableNotFound = errors.New("no such lookup table")
	// ErrRuleNotFound is a Link rule that does not exist in the Organization.
	ErrRuleNotFound = errors.New("no such link rule")
	// ErrTableNameTaken is a name another Lookup table has.
	ErrTableNameTaken = errors.New("another lookup table has this name")
	// ErrRuleNameTaken is a name another Link rule has.
	ErrRuleNameTaken = errors.New("another link rule has this name")
	// ErrVersionMismatch is an If-Match that names another version of the table or the rule.
	ErrVersionMismatch = errors.New("the lookup table or link rule changed since it was read")
	// ErrInUse is the deletion or the renaming of a Lookup table that the URL template of a Link rule reads; the
	// error is an InUseError that names the rules.
	ErrInUse = errors.New("a link rule reads the lookup table")
	// ErrBuiltinImmutable is the deletion of the built-in "Explore" rule, or a change of its name or scope.
	ErrBuiltinImmutable = errors.New("the built-in link rule cannot be deleted, renamed or given another scope")
)
View Source
var ErrNotFound = errors.New("no such alert group")

ErrNotFound is an Alert Group that does not exist in the Organization.

Functions

func EnsureExplore

func EnsureExplore(ctx context.Context, q Queries, orgID int64, now time.Time) error

EnsureExplore creates the built-in "Explore" rule once per Organization, as a start-up ensure step: no Matchers, the scope alert_group and ExploreTemplate. When the rule exists, it replaces a URL template that is still one of the previous built-in templates with ExploreTemplate, a new version of the rule, and changes nothing else: an edited template and the Matchers are kept, and a second run changes nothing. A rule that is not the built-in one and already has its name stops the start with ErrRuleNameTaken.

Types

type Config

type Config struct {
	OrgID     int64
	Store     Store
	Audit     *audit.Writer
	Business  clock.Clock
	Real      clock.Clock
	PublicURL string
	Log       *logging.Logger
}

Config is what a Service needs: the Organization, the Store, the Audit log writer, the business clock that dates the rows and the templates' `now`, the real clock of the sandbox's execution limit and of the render durations, MUSTER_PUBLIC_URL for the links to Alert Groups in the template data, and the logger.

type DBTX

type DBTX = dbgen.DBTX

DBTX is the pool, connection or transaction a render reads through.

type Data

type Data struct {
	templates.Data
	Labels templates.KV
	Value  string
}

Data is what a Link rule's URL template sees: the data of a message template, Labels — the common labels for the scope alert_group, the labels of the first Alert with the value for label_value — and Value, the label's value.

type Entry

type Entry struct {
	Key    string            `json:"key"`
	Values map[string]string `json:"values"`
}

Entry is one row of a Lookup table: its key and one value per column.

type FieldError

type FieldError struct {
	Pointer string
	Code    string
	Detail  string
	Line    int
	Column  int
}

FieldError is a field of a request that is not valid, at a JSON pointer of the request body, with a stable code of the validation-failed problem and, for a template, the line and column.

func (*FieldError) Error

func (e *FieldError) Error() string

type InUseError

type InUseError struct {
	Rules []RuleRef
}

InUseError is the deletion or the renaming of a Lookup table that the URL templates of Link rules read by its name, with those rules in the order they were created. It is ErrInUse.

func (*InUseError) Error

func (e *InUseError) Error() string

func (*InUseError) Is

func (e *InUseError) Is(target error) bool

Is makes an InUseError match ErrInUse.

type Input

type Input struct {
	Group string
	Route string
	Data  templates.Data
}

Input is an Alert Group as its links are computed from: its public_id and its Route's, which the metric and the log name — both empty for a sample, whose failures are neither counted nor logged — and its template data with the values as received, the Alerts firing first and newest first.

type Link struct {
	Kind string
	Name string
	URL  string
	Rule string
}

Link is one link of an Alert Group: its kind, its name and its http(s) URL, and for the kind KindRule the public_id of its Link rule.

type ListFilter

type ListFilter struct {
	After *int64
	Limit int
}

ListFilter selects a page in the order of creation, after the id After when it is set.

type Matcher

type Matcher struct {
	Label string `json:"label"`
	Op    string `json:"op"`
	Value string `json:"value"`
}

Matcher is one Matcher of a Link rule, in the Alertmanager syntax.

type Queries

type Queries interface {
	ListLookupTables(ctx context.Context, arg dbgen.ListLookupTablesParams) ([]dbgen.ListLookupTablesRow, error)
	GetLookupTable(ctx context.Context, arg dbgen.GetLookupTableParams) (dbgen.GetLookupTableRow, error)
	LockLookupTable(ctx context.Context, arg dbgen.LockLookupTableParams) (int64, error)
	ListLookupEntries(ctx context.Context, arg dbgen.ListLookupEntriesParams) ([]dbgen.ListLookupEntriesRow, error)
	InsertLookupTable(ctx context.Context, arg dbgen.InsertLookupTableParams) (int64, error)
	UpdateLookupTable(ctx context.Context, arg dbgen.UpdateLookupTableParams) error
	DeleteLookupEntries(ctx context.Context, arg dbgen.DeleteLookupEntriesParams) error
	InsertLookupEntries(ctx context.Context, arg dbgen.InsertLookupEntriesParams) error
	DeleteLookupTable(ctx context.Context, arg dbgen.DeleteLookupTableParams) error
	GetLookupCells(ctx context.Context, arg dbgen.GetLookupCellsParams) ([]byte, error)
	ListLinkRules(ctx context.Context, arg dbgen.ListLinkRulesParams) ([]dbgen.ListLinkRulesRow, error)
	GetLinkRule(ctx context.Context, arg dbgen.GetLinkRuleParams) (dbgen.GetLinkRuleRow, error)
	LockLinkRule(ctx context.Context, arg dbgen.LockLinkRuleParams) (int64, error)
	ListLinkRuleMatchers(ctx context.Context, arg dbgen.ListLinkRuleMatchersParams) (
		[]dbgen.ListLinkRuleMatchersRow, error)
	ListLinkRuleTemplates(ctx context.Context, orgID int64) ([]dbgen.ListLinkRuleTemplatesRow, error)
	InsertLinkRule(ctx context.Context, arg dbgen.InsertLinkRuleParams) (int64, error)
	UpdateLinkRule(ctx context.Context, arg dbgen.UpdateLinkRuleParams) error
	DeleteLinkRuleMatchers(ctx context.Context, arg dbgen.DeleteLinkRuleMatchersParams) error
	InsertLinkRuleMatcher(ctx context.Context, arg dbgen.InsertLinkRuleMatcherParams) error
	DeleteLinkRule(ctx context.Context, arg dbgen.DeleteLinkRuleParams) error
	EnsureExploreRule(ctx context.Context, arg dbgen.EnsureExploreRuleParams) (string, error)
	GetLinkSnapshotRetention(ctx context.Context, orgID int64) (int64, error)
	ListRecentSnapshots(ctx context.Context, arg dbgen.ListRecentSnapshotsParams) ([][]byte, error)
	GetLinkGroup(ctx context.Context, arg dbgen.GetLinkGroupParams) (dbgen.GetLinkGroupRow, error)
	ListLinkAlerts(ctx context.Context, arg dbgen.ListLinkAlertsParams) ([]dbgen.ListLinkAlertsRow, error)
	audit.Store
}

Queries are the queries of the package, with the insert of the Audit log.

type Requester

type Requester struct {
	Actor     audit.Actor
	Transport audit.Transport
	Address   netip.Addr
}

Requester is who asks for a change and how: the actor and the Transport of the Audit log entry, and the client address.

type Rule

type Rule struct {
	ID          int64
	PublicID    string
	Name        string
	Builtin     bool
	Matchers    []Matcher
	Scope       Scope
	URLTemplate string
	CreatedAt   time.Time
	Version     int64
	// contains filtered or unexported fields
}

Rule is a Link rule as the API reads it.

type RuleInput

type RuleInput struct {
	Name        string
	Matchers    []Matcher
	Scope       Scope
	URLTemplate string
}

RuleInput is what createLinkRule and updateLinkRule write.

type RulePage

type RulePage struct {
	Rules []Rule
	Next  *int64
}

RulePage is a page of Link rules; Next, the id to continue after, is nil on the last page.

type RuleRef

type RuleRef struct {
	PublicID string
	Name     string
}

RuleRef names a Link rule: its public_id and its name.

type Scope

type Scope struct {
	Type  string `json:"type"`
	Label string `json:"label,omitempty"`
}

Scope is the scope of a Link rule; Label is set for label_value only.

type Service

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

Service holds the Lookup tables and Link rules of an Organization and computes links.

func New

func New(cfg Config) *Service

New returns the Service of cfg.

func (*Service) CreateRule

func (s *Service) CreateRule(ctx context.Context, r Requester, in RuleInput) (Rule, error)

CreateRule creates a Link rule after its dry run. A name another rule has is ErrRuleNameTaken.

func (*Service) CreateTable

func (s *Service) CreateTable(ctx context.Context, r Requester, in TableInput) (Table, error)

CreateTable creates a Lookup table with its rows. A name another table has is ErrTableNameTaken.

func (*Service) DeleteRule

func (s *Service) DeleteRule(ctx context.Context, r Requester, publicID string, version *int64) error

DeleteRule deletes the Link rule publicID; a non-nil version must be its current one. The built-in rule is ErrBuiltinImmutable.

func (*Service) DeleteTable

func (s *Service) DeleteTable(ctx context.Context, r Requester, publicID string, version *int64) error

DeleteTable deletes the Lookup table publicID with its rows; a non-nil version must be its current one. A table that the URL template of a Link rule reads by its name is an InUseError.

func (*Service) Evaluate

func (s *Service) Evaluate(ctx context.Context, db DBTX, in Input) ([]Link, error)

Evaluate computes the links of an Alert Group, reading the Link rules and the Lookup tables through db, or the main pool for a nil db (C-12.FR-9): each rule whose Matchers all match the common labels yields one link, or one per distinct value of its label among the Alerts, in the order the rules were created; then the runbook_url and the dashboard_url of the first Alert that has one, and the generatorURL of the first firing Alert that has one. Every URL is kept only as an absolute http(s) URL, and an empty one is no link. A rule whose template fails is left out: for an Alert Group it is counted in muster_template_errors_total and logged once per rule and Alert Group.

func (*Service) ForGroup

func (s *Service) ForGroup(ctx context.Context, groupID int64) ([]Link, error)

ForGroup computes the links of the Alert Group groupID on read, through the main pool (getAlertGroup).

func (*Service) GetRule

func (s *Service) GetRule(ctx context.Context, publicID string) (Rule, error)

GetRule reads the Link rule publicID.

func (*Service) GetTable

func (s *Service) GetTable(ctx context.Context, publicID string) (Table, error)

GetTable reads the Lookup table publicID with its rows.

func (*Service) ListRules

func (s *Service) ListRules(ctx context.Context, f ListFilter) (RulePage, error)

ListRules lists the Link rules in the order they were created.

func (*Service) ListTables

func (s *Service) ListTables(ctx context.Context, f ListFilter) (TablePage, error)

ListTables lists the Lookup tables with their rows.

func (*Service) RenderURL

func (s *Service) RenderURL(ctx context.Context, db DBTX, source string, in Input) (string, error)

RenderURL renders a URL template for the preview of a Link rule against the sample in, with the scope alert_group, reading the Lookup tables through db or the main pool: the link the rule yields — empty when the output is not an absolute http(s) URL, as a message leaves it out — or the *templates.Error of the template.

func (*Service) Sample

func (s *Service) Sample(d templates.Data) Input

Sample is the template data of a Stored Snapshot or of the example as an Alert Group: #1, titled by its alertname, firing when one of its Alerts is, with the labels every Alert shares as its common labels when the webhook has none.

func (*Service) UpdateRule

func (s *Service) UpdateRule(ctx context.Context, r Requester, publicID string, version *int64, in RuleInput) (Rule,
	error)

UpdateRule replaces the fields of the Link rule publicID after the dry run of its URL template; a non-nil version must be its current one (If-Match). The built-in rule keeps its name and scope (ErrBuiltinImmutable).

func (*Service) UpdateTable

func (s *Service) UpdateTable(ctx context.Context, r Requester, publicID string, version *int64, in TableInput) (Table,
	error)

UpdateTable replaces the fields and the rows of the Lookup table publicID; a non-nil version must be its current one (If-Match). A new name for a table that the URL template of a Link rule reads by its name is an InUseError, since the rule would then read nothing.

type Store

type Store interface {
	Queries
	InTx(ctx context.Context, f func(Queries) error) error
}

Store runs the queries over the main pool, alone or in one transaction.

func NewStore

func NewStore(pool *pgxpool.Pool) Store

NewStore is the Store over the main pool.

type Table

type Table struct {
	ID          int64
	PublicID    string
	Name        string
	Description string
	Columns     []string
	Entries     []Entry
	CreatedAt   time.Time
	Version     int64
}

Table is a Lookup table as the API reads it.

type TableInput

type TableInput struct {
	Name        string
	Description *string
	Columns     []string
	Entries     []Entry
}

TableInput is what createLookupTable and updateLookupTable write; the rows replace the stored ones as a whole. A nil Description keeps the stored one on an update and is empty on a creation.

type TablePage

type TablePage struct {
	Tables []Table
	Next   *int64
}

TablePage is a page of Lookup tables; Next, the id to continue after, is nil on the last page.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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