-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support instrumentation of go.mongodb.org/mongo-driver/mongo
- Loading branch information
Showing
8 changed files
with
252 additions
and
0 deletions.
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
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,136 @@ | ||
// 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 2023-present Datadog, Inc. | ||
|
||
package mongo | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/url" | ||
"orchestrion/integration/validator/trace" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/testcontainers/testcontainers-go" | ||
testmongo "github.com/testcontainers/testcontainers-go/modules/mongodb" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
type TestCase struct { | ||
server *testmongo.MongoDBContainer | ||
*mongo.Client | ||
} | ||
|
||
func (tc *TestCase) Setup(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
var err error | ||
tc.server, err = testmongo.Run(ctx, | ||
"mongo:6", | ||
testcontainers.WithLogger(testcontainers.TestLogger(t)), | ||
testcontainers.WithLogConsumers(testLogConsumer{t}), | ||
) | ||
if err != nil { | ||
t.Skipf("Failed to start mongo test container: %v\n", err) | ||
} | ||
|
||
mongoURI, err := tc.server.ConnectionString(ctx) | ||
if err != nil { | ||
log.Fatalf("Failed to obtain connection string: %v\n", err) | ||
} | ||
_, err = url.Parse(mongoURI) | ||
if err != nil { | ||
log.Fatalf("Invalid mongo connection string: %q\n", mongoURI) | ||
} | ||
opts := options.Client() | ||
opts.ApplyURI(mongoURI) | ||
client, err := mongo.Connect(context.Background(), opts) | ||
if err != nil { | ||
log.Fatalf("Failed to connect to mongo: %v\n", err) | ||
} | ||
tc.Client = client | ||
} | ||
|
||
func (tc *TestCase) Run(t *testing.T) { | ||
ctx := context.Background() | ||
span, ctx := tracer.StartSpanFromContext(ctx, "test.root") | ||
defer span.Finish() | ||
|
||
db := tc.Client.Database("test") | ||
c := db.Collection("coll") | ||
|
||
_, err := c.InsertOne(ctx, bson.M{"test_key": "test_value"}) | ||
require.NoError(t, err) | ||
r := c.FindOne(ctx, bson.M{"test_key": "test_value"}) | ||
require.NoError(t, r.Err()) | ||
} | ||
|
||
func (tc *TestCase) Teardown(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) | ||
defer cancel() | ||
|
||
assert.NoError(t, tc.Client.Disconnect(ctx)) | ||
assert.NoError(t, tc.server.Terminate(ctx)) | ||
} | ||
|
||
func (*TestCase) ExpectedTraces() trace.Spans { | ||
return trace.Spans{ | ||
{ | ||
Tags: map[string]any{ | ||
"name": "test.root", | ||
}, | ||
Children: trace.Spans{ | ||
{ | ||
Tags: map[string]any{ | ||
"name": "mongo.command", | ||
"service": "mongo.client", | ||
"resource": "set", | ||
"type": "mongo", | ||
}, | ||
Meta: map[string]any{ | ||
"mongo.args_length": "3", | ||
"component": "go-mongo/mongo.v8", | ||
"out.db": "0", | ||
"span.kind": "client", | ||
"db.system": "mongo", | ||
"mongo.raw_command": "set test_key test_value:", | ||
"out.host": "localhost", | ||
}, | ||
}, | ||
{ | ||
Tags: map[string]any{ | ||
"name": "mongo.command", | ||
"service": "mongo.client", | ||
"resource": "get", | ||
"type": "mongo", | ||
}, | ||
Meta: map[string]any{ | ||
"mongo.args_length": "2", | ||
"component": "go-mongo/mongo.v8", | ||
"out.db": "0", | ||
"span.kind": "client", | ||
"db.system": "mongo", | ||
"mongo.raw_command": "get test_key:", | ||
"out.host": "localhost", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
type testLogConsumer struct { | ||
*testing.T | ||
} | ||
|
||
func (t testLogConsumer) Accept(log testcontainers.Log) { | ||
t.T.Log(string(log.Content)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,26 @@ | ||
# 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 2023-present Datadog, Inc. | ||
%YAML 1.1 | ||
--- | ||
meta: | ||
name: go.mongodb.org/mongo-driver/mongo | ||
description: Official Golang driver for MongoDB. | ||
icon: database | ||
aspects: | ||
- id: Inject mongotrace.NewMonitor | ||
join-point: | ||
one-of: | ||
- function-call: go.mongodb.org/mongo-driver/mongo.Connect | ||
advice: | ||
- wrap-expression: | ||
imports: | ||
redis: go.mongodb.org/mongo-driver/mongo | ||
mongotrace: gopkg.in/DataDog/dd-trace-go.v1/contrib/go.mongodb.org/mongo-driver/mongo | ||
template: |- | ||
func() (opts *mongo.ClientOptions) { | ||
opts = {{ . }} | ||
opts.Monitor = mongotrace.NewMonitor() | ||
return | ||
}() |
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,30 @@ | ||
// 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 2023-present Datadog, Inc. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
func mongoClient() { | ||
ctx := context.Background() | ||
opts := options.Client().ApplyURI("mongodb://localhost:27017") | ||
client, err := mongo.Connect(ctx, opts) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer client.Disconnect(ctx) | ||
|
||
db := client.Database("test") | ||
c := db.Collection("coll") | ||
if _, err := c.InsertOne(ctx, bson.M{"key": "value"}); 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
Oops, something went wrong.