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

[mongodbreceiver] Fix get mongodb version #27442

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 27 additions & 0 deletions .chloggen/receiver-mongodb-fix-get-version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'bug_fix'

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: mongodbreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Fix mongo version not being collected"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [27441]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
7 changes: 4 additions & 3 deletions receiver/mongodbreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbreceiver/internal/metadata"
)

var unknownVersion = func() *version.Version { return version.Must(version.NewVersion("0.0")) }

type mongodbScraper struct {
logger *zap.Logger
config *Config
Expand All @@ -30,12 +32,11 @@ type mongodbScraper struct {
}

func newMongodbScraper(settings receiver.CreateSettings, config *Config) *mongodbScraper {
v, _ := version.NewVersion("0.0")
return &mongodbScraper{
logger: settings.Logger,
config: config,
mb: metadata.NewMetricsBuilder(config.MetricsBuilderConfig, settings),
mongoVersion: v,
mongoVersion: unknownVersion(),
}
}

Expand All @@ -60,7 +61,7 @@ func (s *mongodbScraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
return pmetric.NewMetrics(), errors.New("no client was initialized before calling scrape")
}

if s.mongoVersion == nil {
if s.mongoVersion.Equal(unknownVersion()) {
version, err := s.client.GetVersion(ctx)
if err == nil {
s.mongoVersion = version
Expand Down
25 changes: 17 additions & 8 deletions receiver/mongodbreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ func TestScraperScrape(t *testing.T) {
testCases := []struct {
desc string
partialErr bool
setupMockClient func(t *testing.T) client
setupMockClient func(t *testing.T) *fakeClient
expectedMetricGen func(t *testing.T) pmetric.Metrics
expectedErr error
}{
{
desc: "Nil client",
partialErr: false,
setupMockClient: func(t *testing.T) client {
setupMockClient: func(t *testing.T) *fakeClient {
return nil
},
expectedMetricGen: func(t *testing.T) pmetric.Metrics {
Expand All @@ -139,7 +139,7 @@ func TestScraperScrape(t *testing.T) {
{
desc: "Failed to fetch database names",
partialErr: true,
setupMockClient: func(t *testing.T) client {
setupMockClient: func(t *testing.T) *fakeClient {
fc := &fakeClient{}
mongo40, err := version.NewVersion("4.0")
require.NoError(t, err)
Expand All @@ -155,7 +155,7 @@ func TestScraperScrape(t *testing.T) {
{
desc: "Failed to fetch collection names",
partialErr: true,
setupMockClient: func(t *testing.T) client {
setupMockClient: func(t *testing.T) *fakeClient {
fc := &fakeClient{}
mongo40, err := version.NewVersion("4.0")
require.NoError(t, err)
Expand All @@ -181,7 +181,7 @@ func TestScraperScrape(t *testing.T) {
{
desc: "Failed to scrape client stats",
partialErr: true,
setupMockClient: func(t *testing.T) client {
setupMockClient: func(t *testing.T) *fakeClient {
fc := &fakeClient{}
mongo40, err := version.NewVersion("4.0")
require.NoError(t, err)
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestScraperScrape(t *testing.T) {
{
desc: "Failed to scrape with partial errors on metrics",
partialErr: true,
setupMockClient: func(t *testing.T) client {
setupMockClient: func(t *testing.T) *fakeClient {
fc := &fakeClient{}
mongo40, err := version.NewVersion("4.0")
require.NoError(t, err)
Expand Down Expand Up @@ -240,7 +240,7 @@ func TestScraperScrape(t *testing.T) {
{
desc: "Successful scrape",
partialErr: false,
setupMockClient: func(t *testing.T) client {
setupMockClient: func(t *testing.T) *fakeClient {
fc := &fakeClient{}
adminStatus, err := loadAdminStatusAsMap()
require.NoError(t, err)
Expand Down Expand Up @@ -288,7 +288,12 @@ func TestScraperScrape(t *testing.T) {
scraperCfg.MetricsBuilderConfig.Metrics.MongodbHealth.Enabled = true

scraper := newMongodbScraper(receivertest.NewNopCreateSettings(), scraperCfg)
scraper.client = tc.setupMockClient(t)

mc := tc.setupMockClient(t)
if mc != nil {
scraper.client = mc
}
djaglowski marked this conversation as resolved.
Show resolved Hide resolved

actualMetrics, err := scraper.scrape(context.Background())
if tc.expectedErr == nil {
require.NoError(t, err)
Expand All @@ -307,6 +312,10 @@ func TestScraperScrape(t *testing.T) {
}
}

if mc != nil {
mc.AssertExpectations(t)
}

if tc.partialErr {
require.True(t, scrapererror.IsPartialScrapeError(err))
} else {
Expand Down