Skip to content

Commit

Permalink
go.mongodb.org/mongo-driver/mongo: update to v1.1.0 (#489)
Browse files Browse the repository at this point in the history
This change updates the MongoDB integration to use the latest stable and removes the hard coded dependency.

Fixes #488
  • Loading branch information
gbbr authored Sep 2, 2019
1 parent 90da00c commit e5f0418
Show file tree
Hide file tree
Showing 1,220 changed files with 145 additions and 202,743 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ jobs:
name: Wait for Cassandra
command: dockerize -wait tcp://localhost:9042 -timeout 2m

- run:
name: Wait for Mongo
command: dockerize -wait tcp://localhost:27017 -timeout 1m

- run:
name: Linting
command: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ package mongo_test
import (
"context"

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

mongotrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/mongodb/mongo-go-driver/mongo"
mongotrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/go.mongodb.org/mongo-driver/mongo"
)

func Example() {
// connect to MongoDB
opts := options.Client()
opts.SetMonitor(mongotrace.NewMonitor())
client, err := mongo.Connect(context.Background(), "mongodb://localhost:27017", opts)
opts.Monitor = mongotrace.NewMonitor()
opts.ApplyURI("mongodb://localhost:27017")
client, err := mongo.Connect(context.Background(), opts)
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/event"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/event"
)

type spanKey struct {
Expand Down
132 changes: 132 additions & 0 deletions contrib/go.mongodb.org/mongo-driver/mongo/mongo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2019 Datadog, Inc.

package mongo

import (
"context"
"fmt"
"os"
"testing"
"time"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig"

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

"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
_, ok := os.LookupEnv("INTEGRATION")
if !ok {
fmt.Println("--- SKIP: to enable integration test, set the INTEGRATION environment variable")
os.Exit(0)
}
os.Exit(m.Run())
}

func Test(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

hostname, port := "localhost", "27017"

ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()

span, ctx := tracer.StartSpanFromContext(ctx, "mongodb-test")

addr := fmt.Sprintf("mongodb://localhost:27017/?connect=direct")
opts := options.Client()
opts.Monitor = NewMonitor()
opts.ApplyURI(addr)
client, err := mongo.Connect(ctx, opts)
if err != nil {
t.Fatal(err)
}

client.
Database("test-database").
Collection("test-collection").
InsertOne(ctx, bson.D{{Key: "test-item", Value: "test-value"}})

span.Finish()

spans := mt.FinishedSpans()
assert.Len(t, spans, 2)
assert.Equal(t, spans[0].TraceID(), spans[1].TraceID())

s := spans[0]
assert.Equal(t, "mongo", s.Tag(ext.ServiceName))
assert.Equal(t, "mongo.insert", s.Tag(ext.ResourceName))
assert.Equal(t, hostname, s.Tag(ext.PeerHostname))
assert.Equal(t, port, s.Tag(ext.PeerPort))
assert.Contains(t, s.Tag(ext.DBStatement), `"test-item":"test-value"`)
assert.Equal(t, "test-database", s.Tag(ext.DBInstance))
assert.Equal(t, "mongo", s.Tag(ext.DBType))
}

func TestAnalyticsSettings(t *testing.T) {
assertRate := func(t *testing.T, mt mocktracer.Tracer, rate interface{}, opts ...Option) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()

addr := fmt.Sprintf("mongodb://localhost:27017/?connect=direct")
mongopts := options.Client()
mongopts.Monitor = NewMonitor(opts...)
mongopts.ApplyURI(addr)
client, err := mongo.Connect(ctx, mongopts)
if err != nil {
t.Fatal(err)
}
client.
Database("test-database").
Collection("test-collection").
InsertOne(ctx, bson.D{{Key: "test-item", Value: "test-value"}})

spans := mt.FinishedSpans()
assert.Len(t, spans, 1)
s := spans[0]
assert.Equal(t, rate, s.Tag(ext.EventSampleRate))
}

t.Run("defaults", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

assertRate(t, mt, nil)
})

t.Run("enabled", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

assertRate(t, mt, 1.0, WithAnalytics(true))
})

t.Run("disabled", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

assertRate(t, mt, nil, WithAnalytics(false))
})

t.Run("override", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

rate := globalconfig.AnalyticsRate()
defer globalconfig.SetAnalyticsRate(rate)
globalconfig.SetAnalyticsRate(0.4)

assertRate(t, mt, 0.23, WithAnalyticsRate(0.23))
})
}
Loading

0 comments on commit e5f0418

Please sign in to comment.