datacenterresource

package datacenterresource

import "chantico/internal/datacenterresource"

Index

Constants

const (
	StateInit             = "Init"
	StateEntry            = "Entry point"
	StateValidationFailed = "Validation Failed"
	StateDelete           = "Delete"
	StateEnd              = "End point"
)
const (
	DataCenterResourceTypePDU        = "pdu"
	DataCenterResourceTypeBaremetal  = "baremetal"
	DataCenterResourceTypeVM         = "vm"
	DataCenterResourceTypeKubernetes = "kubernetes"
	DataCenterResourceTypeHeat       = "heat"
)

Variables

var StateMachine = sm.Machine[*chantico.DataCenterResource]{
	Actions: map[string][]sm.ActionFunction[*chantico.DataCenterResource]{
		StateInit: {
			{Type: sm.ActionFunctionPure, Pure: sm.InitializeFinalizer[*chantico.DataCenterResource]},
		},
		StateEntry: {
			{Type: sm.ActionFunctionPure, Pure: WriteRuleFile},
		},
		StateDelete: {
			{Type: sm.ActionFunctionPure, Pure: DeleteRuleFile},
			{Type: sm.ActionFunctionPure, Pure: sm.RemoveFinalizer[*chantico.DataCenterResource]},
		},

		StateValidationFailed: {},
		StateEnd:              {},
	},
	FailState: StateValidationFailed,
}

Functions

func ClearValidationError

func ClearValidationError(
	dataCenterResource *chantico.DataCenterResource,
)

func CoefficientMetricName

func CoefficientMetricName(parentName, childName string) string

CoefficientMetricName returns the deterministic Prometheus metric name for the coefficient from parent to child.

func DeleteRuleFile

func DeleteRuleFile(
	ctx context.Context,
	dataCenterResource *chantico.DataCenterResource,
) *sm.ActionResult

DeleteRuleFile removes the Prometheus recording rule file for this DataCenterResource. After deleting, Prometheus is sent a reload request so it stops evaluating the removed rules.

func EnergyMetricName

func EnergyMetricName(resourceName string) string

EnergyMetricName returns the deterministic Prometheus metric name for a DataCenterResource's energy timeseries.

func FormatResources

func FormatResources(resources []chantico.DataCenterResource) string

func GetFromMap

func GetFromMap(
	resourcesMap map[string]chantico.DataCenterResource,
	nodes []string,
) []chantico.DataCenterResource

func SetValidationError

func SetValidationError(
	dataCenterResource *chantico.DataCenterResource,
	err error,
	involvedResource string,
)

func UpdateState

func UpdateState(
	dataCenterResource *chantico.DataCenterResource,
)

func Validate

func Validate(
	dataCenterResource *chantico.DataCenterResource,
	dataCenterResources []chantico.DataCenterResource,
	physicalMeasurements []chantico.PhysicalMeasurement,
) ([]chantico.DataCenterResource, error, string)

func WriteRuleFile

func WriteRuleFile(
	ctx context.Context,
	dataCenterResource *chantico.DataCenterResource,
) *sm.ActionResult

WriteRuleFile writes a Prometheus recording rule file for this DataCenterResource. The file is written to prometheus/rules/<name>.yml on the shared volume. After writing, Prometheus is sent a reload request so it picks up the new rules.

Types

type ErrorCycleDetected

type ErrorCycleDetected struct {
	InvolvedResource string
}

func (ErrorCycleDetected) Error

func (e ErrorCycleDetected) Error() string

type ErrorMissingEnergyMetric

type ErrorMissingEnergyMetric struct {
	InvolvedResource string
}

func (ErrorMissingEnergyMetric) Error

func (e ErrorMissingEnergyMetric) Error() string

type ErrorResourceNotFound

type ErrorResourceNotFound struct {
	InvolvedResource string
}

func (ErrorResourceNotFound) Error

func (e ErrorResourceNotFound) Error() string

type ErrorUnknownType

type ErrorUnknownType struct {
	Type string
}

func (ErrorUnknownType) Error

func (e ErrorUnknownType) Error() string

type RecordingRule

type RecordingRule struct {
	Record string `yaml:"record"`
	Expr   string `yaml:"expr"`
}

RecordingRule represents a single Prometheus recording rule.

func BuildRecordingRules

func BuildRecordingRules(
	dataCenterResource *chantico.DataCenterResource,
) []RecordingRule

BuildRecordingRules generates the set of Prometheus recording rules for a DataCenterResource node, following the energy accounting design:

  1. For root nodes (spec.energyMetric is set), an alias rule mapping the raw energy metric to the canonical datacenter:<name>:energy_watts name.
  2. One coefficient recording rule per parent that has a coefficient set (from the ParentRef entries in spec.parents).
  3. One energy recording rule for non-root nodes (sum of coefficient * parent energy for each parent).

Returns nil if no rules need to be written.

type RuleFile

type RuleFile struct {
	Groups []RuleGroup `yaml:"groups"`
}

RuleFile represents a complete Prometheus rule file.

func BuildRuleFile

func BuildRuleFile(
	dataCenterResource *chantico.DataCenterResource,
) *RuleFile

BuildRuleFile wraps the recording rules into a complete Prometheus rule file structure with a single group named after the resource.

type RuleGroup

type RuleGroup struct {
	Name  string          `yaml:"name"`
	Rules []RecordingRule `yaml:"rules"`
}

RuleGroup represents a Prometheus rule group.