Skip to content

Commit

Permalink
Port mongodb driver instrumentation (#26)
Browse files Browse the repository at this point in the history
* Copy mongo driver

* Add an updated copy of otel-go mock tracer

* Integrate mock tracer

* Update tests

* Update copyright

* Move to plugins dir

* Move mongo plugin to go.mongodb.org dir

* Upgrade to v0.5.0

* Run make

Includes fixes needed to past testing.

* Update attribution

Based on [this](open-telemetry/community#305 (comment))
comment, updating attribution to be to The OpenTelemetry Authors.

* package.go -> doc.go

* Update package name

* Update package docs

* Suggested fix for docs

* Apply feedback

* Remove unneeded go.mod replaces

* Update testing service name

Co-authored-by: Krzesimir Nowak <krzesimir@kinvolk.io>
Co-authored-by: Tyler Yahn <codingalias@gmail.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
4 people authored May 15, 2020
1 parent e70e751 commit f8f0c98
Show file tree
Hide file tree
Showing 11 changed files with 709 additions and 1 deletion.
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
54 changes: 54 additions & 0 deletions plugins/go.mongodb.org/mongo-driver/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mongo

import (
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/trace"
)

const (
defaultTracerName = "go.opentelemetry.io/contrib/plugins/go.mongodb.org/mongo-driver"
)

// Config is used to configure the mongo tracer.
type Config struct {
Tracer trace.Tracer
}

// newConfig returns a Config with all Options set.
func newConfig(opts ...Option) Config {
cfg := Config{}
for _, opt := range opts {
opt(&cfg)
}
if cfg.Tracer == nil {
cfg.Tracer = global.Tracer(defaultTracerName)
}
return cfg
}

// Option specifies instrumentation configuration options.
type Option func(*Config)

// 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.
func WithTracer(tracer trace.Tracer) Option {
return func(cfg *Config) {
cfg.Tracer = tracer
}
}
56 changes: 56 additions & 0 deletions plugins/go.mongodb.org/mongo-driver/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mongo

import "go.opentelemetry.io/otel/api/kv"

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")
)

// DBApplication indicates the application using the database.
func DBApplication(dbApplication string) kv.KeyValue {
return DBApplicationKey.String(dbApplication)
}

// DBName indicates the database name.
func DBName(dbName string) kv.KeyValue {
return DBNameKey.String(dbName)
}

// DBType indicates the type of Database.
func DBType(dbType string) kv.KeyValue {
return DBTypeKey.String(dbType)
}

// DBInstance indicates the instance name of Database.
func DBInstance(dbInstance string) kv.KeyValue {
return DBInstanceKey.String(dbInstance)
}

// DBUser indicates the user name of Database, e.g. "readonly_user" or "reporting_user".
func DBUser(dbUser string) kv.KeyValue {
return DBUserKey.String(dbUser)
}

// DBStatement records a database statement for the given database type.
func DBStatement(dbStatement string) kv.KeyValue {
return DBStatementKey.String(dbStatement)
}
25 changes: 25 additions & 0 deletions plugins/go.mongodb.org/mongo-driver/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// 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
package mongo // import "go.opentelemetry.io/contrib/plugins/go.mongodb.org/mongo-driver"
52 changes: 52 additions & 0 deletions plugins/go.mongodb.org/mongo-driver/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mongo_test

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 Example() {
// 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)
}
}
15 changes: 15 additions & 0 deletions plugins/go.mongodb.org/mongo-driver/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module go.opentelemetry.io/contrib/plugins/go.mongodb.org/mongo-driver

go 1.13

replace go.opentelemetry.io/contrib => ../../..

require (
github.com/stretchr/testify v1.4.0
github.com/xdg/stringprep v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.3.2
go.opentelemetry.io/contrib v0.0.0-00010101000000-000000000000
go.opentelemetry.io/otel v0.5.0
golang.org/x/crypto v0.0.0-20191105034135-c7e5f84aec59 // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
)
Loading

0 comments on commit f8f0c98

Please sign in to comment.