base

package
v0.21.3 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

This codebase originates from https://github.com/github/freno, See https://github.com/github/freno/blob/master/LICENSE

MIT License

Copyright (c) 2017 GitHub

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Index

Constants

This section is empty.

Variables

View Source
var AppDeniedMetric = &appDeniedMetric{}

AppDeniedMetric is a special metric indicating a "denied" situation

View Source
var ErrAppDenied = errors.New("app denied")

ErrAppDenied is seen when an app is denied access

View Source
var ErrInvalidCheckType = errors.New("unknown throttler check type")

ErrInvalidCheckType is an internal error indicating an unknown check type

View Source
var ErrNoResultYet = errors.New("metric not collected yet")
View Source
var ErrNoSuchMetric = errors.New("no such metric")

ErrNoSuchMetric is for when a user requests a metric by an unknown metric name

View Source
var ErrThresholdExceeded = errors.New("threshold exceeded")

ErrThresholdExceeded is the common error one may get checking on metric result

View Source
var (
	KnownMetricNames = make(MetricNames, 0)
)
View Source
var NoHostsMetricResult = &noHostsMetricResult{}

NoHostsMetricResult is a result indicating "no hosts"

View Source
var NoMetricResultYet = &noMetricResultYet{}

NoMetricResultYet is a result indicating "no data"

View Source
var NoSuchMetric = &noSuchMetric{}

NoSuchMetric is a metric results for an unknown metric name

View Source
var (
	RegisteredSelfMetrics = make(map[MetricName]SelfMetric)
)

Functions

func DisaggregateMetricName added in v0.21.0

func DisaggregateMetricName(aggregatedMetricName string) (scope Scope, metricName MetricName, err error)

DisaggregateMetricName splits a metric name into its scope name and metric name aggregated metric name could be in the form: - loadavg - self - self/threads_running - shard - shard/lag

func IsDialTCPError

func IsDialTCPError(err error) bool

IsDialTCPError sees if the given error indicates a TCP issue

func SetupHTTPClient

func SetupHTTPClient(httpTimeout time.Duration) *http.Client

SetupHTTPClient creates a simple HTTP client with timeout

Types

type AggregatedMetricName added in v0.21.0

type AggregatedMetricName struct {
	Scope  Scope
	Metric MetricName
}

type AppThrottle

type AppThrottle struct {
	AppName  string
	ExpireAt time.Time
	Ratio    float64
	Exempt   bool
}

AppThrottle is the definition for an app throttling instruction - Ratio: [0..1], 0 == no throttle, 1 == fully throttle

func NewAppThrottle

func NewAppThrottle(appName string, expireAt time.Time, ratio float64, exempt bool) *AppThrottle

NewAppThrottle creates an AppThrottle struct

type ClusterProbes added in v0.21.0

type ClusterProbes struct {
	IgnoreHostsCount     int
	IgnoreHostsThreshold float64
	TabletProbes         Probes
}

ClusterProbes has the probes for a specific cluster

type CustomQuerySelfMetric added in v0.21.0

type CustomQuerySelfMetric struct {
}

func (*CustomQuerySelfMetric) DefaultScope added in v0.21.0

func (m *CustomQuerySelfMetric) DefaultScope() Scope

func (*CustomQuerySelfMetric) DefaultThreshold added in v0.21.0

func (m *CustomQuerySelfMetric) DefaultThreshold() float64

func (*CustomQuerySelfMetric) Name added in v0.21.0

func (*CustomQuerySelfMetric) Read added in v0.21.0

func (*CustomQuerySelfMetric) RequiresConn added in v0.21.0

func (m *CustomQuerySelfMetric) RequiresConn() bool

type DefaultSelfMetric added in v0.21.0

type DefaultSelfMetric struct {
}

func (*DefaultSelfMetric) DefaultScope added in v0.21.0

func (m *DefaultSelfMetric) DefaultScope() Scope

func (*DefaultSelfMetric) DefaultThreshold added in v0.21.0

func (m *DefaultSelfMetric) DefaultThreshold() float64

func (*DefaultSelfMetric) Name added in v0.21.0

func (m *DefaultSelfMetric) Name() MetricName

func (*DefaultSelfMetric) Read added in v0.21.0

func (*DefaultSelfMetric) RequiresConn added in v0.21.0

func (m *DefaultSelfMetric) RequiresConn() bool

type Inventory added in v0.21.0

type Inventory struct {
	ClustersProbes       Probes
	IgnoreHostsCount     int
	IgnoreHostsThreshold float64
	TabletMetrics        TabletResultMap
}

Inventory has the operational data about probes, their metrics, and relevant configuration

func NewInventory added in v0.21.0

func NewInventory() *Inventory

NewInventory creates a Inventory

type LagSelfMetric added in v0.21.0

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

func (*LagSelfMetric) DefaultScope added in v0.21.0

func (m *LagSelfMetric) DefaultScope() Scope

func (*LagSelfMetric) DefaultThreshold added in v0.21.0

func (m *LagSelfMetric) DefaultThreshold() float64

func (*LagSelfMetric) GetQuery added in v0.21.0

func (m *LagSelfMetric) GetQuery() string

func (*LagSelfMetric) Name added in v0.21.0

func (m *LagSelfMetric) Name() MetricName

func (*LagSelfMetric) Read added in v0.21.0

func (*LagSelfMetric) RequiresConn added in v0.21.0

func (m *LagSelfMetric) RequiresConn() bool

func (*LagSelfMetric) SetQuery added in v0.21.0

func (m *LagSelfMetric) SetQuery(query string)

SetQuery is only used by unit tests to override the query.

type LoadAvgSelfMetric added in v0.21.0

type LoadAvgSelfMetric struct {
}

func (*LoadAvgSelfMetric) DefaultScope added in v0.21.0

func (m *LoadAvgSelfMetric) DefaultScope() Scope

func (*LoadAvgSelfMetric) DefaultThreshold added in v0.21.0

func (m *LoadAvgSelfMetric) DefaultThreshold() float64

func (*LoadAvgSelfMetric) Name added in v0.21.0

func (m *LoadAvgSelfMetric) Name() MetricName

func (*LoadAvgSelfMetric) Read added in v0.21.0

func (*LoadAvgSelfMetric) RequiresConn added in v0.21.0

func (m *LoadAvgSelfMetric) RequiresConn() bool

type MetricHealth

type MetricHealth struct {
	LastHealthyAt           time.Time
	SecondsSinceLastHealthy int64
}

MetricHealth is a health status for a metric, and more specifically, when it was last checked to be "OK"

func NewMetricHealth

func NewMetricHealth(lastHealthyAt time.Time) *MetricHealth

NewMetricHealth returns a MetricHealth

type MetricHealthMap

type MetricHealthMap map[string](*MetricHealth)

MetricHealthMap maps metric names to metric healths

func (MetricHealthMap) Aggregate

func (m MetricHealthMap) Aggregate(other MetricHealthMap) MetricHealthMap

Aggregate another map into this map, take the worst metric of the two

type MetricName added in v0.21.0

type MetricName string

MetricName is a formalized name for a metric, such as "lag" or "threads_running". A metric name may include a scope, such as "self/lag" or "shard/threads_running". It is possible to add a scope to a name, or to parse the scope out of a name, and there is also always a default scope associated with a metric name.

const (
	DefaultMetricName        MetricName = "default"
	LagMetricName            MetricName = "lag"
	ThreadsRunningMetricName MetricName = "threads_running"
	CustomMetricName         MetricName = "custom"
	LoadAvgMetricName        MetricName = "loadavg"
)

func (MetricName) AggregatedName added in v0.21.0

func (metric MetricName) AggregatedName(scope Scope) string

AggregatedName returns the string representation of this metric in the given scope, e.g.: - "self/loadavg" - "shard/lag"

func (MetricName) DefaultScope added in v0.21.0

func (metric MetricName) DefaultScope() Scope

func (MetricName) Disaggregated added in v0.21.0

func (metric MetricName) Disaggregated() (scope Scope, metricName MetricName, err error)

Disaggregated returns a breakdown of this metric into scope + name.

func (MetricName) String added in v0.21.0

func (metric MetricName) String() string

type MetricNames added in v0.21.0

type MetricNames []MetricName

MetricNames is a formalized list of metric names

func (MetricNames) Contains added in v0.21.0

func (names MetricNames) Contains(name MetricName) bool

func (MetricNames) String added in v0.21.0

func (names MetricNames) String() string

func (MetricNames) Unique added in v0.21.0

func (names MetricNames) Unique() MetricNames

Unique returns a subset of unique metric names, in same order as the original names

type MetricResult

type MetricResult interface {
	Get() (float64, error)
}

MetricResult is what we expect our probes to return. This can be a numeric result, or a special type of result indicating more meta-information

func AggregateTabletMetricResults added in v0.21.0

func AggregateTabletMetricResults(
	metricName MetricName,
	tabletResultsMap TabletResultMap,
	ignoreHostsCount int,
	IgnoreDialTCPErrors bool,
	ignoreHostsThreshold float64,
) (worstMetric MetricResult)

func NewSimpleMetricResult

func NewSimpleMetricResult(value float64) MetricResult

NewSimpleMetricResult creates a simpleMetricResult

type MetricResultFunc

type MetricResultFunc func() (metricResult MetricResult, threshold float64)

MetricResultFunc is a function that returns a metric result

type MetricResultMap added in v0.21.0

type MetricResultMap map[MetricName]MetricResult

func NewMetricResultMap added in v0.21.0

func NewMetricResultMap() MetricResultMap

type MetricsQueryType added in v0.21.0

type MetricsQueryType int

MetricsQueryType indicates the type of metrics query on MySQL backend. See following.

const (
	// MetricsQueryTypeDefault indicates the default, internal implementation. Specifically, our throttler runs a replication lag query
	MetricsQueryTypeDefault MetricsQueryType = iota
	// MetricsQueryTypeShowGlobal indicates SHOW GLOBAL (STATUS|VARIABLES) query
	MetricsQueryTypeShowGlobal
	// MetricsQueryTypeSelect indicates a custom SELECT query
	MetricsQueryTypeSelect
	// MetricsQueryTypeUnknown is an unknown query type, which we cannot run. This is an error
	MetricsQueryTypeUnknown
)

func GetMetricsQueryType added in v0.21.0

func GetMetricsQueryType(query string) MetricsQueryType

GetMetricsQueryType analyzes the type of a metrics query

type Probe added in v0.21.0

type Probe struct {
	Alias           string
	Tablet          *topodatapb.Tablet
	CacheMillis     int
	QueryInProgress int64
}

Probe is the minimal configuration required to connect to a MySQL server

func NewProbe added in v0.21.0

func NewProbe() *Probe

NewProbe creates Probe

func (*Probe) String added in v0.21.0

func (p *Probe) String() string

String returns a human readable string of this struct

type Probes added in v0.21.0

type Probes map[string](*Probe)

Probes maps tablet aliases to probe(s)

func NewProbes added in v0.21.0

func NewProbes() Probes

NewProbes creates Probes

type RecentApp

type RecentApp struct {
	AppName      string
	CheckedAt    time.Time
	StatusCode   int
	ResponseCode tabletmanagerdatapb.CheckThrottlerResponseCode
}

RecentApp indicates when an app was last checked

func NewRecentApp

func NewRecentApp(appName string, statusCode int, responseCode tabletmanagerdatapb.CheckThrottlerResponseCode) *RecentApp

NewRecentApp creates a RecentApp

type Scope added in v0.21.0

type Scope string

Scope defines the tablet range from which a metric is collected. This can be the local tablet ("self") or the entire shard ("shard")

const (
	UndefinedScope Scope = ""
	ShardScope     Scope = "shard"
	SelfScope      Scope = "self"
)

func ScopeFromString added in v0.21.0

func ScopeFromString(s string) (Scope, error)

func (Scope) String added in v0.21.0

func (s Scope) String() string

type SelfMetric added in v0.21.0

type SelfMetric interface {
	Name() MetricName
	DefaultScope() Scope
	DefaultThreshold() float64
	RequiresConn() bool
	Read(ctx context.Context, throttler ThrottlerMetricsPublisher, conn *connpool.Conn) *ThrottleMetric
}

type TabletResultMap added in v0.21.0

type TabletResultMap map[string]MetricResultMap

TabletResultMap maps a tablet to a result

func (TabletResultMap) Split added in v0.21.0

func (m TabletResultMap) Split(alias string) (withAlias TabletResultMap, all TabletResultMap)

type ThreadsRunningSelfMetric added in v0.21.0

type ThreadsRunningSelfMetric struct {
}

func (*ThreadsRunningSelfMetric) DefaultScope added in v0.21.0

func (m *ThreadsRunningSelfMetric) DefaultScope() Scope

func (*ThreadsRunningSelfMetric) DefaultThreshold added in v0.21.0

func (m *ThreadsRunningSelfMetric) DefaultThreshold() float64

func (*ThreadsRunningSelfMetric) Name added in v0.21.0

func (*ThreadsRunningSelfMetric) Read added in v0.21.0

func (*ThreadsRunningSelfMetric) RequiresConn added in v0.21.0

func (m *ThreadsRunningSelfMetric) RequiresConn() bool

type ThrottleMetric added in v0.21.0

type ThrottleMetric struct {
	Name  MetricName
	Scope Scope
	Alias string
	Value float64
	Err   error
}

ThrottleMetric has the probed metric for a tablet

func NewThrottleMetric added in v0.21.0

func NewThrottleMetric() *ThrottleMetric

NewThrottleMetric creates a new ThrottleMetric

func ReadSelfMySQLThrottleMetric added in v0.21.0

func ReadSelfMySQLThrottleMetric(ctx context.Context, conn *connpool.Conn, query string) *ThrottleMetric

ReadSelfMySQLThrottleMetric reads a metric using a given MySQL connection and a query.

func (*ThrottleMetric) Get added in v0.21.0

func (metric *ThrottleMetric) Get() (float64, error)

Get implements MetricResult

func (*ThrottleMetric) GetTabletAlias added in v0.21.0

func (metric *ThrottleMetric) GetTabletAlias() string

GetClusterTablet returns the ClusterTablet part of the metric

func (*ThrottleMetric) WithError added in v0.21.0

func (metric *ThrottleMetric) WithError(err error) *ThrottleMetric

WithError returns this metric with given error

type ThrottleMetrics added in v0.21.0

type ThrottleMetrics map[MetricName]*ThrottleMetric // nolint:revive

func ReadThrottleMetrics added in v0.21.0

func ReadThrottleMetrics(ctx context.Context, probe *Probe, metricsFunc func(context.Context) ThrottleMetrics) ThrottleMetrics

ReadThrottleMetrics returns a metric for the given probe. Either by explicit query or via SHOW REPLICA STATUS

type ThrottlerMetricsPublisher added in v0.21.0

type ThrottlerMetricsPublisher interface {
	GetCustomMetricsQuery() string
}

ThrottlerMetricsPublisher is implemented by throttler.Throttler and is used by SelfMetric implementations to query the throttler.

Jump to

Keyboard shortcuts

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