prom

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: MIT Imports: 9 Imported by: 1

README

prom

Go Report Card PkgGoDev Actions Status codecov Release

Utility library to manage shared connections in Go.

Usage

prom itself does not provide functionality for direct use. Instead, use its sub-packages/modules:

  • 'Prom' for database/sql: (maintained as sub-package) help with managing shared database/sql connections and handling niche cases with various drivers and database types.

Contributing

Feel free to create pull requests or issues to report bugs or suggest new features. If you find this project useful, please start it.

If you develop a cool sub-package for prom, let me know and I will add it to the list above.

License

MIT - see LICENSE.md.

Documentation

Overview

Package prom provides helpers to manage shared connections in Go.

Index

Constants

View Source
const (
	CmdResultOk    = "OK"
	CmdResultError = "ERROR"
)
View Source
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"
)
View Source
const (
	// Version holds the semantic version number of this module.
	Version = "1.0.0"
)

Variables

This section is empty.

Functions

func NewId added in v0.4.0

func NewId() string

NewId generates a new unique id.

prom's internal use only!

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

Directories

Path Synopsis
Package sql provides database/sql specific implementation of btnguyen2k/prom and other utilities.
Package sql provides database/sql specific implementation of btnguyen2k/prom and other utilities.

Jump to

Keyboard shortcuts

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