Documentation ¶
Overview ¶
Package mongo-driver provides functions to trace the go.mongodb.org/mongo-driver/mongo(https://github.com/mongodb/mongo-go-driver) package. It support v0.2.0 of github.com/mongodb/mongo-go-driver
`NewMonitor` will return an event.CommandMonitor which is used to trace requests.
This code was originally based on the following: - https://github.com/DataDog/dd-trace-go/tree/02f0449efa3cb382d499fadc873957385dcb2192/contrib/go.mongodb.org/mongo-driver/mongo - https://github.com/DataDog/dd-trace-go/tree/v1.23.3/ddtrace/ext
Example ¶
package main import ( "context" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" mongotrace "go.opentelemetry.io/contrib/plugins/go.mongodb.org/mongo-driver" ) func main() { // connect to MongoDB opts := options.Client() opts.Monitor = mongotrace.NewMonitor("test-service") 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) } }
Output:
Index ¶
- Constants
- func DBApplication(dbApplication string) kv.KeyValue
- func DBInstance(dbInstance string) kv.KeyValue
- func DBName(dbName string) kv.KeyValue
- func DBStatement(dbStatement string) kv.KeyValue
- func DBType(dbType string) kv.KeyValue
- func DBUser(dbUser string) kv.KeyValue
- func Error(err bool) kv.KeyValue
- func ErrorMsg(errorMsg string) kv.KeyValue
- func HTTPCode(httpCode string) kv.KeyValue
- func HTTPMethod(httpMethod string) kv.KeyValue
- func HTTPURL(httpURL string) kv.KeyValue
- func NewMonitor(serviceName string, opts ...Option) *event.CommandMonitor
- func PeerHostname(peerHostname string) kv.KeyValue
- func PeerPort(peerport string) kv.KeyValue
- func ResourceName(resourceName string) kv.KeyValue
- func ServiceName(serviceName string) kv.KeyValue
- func SpanType(spanType string) kv.KeyValue
- func TargetHost(targetHost string) kv.KeyValue
- func TargetPort(targetPort string) kv.KeyValue
- type Config
- type Option
Examples ¶
Constants ¶
const ( DBApplicationKey = kv.Key("db.application") DBNameKey = kv.Key("db.name") DBTypeKey = kv.Key("db.type") DBInstanceKey = kv.Key("db.instance") DBUserKey = kv.Key("db.user") DBStatementKey = kv.Key("db.statement") )
const ( // PeerHostname records the host name of the peer. PeerHostnameKey = kv.Key("peer.hostname") // PeerPort records the port number of the peer. PeerPortKey = kv.Key("peer.port") )
const ( TargetHostKey = kv.Key("out.host") TargetPortKey = kv.Key("out.port") HTTPMethodKey = kv.Key("http.method") HTTPCodeKey = kv.Key("http.code") HTTPURLKey = kv.Key("http.url") SpanTypeKey = kv.Key("span.type") ServiceNameKey = kv.Key("service.name") ResourceNameKey = kv.Key("resource.name") ErrorKey = kv.Key("error") ErrorMsgKey = kv.Key("error.msg") )
Variables ¶
This section is empty.
Functions ¶
func DBApplication ¶
DBApplication indicates the application using the database.
func DBInstance ¶
DBInstance indicates the instance name of Database.
func DBStatement ¶
DBStatement records a database statement for the given database type.
func HTTPMethod ¶
HTTPMethod specifies the HTTP method used in a span.
func NewMonitor ¶
func NewMonitor(serviceName string, opts ...Option) *event.CommandMonitor
NewMonitor creates a new mongodb event CommandMonitor.
func PeerHostname ¶
PeerHostname records the host name of the peer.
func ResourceName ¶
ResourceName defines the Resource name for the Span.
func ServiceName ¶
ServiceName defines the Service name for this Span.
func TargetHost ¶
TargetHost sets the target host address.
func TargetPort ¶
TargetPort sets the target host port.
Types ¶
type Option ¶
type Option func(*Config)
Option specifies instrumentation configuration options.
func WithTracer ¶
WithTracer specifies a tracer to use for creating spans. If none is specified, a tracer named "go.opentelemetry.io/contrib/plugins/go.mongodb.org/mongo-driver" from the global provider is used.