Documentation
¶
Overview ¶
Package otmongo provides mongo client with opentracing. For documentation about redis usage, see https://pkg.go.dev/go.mongodb.org/mongo-driver
Integration ¶
package otmongo exports the configuration in the following format:
redis:
default:
uri:
Add the mongo dependency to core:
var c *core.C = core.New() c.Provide(otmongo.Provide)
Then you can invoke redis from the application.
c.Invoke(func(client *mongo.Client) {
client.Connect(context.Background())
})
Sometimes there are valid reasons to connect to more than one mongo server. Inject otmongo.Maker to factory a *mongo.Client with a specific configuration entry.
c.Invoke(function(maker otredis.Maker) {
client, err := maker.Make("default")
// do something with client
})
Example ¶
package main
import (
"context"
"fmt"
"github.com/DoNewsCode/core"
"github.com/DoNewsCode/core/otmongo"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func main() {
c := core.New()
c.ProvideEssentials()
c.Provide(otmongo.Provide)
c.Invoke(func(mongo *mongo.Client) {
err := mongo.Ping(context.Background(), readpref.Nearest())
fmt.Println(err)
})
}
Output: <nil>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMonitor ¶
func NewMonitor(tracer opentracing.Tracer) *event.CommandMonitor
NewMonitor creates a new mongodb event CommandMonitor.
Types ¶
type Factory ¶
Factory is a *di.Factory that creates *mongo.Client using a specific configuration entry.
type MongoIn ¶
type MongoIn struct {
dig.In
Logger log.Logger
Conf contract.ConfigAccessor
Tracer opentracing.Tracer `optional:"true"`
}
MongoIn is the injection parameter for Provide.
type MongoOut ¶
type MongoOut struct {
dig.Out
Factory Factory
Maker Maker
Client *mongo.Client
ExportedConfig []config.ExportedConfig `group:"config,flatten"`
}
MongoOut is the result of Provide. The official mongo package doesn't provide a proper interface type. It is up to the users to define their own mongodb repository interface.