prommongo

package module
v0.0.0-...-b6050e2 Latest Latest
Warning

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

Go to latest
Published: May 13, 2022 License: MIT Imports: 4 Imported by: 0

README

prommongo

ci Go Reference codecov Go Report Card

Package prommongo exports pool stats of Mongo Driver as prometheus metrics collector.

Example

package prommongo_test

import (
	"context"
	"fmt"

	"github.com/prometheus/client_golang/prometheus"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"

	"github.com/johejo/prommongo"
)

func Example() {
	cmc := prommongo.NewCommandMonitorCollector()
	pmc := prommongo.NewPoolMonitorCollector()
	reg := prometheus.NewRegistry()
	reg.MustRegister(cmc, pmc)

	client, err := mongo.NewClient(
		options.Client().
			ApplyURI("mongodb://root:example@localhost:27017").
			SetMonitor(cmc.CommandMonitor(nil)).
			SetPoolMonitor(pmc.PoolMonitor(nil)),
	)
	if err != nil {
		panic(err)
	}
	ctx := context.Background()
	if err := client.Connect(ctx); err != nil {
		panic(err)
	}
	db := client.Database("testing")
	coll := db.Collection("prommongo")
	defer func() {
		if err := db.Drop(ctx); err != nil {
			panic(err)
		}
		if err := client.Disconnect(ctx); err != nil {
			panic(err)
		}
	}()

	_, err = coll.InsertOne(ctx, bson.M{"name": "Gopher"})
	if err != nil {
		panic(err)
	}

	if err := coll.FindOne(ctx, bson.M{"name": "Gopher"}).Err(); err != nil {
		panic(err)
	}

	ms, err := reg.Gather()
	if err != nil {
		panic(err)
	}
	for _, m := range ms {
		fmt.Println(*m.Name)
	}

	// Output:
	// go_mongo_command_duration_ns
	// go_mongo_connection_closed
	// go_mongo_connection_created
	// go_mongo_connection_returnd
	// go_mongo_get_failed
	// go_mongo_get_succeeded
	// go_mongo_max_pool_size
	// go_mongo_min_pool_size
	// go_mongo_pool_cleared
	// go_mongo_pool_closed
	// go_mongo_pool_created
	// go_mongo_wait_queue_timeout_ms
}

License

MIT

Author

Mitsuo Heijo

Documentation

Overview

Package prommongo exports pool stats of Mongo Driver as prometheus metrics collector.

Example
package main

import (
	"context"
	"fmt"

	"github.com/prometheus/client_golang/prometheus"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"

	"github.com/johejo/prommongo"
)

func main() {
	cmc := prommongo.NewCommandMonitorCollector()
	pmc := prommongo.NewPoolMonitorCollector()
	reg := prometheus.NewRegistry()
	reg.MustRegister(cmc, pmc)

	client, err := mongo.NewClient(
		options.Client().
			ApplyURI("mongodb://root:example@localhost:27017").
			SetMonitor(cmc.CommandMonitor(nil)).
			SetPoolMonitor(pmc.PoolMonitor(nil)),
	)
	if err != nil {
		panic(err)
	}
	ctx := context.Background()
	if err := client.Connect(ctx); err != nil {
		panic(err)
	}
	db := client.Database("testing")
	coll := db.Collection("prommongo")
	defer func() {
		if err := db.Drop(ctx); err != nil {
			panic(err)
		}
		if err := client.Disconnect(ctx); err != nil {
			panic(err)
		}
	}()

	_, err = coll.InsertOne(ctx, bson.M{"name": "Gopher"})
	if err != nil {
		panic(err)
	}

	if err := coll.FindOne(ctx, bson.M{"name": "Gopher"}).Err(); err != nil {
		panic(err)
	}

	ms, err := reg.Gather()
	if err != nil {
		panic(err)
	}
	for _, m := range ms {
		fmt.Println(*m.Name)
	}

}
Output:

go_mongo_command_duration_ns
go_mongo_connection_closed
go_mongo_connection_created
go_mongo_connection_returnd
go_mongo_get_failed
go_mongo_get_succeeded
go_mongo_max_pool_size
go_mongo_min_pool_size
go_mongo_pool_cleared
go_mongo_pool_closed
go_mongo_pool_created
go_mongo_wait_queue_timeout_ms

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandMonitorCollector

type CommandMonitorCollector interface {
	prometheus.Collector

	// CommandMonitor wraps the given *event.CommandMonitor and returns *event.CommandMonitor that collects the command metrics.
	// If nil is passed, it returns an parentless *event.CommandMonitor.
	CommandMonitor(parent *event.CommandMonitor) *event.CommandMonitor
}

CommandMonitorCollector extends prometheus.Collector.

func NewCommandMonitorCollector

func NewCommandMonitorCollector() CommandMonitorCollector

NewCommandMonitorCollector returns a new CommandMonitorCollector.

type PoolMonitorCollector

type PoolMonitorCollector interface {
	prometheus.Collector

	// PoolMonitor wraps the given *event.PoolMonitor and returns *event.PoolMonitor that collects the command metrics.
	// If nil is passed, it returns an parentless *event.PoolMonitor.
	PoolMonitor(parent *event.PoolMonitor) *event.PoolMonitor
}

CommandMonitorCollector extends prometheus.Collector.

func NewPoolMonitorCollector

func NewPoolMonitorCollector() PoolMonitorCollector

NewCommandMonitorCollector returns a new CommandMonitorCollector.

Jump to

Keyboard shortcuts

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