Skip to content

Commit

Permalink
[mongodbreceiver] change 'Hosts' configuration type (#32550)
Browse files Browse the repository at this point in the history
**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
what changed:mongodbreceiver `Hosts` configuration use
`confignet.TCPAddrConfig`.

Hi master, I looked at the mongodbreceiver code: 

1. The error is caused by
[AddrConfig.Validate()](https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/confignet/confignet.go#L122)
return error.
```go
func (na *AddrConfig) Validate() error {
	switch na.Transport {
	case TransportTypeTCP,
		...
		return nil
	default:
		return fmt.Errorf("invalid transport type %q", na.Transport)
	}
}
```

2. `mongodbreceiver` use
[go.mongodb.org/mongo-driver](https://github.com/mongodb/mongo-go-driver)to
connect mongodb.


https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/receiver/mongodbreceiver/go.mod#L13
```go
module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbreceiver

go 1.21

require (
	...
	go.mongodb.org/mongo-driver v1.15.0
	...
)
```


3. `go.mongodb.org/mongo-driver` based on `TCP`.


https://github.com/mongodb/mongo-go-driver/blob/v1/x/mongo/driver/dns/dns.go#L82
```go
_, addresses, err := r.LookupSRV(srvName, "tcp", host)
```

So I think mongodbreceiver only supporting TCP.

**Link to tracking Issue:** <Issue number if applicable>

#32199

---------

Co-authored-by: Tyler Helmuth <[email protected]>
Co-authored-by: Daniel Jaglowski <[email protected]>
Co-authored-by: Curtis Robert <[email protected]>
  • Loading branch information
4 people authored Jun 11, 2024
1 parent dba9416 commit 21df779
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 21 deletions.
27 changes: 27 additions & 0 deletions .chloggen/fix-mongodbreceiver-hosts.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: breaking

# 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: Now only supports `TCP` connections

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

# (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: This fixes a bug where hosts had to explicitly set `tcp` as the transport type. The `transport` option has been removed.

# 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: []
3 changes: 1 addition & 2 deletions receiver/mongodbreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Mongodb recommends to set up a least privilege user (LPU) with a [`clusterMonito

The following settings are optional:

- `hosts` (default: [`localhost:27017`]): list of `host:port` or unix domain socket endpoints.
- `hosts` (default: [`localhost:27017`]): list of `host:port` or unix domain socket endpoints.The `transport` option is no longer available.
- For standalone MongoDB deployments this is the hostname and port of the mongod instance
- For replica sets specify the hostnames and ports of the mongod instances that are in the replica set configuration. If the `replica_set` field is specified, nodes will be autodiscovered.
- For a sharded MongoDB deployment, please specify a list of the `mongos` hosts.
Expand All @@ -52,7 +52,6 @@ receivers:
mongodb:
hosts:
- endpoint: localhost:27017
transport: tcp
username: otel
password: ${env:MONGODB_PASSWORD}
collection_interval: 60s
Expand Down
11 changes: 6 additions & 5 deletions receiver/mongodbreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ type Config struct {
configtls.ClientConfig `mapstructure:"tls,omitempty"`
// MetricsBuilderConfig defines which metrics/attributes to enable for the scraper
metadata.MetricsBuilderConfig `mapstructure:",squash"`
Hosts []confignet.AddrConfig `mapstructure:"hosts"`
Username string `mapstructure:"username"`
Password configopaque.String `mapstructure:"password"`
ReplicaSet string `mapstructure:"replica_set,omitempty"`
Timeout time.Duration `mapstructure:"timeout"`
// Deprecated - Transport option will be removed in v0.102.0
Hosts []confignet.TCPAddrConfig `mapstructure:"hosts"`
Username string `mapstructure:"username"`
Password configopaque.String `mapstructure:"password"`
ReplicaSet string `mapstructure:"replica_set,omitempty"`
Timeout time.Duration `mapstructure:"timeout"`
}

func (c *Config) Validate() error {
Expand Down
18 changes: 8 additions & 10 deletions receiver/mongodbreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ func TestValidate(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
var hosts []confignet.AddrConfig
var hosts []confignet.TCPAddrConfig

for _, ep := range tc.endpoints {
hosts = append(hosts, confignet.AddrConfig{
Endpoint: ep,
Transport: confignet.TransportTypeTCP,
hosts = append(hosts, confignet.TCPAddrConfig{
Endpoint: ep,
})
}

Expand Down Expand Up @@ -135,10 +134,9 @@ func TestBadTLSConfigs(t *testing.T) {
cfg := &Config{
Username: "otel",
Password: "pword",
Hosts: []confignet.AddrConfig{
Hosts: []confignet.TCPAddrConfig{
{
Endpoint: "localhost:27017",
Transport: confignet.TransportTypeTCP,
Endpoint: "localhost:27017",
},
},
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Expand All @@ -156,7 +154,7 @@ func TestBadTLSConfigs(t *testing.T) {

func TestOptions(t *testing.T) {
cfg := &Config{
Hosts: []confignet.AddrConfig{
Hosts: []confignet.TCPAddrConfig{
{
Endpoint: "localhost:27017",
},
Expand All @@ -181,7 +179,7 @@ func TestOptionsTLS(t *testing.T) {
caFile := filepath.Join("testdata", "certs", "ca.crt")

cfg := &Config{
Hosts: []confignet.AddrConfig{
Hosts: []confignet.TCPAddrConfig{
{
Endpoint: "localhost:27017",
},
Expand Down Expand Up @@ -209,7 +207,7 @@ func TestLoadConfig(t *testing.T) {
require.NoError(t, sub.Unmarshal(cfg))

expected := factory.CreateDefaultConfig().(*Config)
expected.Hosts = []confignet.AddrConfig{
expected.Hosts = []confignet.TCPAddrConfig{
{
Endpoint: "localhost:27017",
},
Expand Down
5 changes: 2 additions & 3 deletions receiver/mongodbreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ func createDefaultConfig() component.Config {
return &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Timeout: time.Minute,
Hosts: []confignet.AddrConfig{
Hosts: []confignet.TCPAddrConfig{
{
Endpoint: "localhost:27017",
Transport: confignet.TransportTypeTCP,
Endpoint: "localhost:27017",
},
},
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
Expand Down
2 changes: 1 addition & 1 deletion receiver/mongodbreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func integrationTest(name string, script []string, cfgMod func(*Config)) func(*t
cfgMod(rCfg)
rCfg.CollectionInterval = 2 * time.Second
rCfg.MetricsBuilderConfig.Metrics.MongodbLockAcquireTime.Enabled = false
rCfg.Hosts = []confignet.AddrConfig{
rCfg.Hosts = []confignet.TCPAddrConfig{
{
Endpoint: fmt.Sprintf("%s:%s", ci.Host(t), ci.MappedPort(t, mongoPort)),
},
Expand Down

0 comments on commit 21df779

Please sign in to comment.