base

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2025 License: Apache-2.0 Imports: 11 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotConnected  = errors.New("database not connected")
	ErrAlreadyClosed = errors.New("database already closed")
)

Functions

This section is empty.

Types

type BaseSQLPlugin

type BaseSQLPlugin struct {
	*plugins.BasePlugin
	// contains filtered or unexported fields
}

BaseSQLPlugin provides common functionality for all SQL plugins

func NewBaseSQLPlugin

func NewBaseSQLPlugin(
	id, name, desc, version, confPrefix string,
	weight int,
	config *interfaces.Config,
) *BaseSQLPlugin

NewBaseSQLPlugin creates a new base SQL plugin

func (*BaseSQLPlugin) CheckHealth

func (p *BaseSQLPlugin) CheckHealth() error

CheckHealth performs a health check

func (*BaseSQLPlugin) CleanupTasks

func (p *BaseSQLPlugin) CleanupTasks() error

CleanupTasks performs cleanup on shutdown

func (*BaseSQLPlugin) GetDB

func (p *BaseSQLPlugin) GetDB() (*sql.DB, error)

GetDB returns the database connection

func (*BaseSQLPlugin) GetDialect

func (p *BaseSQLPlugin) GetDialect() string

GetDialect returns the database dialect

func (*BaseSQLPlugin) GetMetricsRecorder

func (p *BaseSQLPlugin) GetMetricsRecorder() MetricsRecorder

GetMetricsRecorder returns the current metrics recorder

func (*BaseSQLPlugin) GetStats

func (p *BaseSQLPlugin) GetStats() *ConnectionPoolStats

GetStats returns connection pool statistics

func (*BaseSQLPlugin) InitializeResources

func (p *BaseSQLPlugin) InitializeResources(rt plugins.Runtime) error

InitializeResources initializes plugin resources

func (*BaseSQLPlugin) IsConnected

func (p *BaseSQLPlugin) IsConnected() bool

IsConnected checks if database is connected

func (*BaseSQLPlugin) SetMetricsRecorder

func (p *BaseSQLPlugin) SetMetricsRecorder(recorder MetricsRecorder)

SetMetricsRecorder sets the metrics recorder for this plugin

func (*BaseSQLPlugin) StartupTasks

func (p *BaseSQLPlugin) StartupTasks() error

StartupTasks performs startup initialization

type ConnectionPoolStats

type ConnectionPoolStats struct {
	MaxOpenConnections int64         // Maximum number of open connections
	OpenConnections    int64         // Number of established connections
	InUse              int64         // Number of connections currently in use
	Idle               int64         // Number of idle connections
	MaxIdleConnections int64         // Maximum number of idle connections
	WaitCount          int64         // Total number of connections waited for
	WaitDuration       time.Duration // Total time blocked waiting for a new connection
	MaxIdleClosed      int64         // Total number of connections closed due to SetMaxIdleConns
	MaxLifetimeClosed  int64         // Total number of connections closed due to SetConnMaxLifetime
}

ConnectionPoolStats represents database connection pool statistics

type HealthCheckable

type HealthCheckable interface {
	CheckHealth() error
	Name() string
}

HealthCheckable interface for health checkable components

type HealthChecker

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

HealthChecker performs periodic health checks

func NewHealthChecker

func NewHealthChecker(target HealthCheckable, interval time.Duration, customQuery string) *HealthChecker

NewHealthChecker creates a new health checker

func (*HealthChecker) IsHealthy

func (h *HealthChecker) IsHealthy() bool

IsHealthy returns the current health status

func (*HealthChecker) Start

func (h *HealthChecker) Start(ctx context.Context)

Start starts the health check routine

func (*HealthChecker) Stop

func (h *HealthChecker) Stop()

Stop stops the health checker

type MetricsConfig

type MetricsConfig struct {
	// Enabled determines if metrics recording is enabled
	Enabled bool

	// Namespace for Prometheus metrics
	Namespace string

	// Subsystem for Prometheus metrics
	Subsystem string

	// Labels to add to all metrics
	Labels map[string]string

	// SlowQueryThreshold defines the threshold for slow query detection
	SlowQueryThreshold time.Duration
}

MetricsConfig defines configuration for metrics recording

func DefaultMetricsConfig

func DefaultMetricsConfig() *MetricsConfig

DefaultMetricsConfig returns default metrics configuration

type MetricsRecorder

type MetricsRecorder interface {
	// RecordConnectionPoolStats records connection pool statistics
	RecordConnectionPoolStats(stats *ConnectionPoolStats)

	// RecordHealthCheck records health check results
	RecordHealthCheck(success bool)

	// RecordQuery records SQL query duration and errors
	RecordQuery(duration time.Duration, err error, threshold time.Duration)

	// RecordTx records transaction duration and status
	RecordTx(duration time.Duration, committed bool)

	// IncConnectAttempt increments connection attempt counter
	IncConnectAttempt()

	// IncConnectRetry increments connection retry counter
	IncConnectRetry()

	// IncConnectSuccess increments connection success counter
	IncConnectSuccess()

	// IncConnectFailure increments connection failure counter
	IncConnectFailure()
}

MetricsRecorder defines the interface for recording database metrics

type NoOpMetricsRecorder

type NoOpMetricsRecorder struct{}

NoOpMetricsRecorder provides a no-operation implementation of MetricsRecorder This is useful when metrics recording is disabled or not implemented

func (*NoOpMetricsRecorder) IncConnectAttempt

func (n *NoOpMetricsRecorder) IncConnectAttempt()

IncConnectAttempt implements MetricsRecorder

func (*NoOpMetricsRecorder) IncConnectFailure

func (n *NoOpMetricsRecorder) IncConnectFailure()

IncConnectFailure implements MetricsRecorder

func (*NoOpMetricsRecorder) IncConnectRetry

func (n *NoOpMetricsRecorder) IncConnectRetry()

IncConnectRetry implements MetricsRecorder

func (*NoOpMetricsRecorder) IncConnectSuccess

func (n *NoOpMetricsRecorder) IncConnectSuccess()

IncConnectSuccess implements MetricsRecorder

func (*NoOpMetricsRecorder) RecordConnectionPoolStats

func (n *NoOpMetricsRecorder) RecordConnectionPoolStats(stats *ConnectionPoolStats)

RecordConnectionPoolStats implements MetricsRecorder

func (*NoOpMetricsRecorder) RecordHealthCheck

func (n *NoOpMetricsRecorder) RecordHealthCheck(success bool)

RecordHealthCheck implements MetricsRecorder

func (*NoOpMetricsRecorder) RecordQuery

func (n *NoOpMetricsRecorder) RecordQuery(duration time.Duration, err error, threshold time.Duration)

RecordQuery implements MetricsRecorder

func (*NoOpMetricsRecorder) RecordTx

func (n *NoOpMetricsRecorder) RecordTx(duration time.Duration, committed bool)

RecordTx implements MetricsRecorder

type PrometheusMetrics

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

PrometheusMetrics provides a unified Prometheus metrics implementation

func NewPrometheusMetrics

func NewPrometheusMetrics(config *MetricsConfig) *PrometheusMetrics

NewPrometheusMetrics creates a new Prometheus metrics instance

func (*PrometheusMetrics) GetGatherer

func (pm *PrometheusMetrics) GetGatherer() prometheus.Gatherer

GetGatherer returns the Prometheus gatherer

func (*PrometheusMetrics) IncConnectAttempt

func (pm *PrometheusMetrics) IncConnectAttempt()

IncConnectAttempt implements MetricsRecorder

func (*PrometheusMetrics) IncConnectFailure

func (pm *PrometheusMetrics) IncConnectFailure()

IncConnectFailure implements MetricsRecorder

func (*PrometheusMetrics) IncConnectRetry

func (pm *PrometheusMetrics) IncConnectRetry()

IncConnectRetry implements MetricsRecorder

func (*PrometheusMetrics) IncConnectSuccess

func (pm *PrometheusMetrics) IncConnectSuccess()

IncConnectSuccess implements MetricsRecorder

func (*PrometheusMetrics) RecordConnectionPoolStats

func (pm *PrometheusMetrics) RecordConnectionPoolStats(stats *ConnectionPoolStats)

RecordConnectionPoolStats implements MetricsRecorder

func (*PrometheusMetrics) RecordHealthCheck

func (pm *PrometheusMetrics) RecordHealthCheck(success bool)

RecordHealthCheck implements MetricsRecorder

func (*PrometheusMetrics) RecordQuery

func (pm *PrometheusMetrics) RecordQuery(duration time.Duration, err error, threshold time.Duration)

RecordQuery implements MetricsRecorder

func (*PrometheusMetrics) RecordTx

func (pm *PrometheusMetrics) RecordTx(duration time.Duration, committed bool)

RecordTx implements MetricsRecorder

Jump to

Keyboard shortcuts

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