mongo_driver

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

MongoDB Go Driver

MongoDB Go Driver 提供了一组方法对命令执行情况进行监控。

// CommandMonitor represents a monitor that is triggered for different events.
type CommandMonitor struct {
	Started		func(context.Context, *CommandStartedEvent)
	Succeeded	func(context.Context, *CommandSucceededEvent)
	Failed		func(context.Context, *CommandFailedEvent)
}

这组方法可以在 Client 初始化时通过 SetMonitor 方法附带:

// SetMonitor specifies a CommandMonitor to receive command events. See the event.CommandMonitor documentation for more
// information about the structure of the monitor and events that can be received.
func (c *ClientOptions) SetMonitor(m *event.CommandMonitor) *ClientOptions {
	c.Monitor = m
	return c
}

因此,在 MongoDB Go Driver 中可以通过以下代码使用 go-contrib 埋点:

package metrics

import (
	"context"
	"fmt"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"

	mongo_driver "github.com/quwan-sre/observability-go-contrib/metrics/mongo-driver"
)

var mongoClient *mongo.Client

func initMongoDBClient() {
	...
	// 在初始化后使用 SetMonitor 方法增加埋点
	mongoClient, err = mongo.Connect(ctx, options.Client().ApplyURI("mongodb://test_user:password@localhost:27017").SetMonitor(mongo_driver.NewCommandMonitor()))
	if err != nil {
		panic(fmt.Sprintf("init mongo db client err: %v", err))
	}
	...
}

Jump to

Keyboard shortcuts

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