mongoprom

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2022 License: MIT Imports: 4 Imported by: 1

README

mongo-go-prometheus

Monitors that export Prometheus metrics for the MongoDB Go driver

Installation

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

Usage

package main

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

func main() {
	monitor := mongoprom.NewCommandMonitor(
		mongoprom.WithInstanceName("database"),
		mongoprom.WithNamespace("my_namespace"),
		mongoprom.WithDurationBuckets([]float64{.001, .005, .01}),
	)
	opts := options.Client().
		ApplyURI("mongodb://localhost:27019").
		SetMonitor(monitor)

	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"}

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.

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)

Jump to

Keyboard shortcuts

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