Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

otelmongo: Use a mock deployment for testing against a MongoDB server #5749

Merged
merged 32 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
190e822
otelmongo#39 Convert to unit tests
prestonvasquez Jun 10, 2024
236658d
Merge branch 'main' into otelmongo#39
prestonvasquez Jun 10, 2024
74daa57
otelmongo#39 Fix linting errors
prestonvasquez Jun 10, 2024
bccc451
Merge branch 'otelmongo#39' of github.com:prestonvasquez/opentelemetr…
prestonvasquez Jun 10, 2024
5b20239
otelmongo#39 Add test-default back to precommit
prestonvasquez Jun 10, 2024
14448ad
otelmongo#39 Add empty line back to Makefile
prestonvasquez Jun 10, 2024
1bb7956
otelmongo#39 Remove empty line form Makefile
prestonvasquez Jun 10, 2024
a3297ca
otelmongo#39 Remove update to CHANGELOG
prestonvasquez Jun 11, 2024
efbef54
otelmongo#39 Make CI pass with third-party code
prestonvasquez Jun 11, 2024
67a9747
Merge branch 'main' into otelmongo#39
prestonvasquez Jun 11, 2024
7a41be0
Merge branch 'main' into otelmongo#39
prestonvasquez Jun 11, 2024
9840536
Merge branch 'main' into otelmongo#39
prestonvasquez Jun 13, 2024
e06df49
Update instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/te…
prestonvasquez Jun 13, 2024
e153791
Update instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/te…
prestonvasquez Jun 13, 2024
74892a2
Use mtest for mocking otelmongo
prestonvasquez Jul 30, 2024
f30710d
Revert changes to license-check Makefile
prestonvasquez Jul 30, 2024
f001417
Resolve merge conflicts
prestonvasquez Jul 30, 2024
167b222
Revert changes to license-check Makefile
prestonvasquez Jul 30, 2024
3021fc6
otelmongo#39 Resolve merge conflicts
prestonvasquez Aug 10, 2024
fe128ba
Merge branch 'main' into otelmongo#39
prestonvasquez Aug 13, 2024
d4731d4
#39 Resolve merge conflicts
prestonvasquez Aug 27, 2024
71cf7be
Merge branch 'otelmongo#39' of github.com:prestonvasquez/opentelemetr…
prestonvasquez Aug 27, 2024
1c01674
#39 Revert changes to ci
prestonvasquez Aug 27, 2024
9b43a52
#39 re-add removing ci integration
prestonvasquez Aug 29, 2024
aef92ac
Merge branch 'main' into otelmongo#39
prestonvasquez Aug 30, 2024
bafa299
otelmongo#39 Reomve parallel tests
prestonvasquez Sep 9, 2024
a26c56c
Merge branch 'otelmongo#39' of github.com:prestonvasquez/opentelemetr…
prestonvasquez Sep 9, 2024
81f250b
otelmongo#39 resolve merge conflict
prestonvasquez Sep 16, 2024
0a92c0d
Merge branch 'main' into otelmongo#39
pellared Sep 17, 2024
64afe72
Resolve merge conflicts
prestonvasquez Sep 19, 2024
e126410
Merge branch 'otelmongo#39' of github.com:prestonvasquez/opentelemetr…
prestonvasquez Sep 19, 2024
dd36791
Merge branch 'main' into otelmongo#39
prestonvasquez Sep 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,47 +137,3 @@ jobs:
echo ${{ needs.compatibility-test.result }}
test ${{ needs.compatibility-test.result }} == "success"

integration:
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
strategy:
matrix:
target: [test-mongo-driver]
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.DEFAULT_GO_VERSION }}
check-latest: true
cache-dependency-path: "**/go.sum"
- name: Run coverage tests ${{ matrix.target }}
env:
INTEGRATION: ${{ matrix.target }}
run: |
make ${{ matrix.target }}
mkdir -p $TEST_RESULTS
find . -name 'coverage.html' > "${TEST_RESULTS}/coverage.lst"
tar -n -cf - -T "${TEST_RESULTS}/coverage.lst" | tar -C "${TEST_RESULTS}" -xvf -
- name: Upload coverage report
uses: codecov/codecov-action@v4.4.1
if: hashFiles('coverage.out') != ''
with:
file: ./coverage.out
fail_ci_if_error: true
verbose: true
token: ${{ secrets.CODECOV_TOKEN }}
- name: Store coverage test output
uses: actions/upload-artifact@v4
with:
name: opentelemetry-go-contrib-integration-test-output
path: ${{ env.TEST_RESULTS }}

test-integration:
runs-on: ubuntu-latest
needs: [integration]
steps:
- name: Test if integration workflow passed
run: |
echo ${{ needs.integration.result }}
test ${{ needs.integration.result }} == "success"
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Improve performance of `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc` with the usage of `WithAttributeSet()` instead of `WithAttribute()`. (#5664)
- Improve performance of `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` with the usage of `WithAttributeSet()` instead of `WithAttribute()`. (#5664)
- Copy [opmsg_deployment.go](https://github.com/mongodb/mongo-go-driver/blob/d46f29c34e9ff3968a5c00dcdea8fb41a20a2b2f/mongo/integration/mtest/opmsg_deployment.go) from the mongo-go-driver mtest package to create a mock client for testing otelmongo. Remove all helpers specific to running the `test-mongo-driver` integration test. (#39)
prestonvasquez marked this conversation as resolved.
Show resolved Hide resolved

## [1.27.0/0.52.0/0.21.0/0.7.0/0.2.0] - 2024-05-21

Expand Down
21 changes: 2 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ TIMEOUT = 60
.DEFAULT_GOAL := precommit

.PHONY: precommit ci
precommit: generate license-check misspell go-mod-tidy golangci-lint-fix test-default
precommit: generate license-check misspell go-mod-tidy golangci-lint-fix
ci: generate license-check lint vanity-import-check build test-default check-clean-work-tree test-coverage

# Tools
Expand Down Expand Up @@ -263,23 +263,6 @@ test-coverage/%:
&& $$CMD ./... \
&& $(GO) tool cover -html=coverage.out -o coverage.html;

.PHONY: test-mongo-driver
test-mongo-driver:
@if ./tools/should_build.sh mongo-driver; then \
set -e; \
docker run --name mongo-integ --rm -p 27017:27017 -d mongo; \
CMD=mongo IMG_NAME=mongo-integ ./tools/wait.sh; \
(cd instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/test && \
$(GO) test \
-covermode=$(COVERAGE_MODE) \
-coverprofile=$(COVERAGE_PROFILE) \
-coverpkg=go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/... \
./... \
&& $(GO) tool cover -html=$(COVERAGE_PROFILE) -o coverage.html); \
cp ./instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/test/coverage.out ./; \
docker stop mongo-integ; \
fi

# Releasing

.PHONY: gorelease
Expand Down Expand Up @@ -351,4 +334,4 @@ genjsonschema: genjsonschema-cleanup $(GOJSONSCHEMA)

.PHONY: codespell
codespell: $(CODESPELL)
@$(DOCKERPY) $(CODESPELL)
@$(DOCKERPY) $(CODESPELL)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ go 1.21
require (
github.com/stretchr/testify v1.9.0
go.mongodb.org/mongo-driver v1.15.0
go.opentelemetry.io/contrib v1.27.0
go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo v0.52.0
go.opentelemetry.io/otel v1.27.0
go.opentelemetry.io/otel/sdk v1.27.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package test

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

Expand All @@ -15,19 +14,13 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"

"go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo" // nolint:staticcheck // deprecated.
pellared marked this conversation as resolved.
Show resolved Hide resolved
"go.opentelemetry.io/contrib/internal/util"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
)

func TestMain(m *testing.M) {
util.IntegrationShouldRun("test-mongo-driver")
os.Exit(m.Run())
}

type validator func(sdktrace.ReadOnlySpan) bool

func TestDBCrudOperation(t *testing.T) {
Expand All @@ -49,6 +42,7 @@ func TestDBCrudOperation(t *testing.T) {
tt := []struct {
title string
operation func(context.Context, *mongo.Database) (interface{}, error)
mockResponses []bson.D
excludeCommand bool
validators []validator
}{
Expand All @@ -57,6 +51,7 @@ func TestDBCrudOperation(t *testing.T) {
operation: func(ctx context.Context, db *mongo.Database) (interface{}, error) {
return db.Collection("test-collection").InsertOne(ctx, bson.D{{Key: "test-item", Value: "test-value"}})
},
mockResponses: []bson.D{{{Key: "ok", Value: 1}}},
excludeCommand: false,
validators: append(commonValidators, func(s sdktrace.ReadOnlySpan) bool {
for _, attr := range s.Attributes() {
Expand All @@ -72,6 +67,7 @@ func TestDBCrudOperation(t *testing.T) {
operation: func(ctx context.Context, db *mongo.Database) (interface{}, error) {
return db.Collection("test-collection").InsertOne(ctx, bson.D{{Key: "test-item", Value: "test-value"}})
},
mockResponses: []bson.D{{{Key: "ok", Value: 1}}},
excludeCommand: true,
validators: append(commonValidators, func(s sdktrace.ReadOnlySpan) bool {
for _, attr := range s.Attributes() {
Expand Down Expand Up @@ -105,12 +101,27 @@ func TestDBCrudOperation(t *testing.T) {
otelmongo.WithTracerProvider(provider),
otelmongo.WithCommandAttributeDisabled(tc.excludeCommand),
)

opts.ApplyURI(addr)

mock := newMockDeployment()

// nolint:staticcheck
//
// Deployment is not part of the stable API guaruntee of the
prestonvasquez marked this conversation as resolved.
Show resolved Hide resolved
// mongo-go-driver and is therefore marked as deprecated.
//
// See jira.mongodb.org/browse/GODRIVER-3241 for a long-term solution.
opts.Deployment = mock

client, err := mongo.Connect(ctx, opts)
if err != nil {
t.Fatal(err)
}

mock.addResponses(tc.mockResponses...)
t.Cleanup(mock.clearResponses)

_, err = tc.operation(ctx, client.Database("test-database"))
if err != nil {
t.Error(err)
Expand All @@ -131,7 +142,7 @@ func TestDBCrudOperation(t *testing.T) {
assert.Equal(t, trace.SpanKindClient, s.SpanKind())
attrs := s.Attributes()
assert.Contains(t, attrs, attribute.String("db.system", "mongodb"))
assert.Contains(t, attrs, attribute.String("net.peer.name", "localhost"))
assert.Contains(t, attrs, attribute.String("net.peer.name", "<mock_connection>"))
assert.Contains(t, attrs, attribute.Int64("net.peer.port", int64(27017)))
assert.Contains(t, attrs, attribute.String("net.transport", "ip_tcp"))
assert.Contains(t, attrs, attribute.String("db.name", "test-database"))
Expand All @@ -144,15 +155,17 @@ func TestDBCrudOperation(t *testing.T) {

func TestDBCollectionAttribute(t *testing.T) {
tt := []struct {
title string
operation func(context.Context, *mongo.Database) (interface{}, error)
validators []validator
title string
operation func(context.Context, *mongo.Database) (interface{}, error)
mockResponses []bson.D
validators []validator
}{
{
title: "delete",
operation: func(ctx context.Context, db *mongo.Database) (interface{}, error) {
return db.Collection("test-collection").DeleteOne(ctx, bson.D{{Key: "test-item"}})
},
mockResponses: []bson.D{{{Key: "ok", Value: 1}}},
validators: []validator{
func(s sdktrace.ReadOnlySpan) bool {
return assert.Equal(t, "test-collection.delete", s.Name())
Expand All @@ -173,6 +186,12 @@ func TestDBCollectionAttribute(t *testing.T) {
operation: func(ctx context.Context, db *mongo.Database) (interface{}, error) {
return db.ListCollectionNames(ctx, bson.D{})
},
mockResponses: []bson.D{
{
{Key: "ok", Value: 1},
{Key: "cursor", Value: bson.D{{Key: "firstBatch", Value: bson.A{}}}},
},
},
validators: []validator{
func(s sdktrace.ReadOnlySpan) bool {
return assert.Equal(t, "listCollections", s.Name())
Expand Down Expand Up @@ -202,12 +221,27 @@ func TestDBCollectionAttribute(t *testing.T) {
otelmongo.WithTracerProvider(provider),
otelmongo.WithCommandAttributeDisabled(true),
)

opts.ApplyURI(addr)

mock := newMockDeployment()

// nolint:staticcheck
//
// Deployment is not part of the stable API guaruntee of the
prestonvasquez marked this conversation as resolved.
Show resolved Hide resolved
// mongo-go-driver and is therefore marked as deprecated.
//
// See jira.mongodb.org/browse/GODRIVER-3241 for a long-term solution.
opts.Deployment = mock

client, err := mongo.Connect(ctx, opts)
if err != nil {
t.Fatal(err)
}

mock.addResponses(tc.mockResponses...)
t.Cleanup(mock.clearResponses)

_, err = tc.operation(ctx, client.Database("test-database"))
if err != nil {
t.Error(err)
Expand All @@ -228,7 +262,7 @@ func TestDBCollectionAttribute(t *testing.T) {
assert.Equal(t, trace.SpanKindClient, s.SpanKind())
attrs := s.Attributes()
assert.Contains(t, attrs, attribute.String("db.system", "mongodb"))
assert.Contains(t, attrs, attribute.String("net.peer.name", "localhost"))
assert.Contains(t, attrs, attribute.String("net.peer.name", "<mock_connection>"))
assert.Contains(t, attrs, attribute.Int64("net.peer.port", int64(27017)))
assert.Contains(t, attrs, attribute.String("net.transport", "ip_tcp"))
assert.Contains(t, attrs, attribute.String("db.name", "test-database"))
Expand Down
Loading