-
Notifications
You must be signed in to change notification settings - Fork 707
support mongo-driver's db metrics #7983
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
Open
minuk-dev
wants to merge
19
commits into
open-telemetry:main
Choose a base branch
from
minuk-dev:mongometric
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+248
−12
Open
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
827eb03
feat(mongo): add metrics support for MongoDB operations and connectio…
minuk-dev 4bf3481
Update instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo…
minuk-dev d32c713
chore: fix comment
minuk-dev 8556b11
fix typo
minuk-dev 490b3f6
feat: delete unstable & ambiguous status code
minuk-dev 6e764af
Merge branch 'main' into mongometric
minuk-dev a0b4106
docs: remove wrong comments
minuk-dev 6598ca1
fix: prevent nil panic when meter returns error
minuk-dev 77d48f8
fix: wrong type duration is not pointer
minuk-dev 36cd91f
Merge branch 'main' into mongometric
minuk-dev c8dce92
add comment for status code & TODO comments
minuk-dev 1b643c7
fix: handle when initialization failed
minuk-dev 8ad393f
test: make addr as const & assert err
minuk-dev e38d377
test: fix broken test due to merge conflict
minuk-dev 290ea96
use attribute.Set instead keyvalue
minuk-dev bb7eb18
Merge branch 'main' into mongometric
minuk-dev 38dfa7e
chore: fix lint
minuk-dev bce7ce9
Merge branch 'main' into mongometric
minuk-dev 13e621b
fix changelog
minuk-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
142 changes: 142 additions & 0 deletions
142
instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo/metrics_test.go
This file contains hidden or 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,142 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package otelmongo | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.mongodb.org/mongo-driver/v2/bson" | ||
| "go.mongodb.org/mongo-driver/v2/mongo" | ||
| "go.mongodb.org/mongo-driver/v2/mongo/options" | ||
| "go.mongodb.org/mongo-driver/v2/x/mongo/driver/drivertest" | ||
| "go.opentelemetry.io/otel/sdk/metric" | ||
| "go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
| ) | ||
|
|
||
| func TestMetricsOperationDuration(t *testing.T) { | ||
| reader := metric.NewManualReader() | ||
| provider := metric.NewMeterProvider(metric.WithReader(reader)) | ||
|
|
||
| md := drivertest.NewMockDeployment() | ||
|
|
||
| ctx, cancel := context.WithTimeout(t.Context(), time.Second*3) | ||
| defer cancel() | ||
|
|
||
| addr := "mongodb://localhost:27017/?connect=direct" | ||
| opts := options.Client() | ||
| opts.Deployment = md //nolint:staticcheck | ||
| opts.Monitor = NewMonitor( | ||
| WithMeterProvider(provider), | ||
| WithCommandAttributeDisabled(false), | ||
| ) | ||
| opts.ApplyURI(addr) | ||
|
|
||
| md.AddResponses([]bson.D{{{Key: "ok", Value: 1}}}...) | ||
| client, err := mongo.Connect(opts) | ||
| require.NoError(t, err) | ||
| defer func() { | ||
| err := client.Disconnect(t.Context()) | ||
| require.NoError(t, err) | ||
| }() | ||
|
|
||
| // Perform an insert operation | ||
| _, err = client.Database("test-database").Collection("test-collection").InsertOne(ctx, bson.D{{Key: "test-item", Value: "test-value"}}) | ||
| require.NoError(t, err) | ||
|
|
||
| // Collect metrics | ||
| var rm metricdata.ResourceMetrics | ||
| err = reader.Collect(ctx, &rm) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify metrics were recorded | ||
| require.Len(t, rm.ScopeMetrics, 1) | ||
| scopeMetrics := rm.ScopeMetrics[0] | ||
| assert.Equal(t, ScopeName, scopeMetrics.Scope.Name) | ||
|
|
||
| // Find the operation duration metric | ||
| var foundDuration bool | ||
| for _, m := range scopeMetrics.Metrics { | ||
| if m.Name == "db.client.operation.duration" { | ||
| foundDuration = true | ||
| histogram, ok := m.Data.(metricdata.Histogram[float64]) | ||
| assert.True(t, ok, "expected histogram data type") | ||
| assert.NotEmpty(t, histogram.DataPoints) | ||
|
|
||
| // Check that attributes are present | ||
| dp := histogram.DataPoints[0] | ||
| attrs := dp.Attributes.ToSlice() | ||
| hasDBSystem := false | ||
| hasOperation := false | ||
| for _, attr := range attrs { | ||
| if attr.Key == "db.system.name" && attr.Value.AsString() == "mongodb" { | ||
| hasDBSystem = true | ||
| } | ||
| if attr.Key == "db.operation.name" && attr.Value.AsString() == "insert" { | ||
| hasOperation = true | ||
| } | ||
| } | ||
| assert.True(t, hasDBSystem, "expected db.system.name attribute") | ||
| assert.True(t, hasOperation, "expected db.operation.name attribute") | ||
| } | ||
| } | ||
| assert.True(t, foundDuration, "expected db.client.operation.duration metric") | ||
| } | ||
|
|
||
| func TestMetricsOperationFailure(t *testing.T) { | ||
| reader := metric.NewManualReader() | ||
| provider := metric.NewMeterProvider(metric.WithReader(reader)) | ||
|
|
||
| md := drivertest.NewMockDeployment() | ||
|
|
||
| ctx, cancel := context.WithTimeout(t.Context(), time.Second*3) | ||
| defer cancel() | ||
|
|
||
| addr := "mongodb://localhost:27017/?connect=direct" | ||
dmathieu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| opts := options.Client() | ||
| opts.Deployment = md //nolint:staticcheck | ||
| opts.Monitor = NewMonitor( | ||
| WithMeterProvider(provider), | ||
| WithCommandAttributeDisabled(true), | ||
| ) | ||
| opts.ApplyURI(addr) | ||
|
|
||
| // Simulate an error response | ||
| md.AddResponses([]bson.D{{{Key: "ok", Value: 0}, {Key: "errmsg", Value: "test error"}}}...) | ||
| client, err := mongo.Connect(opts) | ||
| require.NoError(t, err) | ||
| defer func() { | ||
| err := client.Disconnect(t.Context()) | ||
| require.NoError(t, err) | ||
| }() | ||
|
|
||
| // This operation will fail | ||
| _, _ = client.Database("test-database").Collection("test-collection").InsertOne(ctx, bson.D{{Key: "test-item", Value: "test-value"}}) | ||
minuk-dev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Collect metrics | ||
| var rm metricdata.ResourceMetrics | ||
| err = reader.Collect(ctx, &rm) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify metrics were recorded even for failed operations | ||
| require.Len(t, rm.ScopeMetrics, 1) | ||
| scopeMetrics := rm.ScopeMetrics[0] | ||
| assert.NotEmpty(t, scopeMetrics.Metrics) | ||
| } | ||
|
|
||
| func TestNewMonitorWithInvalidMeterProvider(t *testing.T) { | ||
| // This test verifies that NewMonitor handles errors gracefully | ||
| // even if metric creation fails. The function should not panic | ||
| // and should return a valid monitor that can be used. | ||
|
|
||
| // Using a nil meter provider will use the global one, which should work | ||
| monitor := NewMonitor() | ||
| assert.NotNil(t, monitor) | ||
| assert.NotNil(t, monitor.Started) | ||
| assert.NotNil(t, monitor.Succeeded) | ||
| assert.NotNil(t, monitor.Failed) | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.