gormetrics

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

gormetrics

Go Report Card GoDoc

A plugin for GORM providing metrics using Prometheus.

Warning: this plugin is still in an early stage of development. APIs may change.

Usage

import "github.com/luyan-lyyy/gormetrics"

err := gormetrics.Register(db, <database name>)
if err != nil {
	// handle the error
}

gormetrics does not expose the metrics endpoint using promhttp, you have to do this yourself. You can use the following snippet for exposing metrics on port 2112 at /metrics:

go func() {
	http.Handle("/metrics", promhttp.Handler())
	log.Fatal(http.ListenAndServe(":2112", nil))
}()

Exported metrics

gormetrics exports the following metrics (counter vectors):

  • gormetrics_all_total
  • gormetrics_creates_total
  • gormetrics_deletes_total
  • gormetrics_queries_total
  • gormetrics_updates_total

These all have the following labels:

  • database: the name of the database
  • driver: the driver for the database (e.g. pq)
  • status: fail or success

It also export the following metrics (gauge vectors):

  • gormetrics_connections_idle
  • gormetrics_connections_in_use
  • gormetrics_connections_open
  • gormetrics_connections_max_open
  • gormetrics_connections_wait_for
  • gormetrics_connections_blocked_seconds
  • gormetrics_connections_closed_max_idle
  • gormetrics_connections_closed_max_lifetime

These all have the following labels:

  • database: the name of the database
  • driver: the driver for the database (e.g. pq)

Documentation

Overview

Package gormetrics provides an easy-to-use plugin for GORM which collects metrics about queries (namely status).

Index

Constants

View Source
const ErrDbIsNil gormetricsErr = "db is nil"

ErrDbIsNil is the error generated by gormetrics if an error occurs when creating a gormetrics plugin instance with a nil plugin.

Variables

This section is empty.

Functions

func Register

func Register(db *gorm.DB, dbName string, opts ...RegisterOpt) error

Register gormetrics. Options (opts) can be used to configure the Prometheus namespace and GORM plugin scope.

func RegisterInterface

func RegisterInterface(db gormi.DB, dbName string, opts ...RegisterOpt) error

RegisterInterface registers gormetrics with a gormi.DB interface, which can be created using one of the adapters in gormi/adapter. This can be useful if you use a forked version of GORM. Options (opts) can be used to configure the Prometheus namespace and GORM plugin scope.

Types

type RegisterOpt

type RegisterOpt func(o *pluginOpts)

RegisterOpt if a function that operates on pluginOpts, configuring one or more parameters of the plugin options.

func WithGORMPluginScope

func WithGORMPluginScope(s string) RegisterOpt

WithGORMPluginScope sets a different plugin scope for the configured callbacks. The default plugin scope is "gormetrics".

func WithPrometheusNamespace

func WithPrometheusNamespace(ns string) RegisterOpt

WithPrometheusNamespace sets a different namespace for the exported metrics. The default namespace is "gormetrics".

Directories

Path Synopsis
Package gormi provides interface so forks of gorm can be used to work with gormetrics.
Package gormi provides interface so forks of gorm can be used to work with gormetrics.

Jump to

Keyboard shortcuts

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