gormetrics

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

README

Gormetrics

Go Report Card GoDoc

Forked from profects/gormetrics.

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/survivorbat/gormetrics"

if err := gormetrics.Register(db, "my_database"); 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

Type Metric Purpose
Counter gormetrics_all_total Counts how many queries have been performed
Counter gormetrics_creates_total Counts how many create-queries have been performed
Counter gormetrics_deletes_total Counts how many delete-queries have been performed
Counter gormetrics_updates_total Counts how many update-queries have been performed
Counter gormetrics_queries_total Counts how many select-queries have been performed
Histogram gormetrics_all_duration A histogram of all query durations in milliseconds
Histogram gormetrics_creates_duration A histogram of create-query durations in milliseconds
Histogram gormetrics_deletes_duration A histogram of delete-query durations in milliseconds
Histogram gormetrics_updates_duration A histogram of update-query durations in milliseconds
Histogram gormetrics_queries_duration A histogram of select-query durations in milliseconds
Gauge gormetrics_connections_idle Amount of idle connections
Gauge gormetrics_connections_in_use Amount of in-use connections
Gauge gormetrics_connections_open Amount of open connections

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 (only for query-related metrics)

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 *gorm.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".

Jump to

Keyboard shortcuts

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