Skip to content

Commit 2605e8c

Browse files
[receiver/mongodbreceiver] Added mongodb replica metrics and routing logic for multiple mongodb instances (#37517)
#### Description - Added mongodb replica metrics and routing logic for multiple mongodb instances ``` mongodb.repl_queries_per_sec mongodb.repl_inserts_per_sec mongodb.repl_commands_per_sec mongodb.repl_getmores_per_sec mongodb.repl_deletes_per_sec mongodb.repl_updates_per_sec ``` <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue - https://sumologic.atlassian.net/browse/OSC-901 <!--Describe what testing was performed and which tests were added.--> #### Testing Screenshot examples: - [mongodb.inserts_per_sec](https://github.com/user-attachments/assets/3311b5ce-f969-4ce1-97db-8af4337983e6) - [mongodb.queries_per_sec](https://github.com/user-attachments/assets/4a1c00c8-aa32-4198-bcfd-f9952a0d2388) <!--Describe the documentation added.--> #### Documentation - https://github.com/influxdata/telegraf/tree/master/plugins/inputs/mongodb - https://www.mongodb.com/docs/manual/reference/command/serverStatus/#opcounters <!--Please delete paragraphs that you did not use before submitting.-->
1 parent d51d861 commit 2605e8c

17 files changed

+839
-26
lines changed

.chloggen/mongodbReplicaMetrics.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: mongodbreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Added mongodb replica metrics and routing logic for multiple mongodb instances
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [37517]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

receiver/mongodbreceiver/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type client interface {
2626
DBStats(ctx context.Context, DBName string) (bson.M, error)
2727
TopStats(ctx context.Context) (bson.M, error)
2828
IndexStats(ctx context.Context, DBName, collectionName string) ([]bson.M, error)
29+
RunCommand(ctx context.Context, db string, command bson.M) (bson.M, error)
2930
}
3031

3132
// mongodbClient is a mongodb metric scraper client
@@ -37,12 +38,11 @@ type mongodbClient struct {
3738

3839
// newClient creates a new client to connect and query mongo for the
3940
// mongodbreceiver
40-
func newClient(ctx context.Context, config *Config, logger *zap.Logger) (client, error) {
41-
driver, err := mongo.Connect(ctx, config.ClientOptions())
41+
var newClient = func(ctx context.Context, config *Config, logger *zap.Logger, secondary bool) (client, error) {
42+
driver, err := mongo.Connect(ctx, config.ClientOptions(secondary))
4243
if err != nil {
4344
return nil, err
4445
}
45-
4646
return &mongodbClient{
4747
cfg: config,
4848
logger: logger,

receiver/mongodbreceiver/client_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ func (fc *fakeClient) IndexStats(ctx context.Context, dbName, collectionName str
6969
return args.Get(0).([]bson.M), args.Error(1)
7070
}
7171

72+
func (fc *fakeClient) RunCommand(ctx context.Context, db string, command bson.M) (bson.M, error) {
73+
args := fc.Called(ctx, db, command)
74+
if args.Get(0) == nil {
75+
return nil, args.Error(1)
76+
}
77+
78+
result, ok := args.Get(0).(bson.M)
79+
if !ok {
80+
err := errors.New("mock returned invalid type")
81+
zap.L().Error("type assertion failed",
82+
zap.String("expected", "bson.M"))
83+
return nil, err
84+
}
85+
86+
return result, args.Error(1)
87+
}
88+
7289
func TestListDatabaseNames(t *testing.T) {
7390
mont := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
7491

receiver/mongodbreceiver/config.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"go.mongodb.org/mongo-driver/mongo/options"
14+
"go.mongodb.org/mongo-driver/mongo/readpref"
1415
"go.opentelemetry.io/collector/config/confignet"
1516
"go.opentelemetry.io/collector/config/configopaque"
1617
"go.opentelemetry.io/collector/config/configtls"
@@ -59,7 +60,27 @@ func (c *Config) Validate() error {
5960
return err
6061
}
6162

62-
func (c *Config) ClientOptions() *options.ClientOptions {
63+
func (c *Config) ClientOptions(secondary bool) *options.ClientOptions {
64+
if secondary {
65+
// For secondary nodes, create a direct connection
66+
clientOptions := options.Client().
67+
SetHosts(c.hostlist()).
68+
SetDirect(true).
69+
SetReadPreference(readpref.SecondaryPreferred())
70+
71+
if c.Timeout > 0 {
72+
clientOptions.SetConnectTimeout(c.Timeout)
73+
}
74+
75+
if c.Username != "" && c.Password != "" {
76+
clientOptions.SetAuth(options.Credential{
77+
Username: c.Username,
78+
Password: string(c.Password),
79+
})
80+
}
81+
82+
return clientOptions
83+
}
6384
clientOptions := options.Client()
6485
connString := "mongodb://" + strings.Join(c.hostlist(), ",")
6586
clientOptions.ApplyURI(connString)

receiver/mongodbreceiver/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestOptions(t *testing.T) {
166166
ReplicaSet: "rs-1",
167167
}
168168

169-
clientOptions := cfg.ClientOptions()
169+
clientOptions := cfg.ClientOptions(false)
170170
require.Equal(t, clientOptions.Auth.Username, cfg.Username)
171171
require.Equal(t,
172172
clientOptions.ConnectTimeout.Milliseconds(),
@@ -192,7 +192,7 @@ func TestOptionsTLS(t *testing.T) {
192192
},
193193
},
194194
}
195-
opts := cfg.ClientOptions()
195+
opts := cfg.ClientOptions(false)
196196
require.NotNil(t, opts.TLSConfig)
197197
}
198198

receiver/mongodbreceiver/documentation.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,54 @@ The number of replicated operations executed.
340340
| ---- | ----------- | ------ |
341341
| operation | The MongoDB operation being counted. | Str: ``insert``, ``query``, ``update``, ``delete``, ``getmore``, ``command`` |
342342
343+
### mongodb.repl_commands_per_sec
344+
345+
The number of replicated commands executed per second.
346+
347+
| Unit | Metric Type | Value Type |
348+
| ---- | ----------- | ---------- |
349+
| {command}/s | Gauge | Double |
350+
351+
### mongodb.repl_deletes_per_sec
352+
353+
The number of replicated deletes executed per second.
354+
355+
| Unit | Metric Type | Value Type |
356+
| ---- | ----------- | ---------- |
357+
| {delete}/s | Gauge | Double |
358+
359+
### mongodb.repl_getmores_per_sec
360+
361+
The number of replicated getmores executed per second.
362+
363+
| Unit | Metric Type | Value Type |
364+
| ---- | ----------- | ---------- |
365+
| {getmore}/s | Gauge | Double |
366+
367+
### mongodb.repl_inserts_per_sec
368+
369+
The number of replicated insertions executed per second.
370+
371+
| Unit | Metric Type | Value Type |
372+
| ---- | ----------- | ---------- |
373+
| {insert}/s | Gauge | Double |
374+
375+
### mongodb.repl_queries_per_sec
376+
377+
The number of replicated queries executed per second.
378+
379+
| Unit | Metric Type | Value Type |
380+
| ---- | ----------- | ---------- |
381+
| {query}/s | Gauge | Double |
382+
383+
### mongodb.repl_updates_per_sec
384+
385+
The number of replicated updates executed per second.
386+
387+
| Unit | Metric Type | Value Type |
388+
| ---- | ----------- | ---------- |
389+
| {update}/s | Gauge | Double |
390+
343391
### mongodb.uptime
344392
345393
The amount of time that the server has been running.

receiver/mongodbreceiver/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ require (
5555
github.com/gogo/protobuf v1.3.2 // indirect
5656
github.com/golang/snappy v0.0.4 // indirect
5757
github.com/google/uuid v1.6.0 // indirect
58+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1 // indirect
5859
github.com/json-iterator/go v1.1.12 // indirect
5960
github.com/klauspost/compress v1.17.9 // indirect
6061
github.com/knadh/koanf/maps v0.1.1 // indirect
@@ -110,7 +111,7 @@ require (
110111
golang.org/x/sync v0.11.0 // indirect
111112
golang.org/x/sys v0.30.0 // indirect
112113
golang.org/x/text v0.22.0 // indirect
113-
google.golang.org/genproto/googleapis/rpc v0.0.0-20241202173237-19429a94021a // indirect
114+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect
114115
google.golang.org/grpc v1.70.0 // indirect
115116
google.golang.org/protobuf v1.36.5 // indirect
116117
gopkg.in/yaml.v3 v3.0.1 // indirect

receiver/mongodbreceiver/go.sum

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/mongodbreceiver/internal/metadata/generated_config.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/mongodbreceiver/internal/metadata/generated_config_test.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)