sqlmetrics

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2023 License: MIT Imports: 5 Imported by: 0

README

sqlmetrics

build-img pkg-img reportcard-img coverage-img version-img

Prometheus metrics for Go database/sql via VictoriaMetrics/metrics

Features

  • Simple API.
  • Easy to integrate.

Install

Go version 1.16+

go get github.com/cristalhq/sqlmetrics

Example

import (
    "github.com/VictoriaMetrics/metrics"
    "github.com/cristalhq/sqlmetrics"
)

// ...

db, err := sql.Open("<some-connection-string>")
if err != nil {
    panic(err)
}

ctx := context.Background() // or any other context you have
every := 3 * time.Second

sqlmetrics.NewCollector(ctx, db, every, "label1", "value1", "another", "etc")

// done, db metrics are registered
// you can see them here
w := &bytes.Buffer{}
metrics.WritePrometheus(w, true)

See this examples: example_test.go.

Documentation

See these docs.

License

MIT License.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

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

Collector for the sql.DBStats for Prometheus (via VictoriaMetrics client).

Example
package main

import (
	"bytes"
	"context"
	"database/sql"
	"time"

	"github.com/VictoriaMetrics/metrics"
	"github.com/cristalhq/sqlmetrics"
)

func main() {
	db, err := sql.Open("driver", "<some-connection-string>")
	if err != nil {
		panic(err)
	}

	ctx := context.Background() // or any other context you have
	every := 3 * time.Second

	sqlmetrics.NewCollector(ctx, db, every, "label1", "value1", "another", "etc")

	// done, db metrics are registered
	// you can see them here
	w := &bytes.Buffer{}
	metrics.WritePrometheus(w, true)
}
Output:

func NewCollector

func NewCollector(ctx context.Context, stats Statser, every time.Duration, labels ...string) *Collector

NewCollector creates a new Collector.

type Statser

type Statser interface {
	Stats() sql.DBStats
}

Statser is an interface that gets sql.DBStats. Most of the DB clients support this.

Jump to

Keyboard shortcuts

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