-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port mongodb driver instrumentation (#26)
* 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
1 parent
e70e751
commit f8f0c98
Showing
11 changed files
with
709 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.