mongoprom

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 5 Imported by: 0

README

mongo-go-prometheus

Monitors that export Prometheus metrics for the MongoDB Go driver

Installation

go get github.com/tracepath/mongo-go-prometheus

Usage

package main

import (
	"github.com/tracepath/mongo-go-prometheus"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
	monitor := mongoprom.NewCommandMonitor(
		mongoprom.WithInstanceName("database"),
		mongoprom.WithNamespace("my_namespace"),
		mongoprom.WithDurationBuckets([]float64{.001, .005, .01}),
	)

	poolMonitor := mongoprom.NewPoolMonitor(
		mongoprom.PoolWithInstanceName("database"),
		mongoprom.PoolWithNamespace("my_namespace"),
	)

	opts := options.Client().
		ApplyURI("mongodb://localhost:27019").
		SetMonitor(monitor).SetPoolMonitor(poolMonitor)

	client, err := mongo.Connect(context.TODO(), opts)
	if err != nil {
		panic(err)
	}

	// run MongoDB commands...
}

Exported metrics

The command monitor exports the following metrics:

  • Commands:
    • Histogram of commands: mongo_commands{instance="db", command="insert"}
    • Counter of errors: mongo_command_errors{instance="db", command="update"}
  • Pool:
    • Max number of connections allowed in Connection Pool: mongodb_connection_pool_max{instance="db"}
    • Min number of connections allowed in Connection Pool: mongodb_connection_pool_min{instance="db"}
    • Actual connections in usage: mongodb_connection_pool_usage{instance="db"}

API stability

The API is unstable at this point and it might change before v1.0.0 is released.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCommandMonitor

func NewCommandMonitor(opts ...Option) *event.CommandMonitor

NewCommandMonitor creates a event.CommandMonitor that exports metrics of Mongo commands. It also registers Prometheus collectors.

The following metrics are exported:

- Histogram of command duration. - Counter of command errors.

func NewPoolMonitor

func NewPoolMonitor(opts ...PoolOption) *event.PoolMonitor

Types

type Option

type Option func(*Options)

func WithDurationBuckets

func WithDurationBuckets(buckets []float64) Option

WithDurationBuckets sets the duration buckets of commands.

func WithInstanceName

func WithInstanceName(name string) Option

WithInstanceName sets the name of the MongoDB instance.

func WithNamespace

func WithNamespace(namespace string) Option

WithNamespace sets the namespace of all metrics.

type Options

type Options struct {
	InstanceName    string
	Namespace       string
	DurationBuckets []float64
}

Options represents options to customize the exported metrics.

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns the default options.

func (*Options) Merge

func (options *Options) Merge(opts ...Option)

type PoolConnectionsRepository

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

func (*PoolConnectionsRepository) Clear

func (c *PoolConnectionsRepository) Clear()

func (*PoolConnectionsRepository) Get

func (c *PoolConnectionsRepository) Get()

func (*PoolConnectionsRepository) Return

func (c *PoolConnectionsRepository) Return()

func (*PoolConnectionsRepository) Value

func (c *PoolConnectionsRepository) Value() int

type PoolOption

type PoolOption func(*PoolOptions)

func PoolWithInstanceName

func PoolWithInstanceName(name string) PoolOption

PoolWithInstanceName sets the name of the MongoDB instance.

func PoolWithNamespace

func PoolWithNamespace(namespace string) PoolOption

PoolWithNamespace sets the namespace of all metrics.

type PoolOptions

type PoolOptions struct {
	InstanceName string
	Namespace    string
}

PoolOptions represents options to customize the exported metrics.

func DefaultPoolOptions

func DefaultPoolOptions() *PoolOptions

DefaultPoolOptions returns the default options.

func (*PoolOptions) Merge

func (options *PoolOptions) Merge(opts ...PoolOption)

Jump to

Keyboard shortcuts

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