common

package
v0.0.0-...-7d8f466 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ClusterEntityKind   = "cluster"
	NodeEntityKind      = Node
	ContainerEntityKind = Container
	CrqEntityKind       = "crq"
	RqEntityKind        = "rq"
	Quota               = "quota"
	Hpa                 = "hpa"
)

EntityKinds

View Source
const (
	Empty       = cconf.Empty
	Cpu         = "cpu"
	Memory      = "memory"
	Mem         = "mem"
	Disk        = "disk"
	Net         = "net"
	Limit       = "limit"
	Request     = "request"
	Capacity    = "capacity"
	Allocatable = "allocatable"
	Resource    = "resource"
	Namespace   = "namespace"
	Type        = "type"
	Hard        = "hard"
	Used        = "used"
	Count       = "count"
	Node        = "node"
	Instance    = "instance"
	Speed       = "speed"
	Utilization = "utilization"
	Reservation = "reservation"
	Percent     = "percent"
	Workload    = "workload"
	Actual      = "actual"
	Raw         = "raw"
	Read        = "read"
	Write       = "write"
	Received    = "received"
	Sent        = "sent"
	Total       = "total"
	Bytes       = "bytes"
	Ops         = "ops"
	Packets     = "packets"
	Current     = "current"
	Size        = "size"
	Group       = "group"
	Name        = "name"
	Entity      = "entity"
	Metric      = "metric"
	Time        = "time"
	Pod         = "pod"
	Container   = "container"
	Replica     = "replica"
	Daemon      = "daemon"
	Stateful    = "stateful"
	Set         = "set"
	Job         = "job"
	Cron        = "cron"
	Deployment  = "deployment"
	Replication = "replication"
	Controller  = "controller"
	Owner       = "owner"
	Kind        = "kind"
	Running     = "Running"
	Terminated  = "Terminated"
	Day         = "day"
	Hour        = "hour"
	Label       = "label"
	Max         = "max"
	Avg         = "avg"
	Min         = "min"
	MCoresSt    = "mcores"
	Extra       = "extra"
	Bearer      = "Bearer"
	InfoSt      = "info"
	ConfigSt    = "config"
	Map         = "map"
	Catalog     = "catalog"
	Source      = "source"
)
View Source
const (
	Or = "|"

	Comma      = cconf.Comma
	Dot        = cconf.Dot
	Slash      = cconf.Slash
	Space      = " "
	Underscore = "_"
)
View Source
const (
	LabelNamesPlaceholder = `LNPH`

	EntityFormat      = "entity=%s"
	ClusterFileFormat = "cluster=%s file=%s"
)
View Source
const (
	ClusterFormat    = "cluster=%s"
	DefaultLogFormat = ClusterFormat + Space + "entity=%s"
)
View Source
const (
	QueryFormat        = "metric=%s query=%s"
	ClusterQueryFormat = ClusterFormat + Space + QueryFormat
)
View Source
const (
	UnknownValue = -1
)

Variables

View Source
var (
	NodeGroupEntityKind = SnakeCase(Node, Group)
	HpaEntityKind       = SnakeCase(ContainerEntityKind, Hpa)
)
View Source
var (
	PodOwner                   = CamelCase(Pod)
	DeploymentOwner            = CamelCase(Deployment)
	JobOwner                   = CamelCase(Job)
	NodeOwner                  = CamelCase(Node)
	ReplicaSetOwner            = CamelCase(Replica, Set)
	DaemonSetOwner             = CamelCase(Daemon, Set)
	StatefulSetOwner           = CamelCase(Stateful, Set)
	ReplicationControllerOwner = CamelCase(Replication, Controller)
	CronJobOwner               = CamelCase(Cron, Job)
	ConfigMapOwner             = CamelCase(ConfigSt, Map)   // openshift
	CatalogSourceOwner         = CamelCase(Catalog, Source) // openshift
)

owner kind labels

View Source
var (
	Limits                = Plural(Limit)
	Requests              = Plural(Request)
	Pods                  = Plural(Pod)
	CpuLimit              = DromedaryCase(Cpu, Limit)
	LimitsCpu             = JoinDot(Limits, Cpu)
	MemLimit              = DromedaryCase(Memory, Limit)
	LimitsMem             = JoinDot(Limits, Memory)
	CpuRequest            = DromedaryCase(Cpu, Request)
	RequestsCpu           = JoinDot(Requests, Cpu)
	MemRequest            = DromedaryCase(Memory, Request)
	RequestsMem           = JoinDot(Requests, Memory)
	CpuCapacity           = DromedaryCase(Cpu, Capacity)
	MemCapacity           = DromedaryCase(Memory, Capacity)
	PodsCapacity          = DromedaryCase(Pods, Capacity)
	CpuAllocatable        = DromedaryCase(Cpu, Allocatable)
	MemAllocatable        = DromedaryCase(Memory, Allocatable)
	PodsAllocatable       = DromedaryCase(Pods, Allocatable)
	CountPods             = Join(Slash, Count, Pods)
	NetSpeedBytes         = DromedaryCase(Net, Speed, Bytes)
	ReplicaSet            = strings.ToLower(ReplicaSetOwner)
	DaemonSet             = strings.ToLower(DaemonSetOwner)
	StatefulSet           = strings.ToLower(StatefulSetOwner)
	ReplicationController = strings.ToLower(ReplicationControllerOwner)
	CronJob               = strings.ToLower(CronJobOwner)
	Replicas              = Plural(Replica)
	Days                  = Plural(Day)
	Hours                 = Plural(Hour)
	Labels                = Plural(Label)
	CurrentSizeName       = DromedaryCase(CurrentSize.GetMetricName())
	NodeGroupInclude      = JoinNoSep(Node, Group)
)

these are practically constants but as they use functions they need to be vars

common WorkloadMetricHolder structs

View Source
var BoolValues = []bool{true, false}
View Source
var CurrentTime time.Time
View Source
var Version string

globals

Functions

func AddToLabelMap

func AddToLabelMap(key string, value string, labelPath map[string]string)

AddToLabelMap is used to add values to label map used for attributes

func CamelCase

func CamelCase(elements ...string) string

func CheckPrometheusUp

func CheckPrometheusUp() (n int)

func CollectAndProcessMetric

func CollectAndProcessMetric(query string, promRange v1.Range, matrixFunc ClusterMatrixFunc) (n int, err error)

func CollectAndProcessMetricErrorLogLevel

func CollectAndProcessMetricErrorLogLevel(callDepth int, query string, promRange v1.Range, matrixFunc ClusterMatrixFunc, level LogLevel) (n int, err error)

func ConditionalPrintCSVLabelMap

func ConditionalPrintCSVLabelMap(file *os.File, labelMap map[string]string, last bool, rejectKeys map[string]bool) (err error)

func Contains

func Contains[K, V comparable](m map[K]V, k K, v V) bool

func DebugLogMemStats

func DebugLogMemStats(callDepth int, msg string)

func DebugLogObjectMemStats

func DebugLogObjectMemStats(obj string)

func DromedaryCase

func DromedaryCase(elements ...string) string

func Eval

func Eval(n int, eval bool) int

func FatalError

func FatalError(err error, format string, v ...any)

func Format

func Format(t *time.Time) string

func FormatCurrentTime

func FormatCurrentTime() string

func FormatTime

func FormatTime(mt model.Time) string

func FormatTimeInSec

func FormatTimeInSec(i int64) string

func Found

func Found(indicators map[string]int, indicator string, eval bool) bool

func FoundCounter

func FoundCounter(n int) (res []bool)

func FoundIndicatorCounter

func FoundIndicatorCounter(indicators map[string]int, indicator string) []bool

func GetConditionalMetricsWorkload

func GetConditionalMetricsWorkload(indicators map[string]int, indicator string, querySubToMetricFields map[string][]model.LabelName, entityKind string)

func GetCsvHeaderFormat

func GetCsvHeaderFormat(entityKind string) (format string, f bool)

func GetExtraFileNameByType

func GetExtraFileNameByType(cluster, entityKind string, ft FileType) string

func GetFileName

func GetFileName(cluster, entityKind, fileName string) string

func GetFileNameByType

func GetFileNameByType(cluster, entityKind string, ft FileType) string

func GetLabelValue

func GetLabelValue(ss *model.SampleStream, name string) (s string, b bool)

func GetLabelsValues

func GetLabelsValues(ss *model.SampleStream, names []string) (m map[string]string, b bool)

func GetPrometheusVersion

func GetPrometheusVersion() (version string, found bool)

func GetValue

func GetValue(ss *model.SampleStream, name string) (s string)

func GetWorkload

func GetWorkload(callDepth int, fileName, metricName, query string, metricFields []model.LabelName, entityKind string)

GetWorkload used to query for the workload data and then calls write workload

func GetWorkloadQueryVariants

func GetWorkloadQueryVariants(callDepth int, fileName, metricName string, queryProcessors map[string]*QueryProcessor, entityKind string)

func GetWorkloadQueryVariantsFieldConversion

func GetWorkloadQueryVariantsFieldConversion(callDepth int, fileName, metricName string, queryProcessors map[string]*QueryProcessor, entityKind string)

func InitLogs

func InitLogs()

func InitWorkloadFile

func InitWorkloadFile(cluster, fileName, entityKind, csvHeaderFormat, metricName string) *os.File

func IntMCores

func IntMCores[N Number](n N) int

func IntMiB

func IntMiB[N Number](n N) int

func IsSubset

func IsSubset[K, V comparable](m, sub map[K]V) bool

func Join

func Join(sep string, elements ...string) string

func JoinComma

func JoinComma(elements ...string) string

func JoinDot

func JoinDot(elements ...string) string

func JoinNoSep

func JoinNoSep(elements ...string) string

func JoinSpace

func JoinSpace(elements ...string) string

func KeySet

func KeySet[K comparable, V any](m map[K]V) (keys []K)

KeySet returns the key set of m, nil if m is empty or nil

func KnownValueFunc

func KnownValueFunc(n int) bool

func LastSampleValue

func LastSampleValue(ss *model.SampleStream) (value model.SampleValue)

func LastValue

func LastValue(ss *model.SampleStream) float64

func LogAll

func LogAll(callDepth int, level LogLevel, format string, v ...any)

func LogCluster

func LogCluster(callDepth int, level LogLevel, format string, cluster string, toStdOut bool, v ...any)

func LogError

func LogError(err error, format string, v ...any)

func LogErrorWithLevel

func LogErrorWithLevel(callDepth int, level LogLevel, err error, format string, v ...any)

func MCores

func MCores[N Number](n N) N

func Merge

func Merge[K comparable, V any](m1, m2 map[K]V, mds MergeDuplicateStrategy) (m map[K]V, err error)

func MiB

func MiB[N Number](n N) N

func MkdirAll

func MkdirAll() error

func NumClusters

func NumClusters() int

func Plural

func Plural(s string) string

func PositiveValueFunc

func PositiveValueFunc(n int) bool

func PrintCSVIntValue

func PrintCSVIntValue(file *os.File, value int, last bool) error

func PrintCSVIntValueConditional

func PrintCSVIntValueConditional(file *os.File, value int, last bool, f IntValueFunc) error

func PrintCSVJoinedStringValue

func PrintCSVJoinedStringValue(file *os.File, value string, sep string, last bool) error

func PrintCSVLabelMap

func PrintCSVLabelMap(file *os.File, labelMap map[string]string, last bool) error

func PrintCSVPositiveIntValue

func PrintCSVPositiveIntValue(file *os.File, value int, last bool) error

func PrintCSVStringValue

func PrintCSVStringValue(file *os.File, value string, last bool) (err error)

func PrintCSVTimeValue

func PrintCSVTimeValue(file *os.File, value *time.Time, last bool) error

func ProcessResults

func ProcessResults(callDepth int, resultMap ClusterResultMap, err error, query string, matrixFunc ClusterMatrixFunc)

func ProcessResultsErrorLogLevel

func ProcessResultsErrorLogLevel(callDepth int, resultMap ClusterResultMap, err error, query string, matrixFunc ClusterMatrixFunc, level LogLevel)

func RegisterClusterFilters

func RegisterClusterFilters(cfps []*cconf.ClusterFilterParameters) error

func ReplaceColons

func ReplaceColons(s string) string

func ReplaceSemiColons

func ReplaceSemiColons(s string) string

func ReplaceSemiColonsPipes

func ReplaceSemiColonsPipes(s string) string

func SetCurrentTime

func SetCurrentTime()

func SnakeCase

func SnakeCase(elements ...string) string

func SortSlice

func SortSlice[T constraints.Ordered](s []T)

func SortedKeySet

func SortedKeySet[K constraints.Ordered, V any](m map[K]V) (keys []K)

func TimeRange

func TimeRange() (promRange v1.Range)

TimeRange allows you to define the start and end values of the range will pass to the Prometheus for the query

func TimeRangeForInterval

func TimeRangeForInterval(historyInterval time.Duration) (promRange v1.Range)

func ToPrometheusLabelName

func ToPrometheusLabelName(s string) string

ToPrometheusLabelName is actually a copy of https://github.com/kubernetes/kube-state-metrics/blob/main/internal/store/utils.go#L125 and added here to prevent dependency on that package for one function only; cannot use SnakeCase() as proper snake case treats a number as a new word (e.g. "group1" -> "group_1"), whereas ksm's toSnakeCase() does not (e.g. "group1" -> "group1"),

func ToPrometheusLabelNameList

func ToPrometheusLabelNameList(list string) string

func WriteValues

func WriteValues(file io.Writer, clusterName, fields string, values []model.SamplePair) error

Types

type ClusterFilter

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

func NewClusterFilter

func NewClusterFilter(cfp *cconf.ClusterFilterParameters) *ClusterFilter

type ClusterLeveledLogger

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

ClusterLeveledLogger implements rhttp.LeveledLogger to avoid clutter

func (*ClusterLeveledLogger) Debug

func (cll *ClusterLeveledLogger) Debug(msg string, keysAndValues ...interface{})

func (*ClusterLeveledLogger) Error

func (cll *ClusterLeveledLogger) Error(msg string, keysAndValues ...interface{})

func (*ClusterLeveledLogger) Info

func (cll *ClusterLeveledLogger) Info(msg string, keysAndValues ...interface{})

func (*ClusterLeveledLogger) Warn

func (cll *ClusterLeveledLogger) Warn(msg string, keysAndValues ...interface{})

type ClusterMatrixFunc

type ClusterMatrixFunc func(string, model.Matrix)

type ClusterResultMap

type ClusterResultMap map[string]*Result

func CollectMetric

func CollectMetric(callDepth int, query string, promRange v1.Range) (crm ClusterResultMap, n int, err error)

CollectMetric is used to query Prometheus to get data for specific query and return the results to be processed

type FieldProvider

type FieldProvider struct {
	Cluster      string
	MetricFields []model.LabelName
	ConvF        FieldsFunc
}

func (*FieldProvider) Fields

func (fp *FieldProvider) Fields(metric model.Metric) (string, bool)

type FieldsFunc

type FieldsFunc func(string, []string) ([]string, bool)

type FileType

type FileType int
const (
	Config FileType = iota
	Attributes
)

func (FileType) String

func (ft FileType) String() (s string)

type IntValueFunc

type IntValueFunc func(int) bool

type LogLevel

type LogLevel int
const (
	Debug LogLevel = iota
	Info
	Warn
	Error
	Unknown
)

func (LogLevel) String

func (ll LogLevel) String() string

type MergeDuplicateStrategy

type MergeDuplicateStrategy int
const (
	Fail MergeDuplicateStrategy = iota
	Ignore
	Override
)

type Number

type Number interface {
	~int | ~uint | ~int32 | ~uint32 | ~int64 | ~uint64 | ~float32 | ~float64
}

Number - Ideally we would define Number as

type Number interface {
    constraints.Integer | constraints.Float
}

However, this doesn't work, as in some cases we have arithmetic expressions involving constant values which exceed int8, int16, unit8, uint16 - therefore we need to exclude these types (to avoid compilation errors). In addition, constraints.Integer (specifically constraints.Unsigned) includes ~uintptr which we want to exclude as well.

type ObservabilityPlatform

type ObservabilityPlatform string
const (
	UnknownPlatform               ObservabilityPlatform = Empty
	AWSManagedPrometheus          ObservabilityPlatform = "AWS Managed Prometheus"
	AzureMonitorManagedPrometheus ObservabilityPlatform = "Azure Monitor Managed Prometheus"
	GrafanaCloud                  ObservabilityPlatform = "Grafana Cloud"
)

func GetObservabilityPlatform

func GetObservabilityPlatform() ObservabilityPlatform

type QueryProcessor

type QueryProcessor struct {
	MetricFields []model.LabelName
	FF           FieldsFunc
}

type Result

type Result struct {
	Query  string
	Matrix model.Matrix
	Error  error
}

type WorkloadMetricHolder

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

func NewWorkloadMetricHolder

func NewWorkloadMetricHolder(nameElements ...string) *WorkloadMetricHolder

func (*WorkloadMetricHolder) GetFileName

func (wmh *WorkloadMetricHolder) GetFileName() string

func (*WorkloadMetricHolder) GetMetricName

func (wmh *WorkloadMetricHolder) GetMetricName() string

func (*WorkloadMetricHolder) GetWorkload

func (wmh *WorkloadMetricHolder) GetWorkload(query string, metricField []model.LabelName, entityKind string)

func (*WorkloadMetricHolder) GetWorkloadQueryVariants

func (wmh *WorkloadMetricHolder) GetWorkloadQueryVariants(callDepth int, qps map[string]*QueryProcessor, entityKind string)

func (*WorkloadMetricHolder) OverrideFileName

func (wmh *WorkloadMetricHolder) OverrideFileName(nameElements ...string) *WorkloadMetricHolder

type WorkloadQueryWrapper

type WorkloadQueryWrapper struct {
	Prefix, Suffix string
}

func (*WorkloadQueryWrapper) Wrap

func (wqw *WorkloadQueryWrapper) Wrap(query string) string

Jump to

Keyboard shortcuts

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