dbstats

package module
v0.0.0-...-c9db8cf Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2015 License: MIT Imports: 4 Imported by: 2

README

dbstats

Build Status Coverage GoDoc

A golang database/sql driver wrapper that provides hooks around database operations in order to gather usage/performance statistics.

Usage

dbstats provides a wrapper for a database/sql/driver.Driver . This is done by wrapping the Driver's Open function and then registering the new wrapped driver with database/sql. Once the driver has been wrapped, Hooks can be registered in order to gather various statistics. A very basic Hook CounterHook is provided by this package.

import (
  "database/sql"
  
  "github.com/cgilling/dbstats"
  "github.com/lib/pq"
)

var pqStats dbstats.CounterHook

func init() {
  s := dbstats.New(pq.Open)
  s.AddHook(pqStats)
  sql.Register("pqstats", s)
}

func main() {
  db, err := sql.Open("pqstats", "dbname=pqgotest"); // use the normal database connection string
  ... // use db as normal
  fmt.Printf("%d queries and %d execs\n", pqStats.Queries(), pqStats.Execs())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CounterHook

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

CounterHook is a Hook that keeps counters of various stats with respect to database usage.

func (*CounterHook) CommittedTxs

func (h *CounterHook) CommittedTxs() int

CommittedTxs returns the total number of transactions that were committed.

func (*CounterHook) ConnClosed

func (h *CounterHook) ConnClosed(err error)

ConnClosed implements ConnClosed of the Hook interface.

func (*CounterHook) ConnErrs

func (h *CounterHook) ConnErrs() int

ConnErrs returns the number of errors encountered trying to open a connection.

func (*CounterHook) ConnOpened

func (h *CounterHook) ConnOpened(err error)

ConnOpened implements ConnOpened of the Hook interface.

func (*CounterHook) ExecErrs

func (h *CounterHook) ExecErrs() int

ExexErrs returns the number of errors encountered trying to exec command.

func (*CounterHook) Execed

func (h *CounterHook) Execed(d time.Duration, query string, err error)

Execed implements Execed of the Hook interface.

func (*CounterHook) Execs

func (h *CounterHook) Execs() int

Execs returns the total number of Exex statements ran.

func (*CounterHook) OpenConns

func (h *CounterHook) OpenConns() int

OpenConns returns the current count of open connections.

func (*CounterHook) OpenStmts

func (h *CounterHook) OpenStmts() int

OpenStmts returns the current count of prepared statements.

func (*CounterHook) OpenTxs

func (h *CounterHook) OpenTxs() int

OpenTxs returns the current number of open transactions

func (*CounterHook) Queried

func (h *CounterHook) Queried(d time.Duration, query string, err error)

Queried implements Queried of the Hook interface.

func (*CounterHook) Queries

func (h *CounterHook) Queries() int

Queries returns the total number of Query statements ran.

func (*CounterHook) QueryErrs

func (h *CounterHook) QueryErrs() int

QueryErrs returns the number of errors encountered trying to run a query.

func (*CounterHook) RolledbackTxs

func (h *CounterHook) RolledbackTxs() int

RolledbackTxs returns the total number of transactions there were rolled back.

func (*CounterHook) RowErrs

func (h *CounterHook) RowErrs() int

RowErrs returns the number of error encountered while iterating rows.

func (*CounterHook) RowIterated

func (h *CounterHook) RowIterated(err error)

RowIterated implements RowIterated of the Hook interface.

func (*CounterHook) RowsIterated

func (h *CounterHook) RowsIterated() int

RowsIterated returns the total number of rows that have been iterated through.

func (*CounterHook) StmtClosed

func (h *CounterHook) StmtClosed(err error)

StmtClosed implements StmtClosed of the Hook interface.

func (*CounterHook) StmtErrs

func (h *CounterHook) StmtErrs() int

StmtErrs returns the number of errors encountered trying to prepare a statement.

func (*CounterHook) StmtPrepared

func (h *CounterHook) StmtPrepared(query string, err error)

StmtPrepared implements StmtPrepared of the Hook interface.

func (*CounterHook) TotalConns

func (h *CounterHook) TotalConns() int

TotalConns returns the total number of connections ever made.

func (*CounterHook) TotalStmts

func (h *CounterHook) TotalStmts() int

TotalStmts returns the total number of prepared statements ever made.

func (*CounterHook) TotalTxs

func (h *CounterHook) TotalTxs() int

TotalTxs returns the total number of transactions ever openned.

func (*CounterHook) TxBegan

func (h *CounterHook) TxBegan(err error)

TxBegan implements TxBegan of the Hook interface.

func (*CounterHook) TxCloseErrs

func (h *CounterHook) TxCloseErrs() int

TxCloseErrs returns the number of errors encountered trying to Commit or Rollback a transacttion.

func (*CounterHook) TxCommitted

func (h *CounterHook) TxCommitted(err error)

TxCommitted implements TxCommitted of the Hook interface.

func (*CounterHook) TxOpenErrs

func (h *CounterHook) TxOpenErrs() int

TxOpenErrs returns the number of errors encountered trying to start a transaction.

func (*CounterHook) TxRolledback

func (h *CounterHook) TxRolledback(err error)

TxRolledback implements TxRolledback of the Hook interface.

type Driver

type Driver interface {
	driver.Driver

	// AddHook will add a Hook to be called when various database events occurs. AddHook
	// should be called before any database activity happens as there is no gaurantee that
	// locking will occur between addining and using Hooks.
	AddHook(h Hook)
}

func New

func New(open OpenFunc) Driver

type Hook

type Hook interface {
	ConnOpened(err error)
	ConnClosed(err error)
	StmtPrepared(query string, err error)
	StmtClosed(err error)
	TxBegan(err error)
	TxCommitted(err error)
	TxRolledback(err error)
	Queried(d time.Duration, query string, err error)
	Execed(d time.Duration, query string, err error)
	RowIterated(err error)
}

Hook is an interface through which database events can be received. A Hook may received multiple events concurrently. Each function's last argument is of type error which will contain an error encountered while trying to perform the action. The one exception is RowInterated which will not return io.EOF because it is an expected return value.

type OpenFunc

type OpenFunc func(name string) (driver.Conn, error)

OpenFunc is the func used on driver.Driver. This is used as some driver libraries (lib/pq for example) do not expose their driver.Driver struct, but do expose an Open function.

Jump to

Keyboard shortcuts

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