otelmongo

package module
v0.0.0-...-5e4307d Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0, BSD-3-Clause Imports: 15 Imported by: 16

Documentation

Overview

Package otelmongo instruments go.mongodb.org/mongo-driver/v2/mongo.

`NewMonitor` will return an event.CommandMonitor which is used to trace requests and collect its metrics.

This code was originally based on the following: - https://github.com/open-telemetry/opentelemetry-go-contrib/tree/323e373a6c15ae310bdd0617e3ed52d8cb8e4e6f/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo

Example
package main

import (
	"context"

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

	"go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo"
)

func main() {
	// connect to MongoDB
	opts := options.Client()
	opts.Monitor = otelmongo.NewMonitor()
	opts.ApplyURI("mongodb://localhost:27017")
	client, err := mongo.Connect(opts)
	if err != nil {
		panic(err)
	}

	defer func() {
		if err := client.Disconnect(context.TODO()); err != nil {
			panic(err)
		}
	}()
	db := client.Database("example")
	inventory := db.Collection("inventory")

	_, err = inventory.InsertOne(context.TODO(), bson.D{
		{Key: "item", Value: "canvas"},
		{Key: "qty", Value: 100},
		{Key: "attributes", Value: bson.A{"cotton"}},
		{Key: "size", Value: bson.D{
			{Key: "h", Value: 28},
			{Key: "w", Value: 35.5},
			{Key: "uom", Value: "cm"},
		}},
	})
	if err != nil {
		panic(err)
	}
}

Index

Examples

Constants

View Source
const ScopeName = "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo"

ScopeName is the instrumentation scope name.

View Source
const Version = "0.64.0"

Version is the current release version of the mongo-go-driver V2 instrumentation.

Variables

This section is empty.

Functions

func NewMonitor

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

NewMonitor creates a new mongodb event CommandMonitor.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option specifies instrumentation configuration options.

func WithCommandAttributeDisabled

func WithCommandAttributeDisabled(disabled bool) Option

WithCommandAttributeDisabled specifies if the MongoDB command is added as an attribute to Spans or not. This is disabled by default and the MongoDB command will not be added as an attribute to Spans if this option is not provided.

func WithMeterProvider

func WithMeterProvider(provider metric.MeterProvider) Option

WithMeterProvider specifies a metric.MeterProvider to use for creating a Meter. If none is specified, the global MeterProvider is used.

func WithSpanNameFormatter

func WithSpanNameFormatter(resolver SpanNameFormatterFunc) Option

WithSpanNameFormatter specifies a function that resolves the span name given an *event.CommandStartedEvent. If none is specified, the default resolver is used, which returns "<collection>.<command>" if the collection is non-empty, and just "<command>" otherwise.

Example
package main

import (
	"context"
	"fmt"

	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/event"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/options"

	"go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo"
)

func main() {
	// connect to MongoDB
	opts := options.Client()
	opts.Monitor = otelmongo.NewMonitor(
		otelmongo.WithSpanNameFormatter(func(event *event.CommandStartedEvent) string {
			// optionally, the collection name can be extracted for more
			// descriptive span names; see the extractCollection helper function
			// in the otelmongo package for an example of how to do this.
			return fmt.Sprintf("my-prefix-%s", event.CommandName)
		}),
	)
	opts.ApplyURI("mongodb://localhost:27017")
	client, err := mongo.Connect(opts)
	if err != nil {
		panic(err)
	}

	defer func() {
		if err := client.Disconnect(context.TODO()); err != nil {
			panic(err)
		}
	}()
	db := client.Database("mystore")
	inventory := db.Collection("inventory")

	_, err = inventory.InsertOne(context.TODO(), bson.D{
		{Key: "item", Value: "canvas"},
		// [..]
	})
	if err != nil {
		panic(err)
	}
}

func WithTracerProvider

func WithTracerProvider(provider trace.TracerProvider) Option

WithTracerProvider specifies a tracer provider to use for creating a tracer. If none is specified, the global provider is used.

type SpanNameFormatterFunc

type SpanNameFormatterFunc func(e *event.CommandStartedEvent) string

SpanNameFormatterFunc is a function that resolves the span name given an *event.CommandStartedEvent.

Jump to

Keyboard shortcuts

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