sql_exporter: github.com/free/sql_exporter Index | Files | Directories

package sql_exporter

import "github.com/free/sql_exporter"


Package Files

collector.go exporter.go job.go metric.go query.go sql.go target.go

func OpenConnection Uses

func OpenConnection(ctx context.Context, logContext, dsn string, maxConns, maxIdleConns int) (*sql.DB, error)

OpenConnection extracts the driver name from the DSN (expected as the URI scheme), adjusts it where necessary (e.g. some driver supported DSN formats don't include a scheme), opens a DB handle ensuring early termination if the context is closed (this is actually prevented by `database/sql` implementation), sets connection limits and returns the handle.

Below is the list of supported databases (with built in drivers) and their DSN formats. Unfortunately there is no dynamic way of loading a third party driver library (as e.g. with Java classpaths), so any driver additions require a binary rebuild.


Using the https://github.com/go-sql-driver/mysql driver, DSN format (passed to the driver stripped of the `mysql://` prefix):



Using the https://godoc.org/github.com/lib/pq driver, DSN format (passed through to the driver unchanged):


MS SQL Server

Using the https://github.com/denisenkom/go-mssqldb driver, DSN format (passed through to the driver unchanged):



Using the https://github.com/kshvakov/clickhouse driver, DSN format (passed to the driver with the`clickhouse://` prefix replaced with `tcp://`):


func PingDB Uses

func PingDB(ctx context.Context, conn *sql.DB) error

PingDB is a wrapper around sql.DB.PingContext() that terminates as soon as the context is closed.

sql.DB does not actually pass along the context to the driver when opening a connection (which always happens if the database is down) and the driver uses an arbitrary timeout which may well be longer than ours. So we run the ping call in a goroutine and terminate immediately if the context is closed.

type Collector Uses

type Collector interface {
    // Collect is the equivalent of prometheus.Collector.Collect() but takes a context to run in and a database to run on.
    Collect(context.Context, *sql.DB, chan<- Metric)

Collector is a self-contained group of SQL queries and metric families to collect from a specific database. It is conceptually similar to a prometheus.Collector.

func NewCollector Uses

func NewCollector(logContext string, cc *config.CollectorConfig, constLabels []*dto.LabelPair) (Collector, errors.WithContext)

NewCollector returns a new Collector with the given configuration and database. The metrics it creates will all have the provided const labels applied.

type Exporter Uses

type Exporter interface {

    // WithContext returns a (single use) copy of the Exporter, which will use the provided context for Gather() calls.
    WithContext(context.Context) Exporter
    // Config returns the Exporter's underlying Config object.
    Config() *config.Config

Exporter is a prometheus.Gatherer that gathers SQL metrics from targets and merges them with the default registry.

func NewExporter Uses

func NewExporter(configFile string) (Exporter, error)

NewExporter returns a new Exporter with the provided config.

type Job Uses

type Job interface {
    Targets() []Target

Job is a collection of targets with the same collectors applied.

func NewJob Uses

func NewJob(jc *config.JobConfig, gc *config.GlobalConfig) (Job, errors.WithContext)

NewJob returns a new Job with the given configuration.

type Metric Uses

type Metric interface {
    Desc() MetricDesc
    Write(out *dto.Metric) errors.WithContext

A Metric models a single sample value with its meta data being exported to Prometheus.

func NewInvalidMetric Uses

func NewInvalidMetric(err errors.WithContext) Metric

NewInvalidMetric returns a metric whose Write method always returns the provided error.

func NewMetric Uses

func NewMetric(desc MetricDesc, value float64, labelValues ...string) Metric

NewMetric returns a metric with one fixed value that cannot be changed.

NewMetric panics if the length of labelValues is not consistent with desc.labels().

type MetricDesc Uses

type MetricDesc interface {
    Name() string
    Help() string
    ValueType() prometheus.ValueType
    ConstLabels() []*dto.LabelPair
    Labels() []string
    LogContext() string

MetricDesc is a descriptor for a family of metrics, sharing the same name, help, labes, type.

func NewAutomaticMetricDesc Uses

func NewAutomaticMetricDesc(
    logContext, name, help string, valueType prometheus.ValueType, constLabels []*dto.LabelPair, labels ...string) MetricDesc

NewAutomaticMetricDesc creates a MetricDesc for automatically generated metrics.

type MetricFamily Uses

type MetricFamily struct {
    // contains filtered or unexported fields

MetricFamily implements MetricDesc for SQL metrics, with logic for populating its labels and values from sql.Rows.

func NewMetricFamily Uses

func NewMetricFamily(logContext string, mc *config.MetricConfig, constLabels []*dto.LabelPair) (*MetricFamily, errors.WithContext)

NewMetricFamily creates a new MetricFamily with the given metric config and const labels (e.g. job and instance).

func (MetricFamily) Collect Uses

func (mf MetricFamily) Collect(row map[string]interface{}, ch chan<- Metric)

Collect is the equivalent of prometheus.Collector.Collect() but takes a Query output map to populate values from.

func (MetricFamily) ConstLabels Uses

func (mf MetricFamily) ConstLabels() []*dto.LabelPair

ConstLabels implements MetricDesc.

func (MetricFamily) Help Uses

func (mf MetricFamily) Help() string

Help implements MetricDesc.

func (MetricFamily) Labels Uses

func (mf MetricFamily) Labels() []string

Labels implements MetricDesc.

func (MetricFamily) LogContext Uses

func (mf MetricFamily) LogContext() string

LogContext implements MetricDesc.

func (MetricFamily) Name Uses

func (mf MetricFamily) Name() string

Name implements MetricDesc.

func (MetricFamily) ValueType Uses

func (mf MetricFamily) ValueType() prometheus.ValueType

ValueType implements MetricDesc.

type Query Uses

type Query struct {
    // contains filtered or unexported fields

Query wraps a sql.Stmt and all the metrics populated from it. It helps extract keys and values from result rows.

func NewQuery Uses

func NewQuery(logContext string, qc *config.QueryConfig, metricFamilies ...*MetricFamily) (*Query, errors.WithContext)

NewQuery returns a new Query that will populate the given metric families.

func (*Query) Collect Uses

func (q *Query) Collect(ctx context.Context, conn *sql.DB, ch chan<- Metric)

Collect is the equivalent of prometheus.Collector.Collect() but takes a context to run in and a database to run on.

type Target Uses

type Target interface {
    // Collect is the equivalent of prometheus.Collector.Collect(), but takes a context to run in.
    Collect(ctx context.Context, ch chan<- Metric)

Target collects SQL metrics from a single sql.DB instance. It aggregates one or more Collectors and it looks much like a prometheus.Collector, except its Collect() method takes a Context to run in.

func NewTarget Uses

func NewTarget(
    logContext, name, dsn string, ccs []*config.CollectorConfig, constLabels prometheus.Labels, gc *config.GlobalConfig) (
    Target, errors.WithContext)

NewTarget returns a new Target with the given instance name, data source name, collectors and constant labels. An empty target name means the exporter is running in single target mode: no synthetic metrics will be exported.



Package sql_exporter imports 19 packages (graph). Updated 2019-11-29. Refresh now. Tools for package owners.