otelmongo

package module
v0.72.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0, BSD-3-Clause Imports: 10 Imported by: 157

Documentation

Overview

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

This package is compatible with v0.2.0 of go.mongodb.org/mongo-driver/mongo.

NewMonitor will return an event.CommandMonitor which is used to trace requests.

This code was originally based on the following:

The "OTEL_SEMCONV_STABILITY_OPT_IN" environment variable can be used to opt into emitting the previous (v1.21.0) semantic conventions alongside the current stable ones:

  • "": (default) emit the latest stable semantic conventions
  • "database/dup": emit both v1.21.0 and the latest stable semantic conventions

By default, otelmongo emits the latest stable semantic conventions.

Example
package main

import (
	"context"

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

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

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

	_, err = inventory.InsertOne(context.Background(), 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/mongo/otelmongo"

ScopeName is the instrumentation scope name.

View Source
const Version = "0.72.0"

Version is the current release version of the mongo-driver 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 added in v0.20.0

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 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.

Directories

Path Synopsis
internal
semconv
Package semconv provides semantic convention types and functionality.
Package semconv provides semantic convention types and functionality.
test module

Jump to

Keyboard shortcuts

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