Documentation ¶
Overview ¶
Package prom provides helpers to manage shared connections in Go.
Index ¶
- Constants
- func NewId() string
- type BaseConnection
- func (c *BaseConnection) LogMetrics(category string, cmd *CmdExecInfo) error
- func (c *BaseConnection) Metrics(category string, opts ...MetricsOpts) (*Metrics, error)
- func (c *BaseConnection) MetricsLogger() IMetricsLogger
- func (c *BaseConnection) NewCmdExecInfo() *CmdExecInfo
- func (c *BaseConnection) PoolOpts() IBasePoolOpts
- func (c *BaseConnection) RegisterMetricsLogger(logger IMetricsLogger) IBaseConnection
- func (c *BaseConnection) SetPoolOpts(opts IBasePoolOpts) IBaseConnection
- type BasePoolOpts
- type CmdExecInfo
- type IBaseConnection
- type IBasePoolOpts
- type IMetricsLogger
- type MemoryStoreMetricsLogger
- type Metrics
- type MetricsOpts
Constants ¶
const ( CmdResultOk = "OK" CmdResultError = "ERROR" )
const ( // MetricsCatAll is a common metrics category for "all commands". MetricsCatAll = "all" // MetricsCatDDL is a common metrics category for "DDL commands". MetricsCatDDL = "ddl" // MetricsCatDML is a common metrics category for "DML commands". MetricsCatDML = "dml" // MetricsCatDQL is a common metrics category for "DQL commands". MetricsCatDQL = "dql" // MetricsCatOther is a common metrics category for "other commands". MetricsCatOther = "other" )
const (
// Version holds the semantic version number of this module.
Version = "1.0.0"
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BaseConnection ¶ added in v1.0.0
type BaseConnection struct {
// contains filtered or unexported fields
}
BaseConnection is an abstract implementation of IBaseConnection.
@Available since v1.0.0
func (*BaseConnection) LogMetrics ¶ added in v1.0.0
func (c *BaseConnection) LogMetrics(category string, cmd *CmdExecInfo) error
LogMetrics implements IBaseConnection.LogMetrics.
func (*BaseConnection) Metrics ¶ added in v1.0.0
func (c *BaseConnection) Metrics(category string, opts ...MetricsOpts) (*Metrics, error)
Metrics implements IBaseConnection.Metrics.
func (*BaseConnection) MetricsLogger ¶ added in v1.0.0
func (c *BaseConnection) MetricsLogger() IMetricsLogger
MetricsLogger implements IBaseConnection.MetricsLogger.
func (*BaseConnection) NewCmdExecInfo ¶ added in v1.0.0
func (c *BaseConnection) NewCmdExecInfo() *CmdExecInfo
NewCmdExecInfo implements IBaseConnection.NewCmdExecInfo.
func (*BaseConnection) PoolOpts ¶ added in v1.0.0
func (c *BaseConnection) PoolOpts() IBasePoolOpts
PoolOpts implements IBaseConnection.PoolOpts.
func (*BaseConnection) RegisterMetricsLogger ¶ added in v1.0.0
func (c *BaseConnection) RegisterMetricsLogger(logger IMetricsLogger) IBaseConnection
RegisterMetricsLogger implements IBaseConnection.RegisterMetricsLogger.
func (*BaseConnection) SetPoolOpts ¶ added in v1.0.0
func (c *BaseConnection) SetPoolOpts(opts IBasePoolOpts) IBaseConnection
SetPoolOpts attaches a pooling options object to the connection.
type BasePoolOpts ¶ added in v1.0.0
type BasePoolOpts struct { // Maximum number of connections. // Set to zero or negative value to use default value. MaxPoolSize int `json:"max_size"` // Minimum number of idle connections. Default value is 1. MinPoolSize int `json:"min_size"` // Maximum amount of time a connection may be reused. // Set to zero or negative value to use default value. ConnLifetime time.Duration `json:"conn_lifetime"` }
BasePoolOpts is an abstract implementation of IBasePoolOpts.
@Available since v1.0.0
type CmdExecInfo ¶ added in v0.3.0
type CmdExecInfo struct { // Id is the command's unique id. Id string `json:"id"` // BeginTime is the timestamp when the command started execution. BeginTime time.Time `json:"tbegin"` // EndTime is the timestamp when the command finished execution. EndTime time.Time `json:"tend"` // CmdName is name of the command. CmdName string `json:"cname"` // CmdRequest captures the command's arguments/input. CmdRequest interface{} `json:"creq"` // CmdResponse captures the command's output. CmdResponse interface{} `json:"cres"` // CmdMeta captures other metadata of the command. CmdMeta interface{} `json:"meta"` // Result is the output status upon command execution. Result interface{} `json:"result"` // Cost is the associated execution cost. Cost float64 `json:"cost"` // Error captures the execution error, if any. Error error `json:"error"` }
CmdExecInfo captures information around an executing command.
Available since v0.3.0
func (*CmdExecInfo) EndWithCost ¶ added in v0.3.0
func (cmd *CmdExecInfo) EndWithCost(cost float64, successResult, failedResult interface{}, err error)
EndWithCost is convenient function to "close" the command execution.
func (*CmdExecInfo) EndWithCostAsExecutionTime ¶ added in v0.3.0
func (cmd *CmdExecInfo) EndWithCostAsExecutionTime(successResult, failedResult interface{}, err error)
EndWithCostAsExecutionTime is convenient function to "close" the command execution and calculate the execution cost as the total microseconds taken.
type IBaseConnection ¶ added in v1.0.0
type IBaseConnection interface { // PoolOpts returns the pooling options attached to this connection. PoolOpts() IBasePoolOpts // MetricsLogger returns the metrics logger attached to this connection. MetricsLogger() IMetricsLogger // RegisterMetricsLogger attaches a metrics logger to this connection. // This function returns the connection itself for chaining. RegisterMetricsLogger(logger IMetricsLogger) IBaseConnection // NewCmdExecInfo is the convenient function to create a new CmdExecInfo instance. // The returned CmdExecInfo should have its 'id' and 'begin-time' fields initialized. NewCmdExecInfo() *CmdExecInfo // LogMetrics is the convenient function to put the CmdExecInfo to the metrics log. // This function is silently no-op if the input iis nil or there is no associated metrics logger. LogMetrics(category string, cmd *CmdExecInfo) error // Metrics is the convenient function to capture the snapshot of command execution metrics. // This function is silently no-op if there is no associated metrics logger. Metrics(category string, opts ...MetricsOpts) (*Metrics, error) }
IBaseConnection is the base interface to define a connection.
@Available since v1.0.0
type IBasePoolOpts ¶ added in v1.0.0
type IBasePoolOpts interface { }
IBasePoolOpts is the base interface to define configurations for a connection pool.
@Available since v1.0.0
type IMetricsLogger ¶ added in v0.3.0
type IMetricsLogger interface { // Put stores a CmdExecInfo instance. Put(category string, cmd *CmdExecInfo) error // Metrics returns the snapshot of command execution metrics. Metrics(category string, opts ...MetricsOpts) (*Metrics, error) }
IMetricsLogger defines APIs to log command executions and retrieve metrics.
Available since v0.3.0
func NewMemoryStoreMetricsLogger ¶ added in v0.3.0
func NewMemoryStoreMetricsLogger(capacity int) IMetricsLogger
NewMemoryStoreMetricsLogger creates a new MemoryStoreMetricsLogger instance.
- capacity: max number of items MemoryStoreMetricsLogger can hold.
Available since v0.3.0
type MemoryStoreMetricsLogger ¶ added in v0.3.0
type MemoryStoreMetricsLogger struct {
// contains filtered or unexported fields
}
MemoryStoreMetricsLogger is an in-memory bound storage implementation of IMetricsLogger.
Available since v0.3.0
func (*MemoryStoreMetricsLogger) Capacity ¶ added in v0.3.0
func (logger *MemoryStoreMetricsLogger) Capacity() int
Capacity returns max number of items can be hold by this logger.
func (*MemoryStoreMetricsLogger) Metrics ¶ added in v0.3.0
func (logger *MemoryStoreMetricsLogger) Metrics(category string, opts ...MetricsOpts) (*Metrics, error)
Metrics implements IMetricsLogger.Metrics
func (*MemoryStoreMetricsLogger) Put ¶ added in v0.3.0
func (logger *MemoryStoreMetricsLogger) Put(category string, cmd *CmdExecInfo) error
Put implements IMetricsLogger.Put
type Metrics ¶ added in v0.3.0
type Metrics struct { // Name of the category the metrics belong to. Category string `json:"cat"` // Total number of executed commands since the last reset. TotalNumCmds int64 `json:"total"` // It might not be practical to store all executed commands to calculate metrics with 100% accuracy. Most of the // time, stats of latest executed command is more important. This is the number of last executed commands used // to calculate the metrics. ReservoirNumCmds int64 `json:"window"` // The statistics minimum value of command execution cost. MinCost float64 `json:"min"` // The statistics maximum value of command execution cost. MaxCost float64 `json:"max"` // The statistics mean value of command execution cost. MeanCost float64 `json:"avg"` // The statistics p99 value of command execution cost. P99Cost float64 `json:"p99"` // The statistics p95 value of command execution cost. P95Cost float64 `json:"p95"` // The statistics p90 value of command execution cost. P90Cost float64 `json:"p90"` // The statistics p75 value of command execution cost. P75Cost float64 `json:"p75"` // The statistics p50 value of command execution cost. P50Cost float64 `json:"p50"` // Last N executed commands. LastNCmds []*CmdExecInfo `json:"last"` }
Metrics is the snapshot of command execution metrics.
Available since v0.3.0
type MetricsOpts ¶ added in v0.3.0
type MetricsOpts struct { // This option specifies the number of last commands to be returned along with the metrics. ReturnLatestCommands int }
MetricsOpts is argument used by function IMetricsLogger.Metrics.
Available since v0.3.0